@@ -4,26 +4,23 @@ import (
44 "context"
55 "fmt"
66 "net/http"
7- "regexp"
87 "sync/atomic"
98 "time"
109
1110 "github.com/divar-ir/golangfuse/internal/constants"
12- "github.com/divar-ir/golangfuse/internal/observer"
1311 "github.com/google/uuid"
1412 "resty.dev/v3"
1513)
1614
1715type Langfuse interface {
1816 StartSendingEvents (ctx context.Context , period time.Duration ) error
1917 Trace (input , output any , options ... TraceOption )
20- GetPromptTemplate (ctx context.Context , promptName string ) (string , error )
18+ GetSystemPromptTemplate (ctx context.Context , promptName string ) (string , error )
2119}
2220
2321type langfuseImpl struct {
2422 restClient * resty.Client
25- eventObserver observer.Observer [IngestionEvent ]
26- eventQueue observer.Queue [IngestionEvent ]
23+ eventBuffer * eventBuffer
2724 isSendingEventsStarted atomic.Bool
2825 endpoint string
2926 promptLabel string
@@ -37,24 +34,23 @@ func NewWithHttpClient(httpClient *http.Client, endpoint, publicKey, secretKey s
3734 client := resty .NewWithClient (httpClient ).SetBasicAuth (publicKey , secretKey )
3835 c := & langfuseImpl {
3936 restClient : client ,
40- eventQueue : observer .NewQueue [IngestionEvent ](),
4137 endpoint : endpoint ,
4238 promptLabel : "production" , // TODO: use option pattern to override this default if needed
4339 }
44- c .eventObserver = observer . NewObserver [ IngestionEvent ]( c . eventQueue , c .sendEvents )
40+ c .eventBuffer = newEventBufferer ( c .sendEvents )
4541 return c
4642}
4743
4844func (c * langfuseImpl ) StartSendingEvents (ctx context.Context , period time.Duration ) error {
4945 if c .isSendingEventsStarted .CompareAndSwap (false , true ) {
50- go c .eventObserver . StartObserve (ctx , period )
46+ go c .eventBuffer . Start (ctx , period )
5147 return nil
5248 } else {
5349 return AlreadyStartedErr
5450 }
5551}
5652
57- func (c * langfuseImpl ) GetPromptTemplate (ctx context.Context , promptName string ) (string , error ) {
53+ func (c * langfuseImpl ) GetSystemPromptTemplate (ctx context.Context , promptName string ) (string , error ) {
5854 promptObject := ChatPrompt {}
5955 resp , err := c .restClient .R ().
6056 SetContext (ctx ).
@@ -75,7 +71,7 @@ func (c *langfuseImpl) GetPromptTemplate(ctx context.Context, promptName string)
7571 if promptObject .Prompt [0 ].Role != "system" {
7672 return "" , fmt .Errorf ("prompt role is not system" )
7773 }
78- return convertJinjaVariablesToGoTemplate ( promptObject .Prompt [0 ].Content ) , nil
74+ return promptObject .Prompt [0 ].Content , nil
7975}
8076
8177func (c * langfuseImpl ) Trace (input , output any , options ... TraceOption ) {
@@ -86,7 +82,7 @@ func (c *langfuseImpl) Trace(input, output any, options ...TraceOption) {
8682 for _ , opt := range options {
8783 opt (trace )
8884 }
89- c .eventQueue . Enqueue (IngestionEvent {
85+ c .eventBuffer . Add (IngestionEvent {
9086 ID : uuid .NewString (),
9187 Timestamp : time .Now (),
9288 Type : constants .IngestionEventTypeTraceCreate ,
@@ -108,8 +104,3 @@ func (c *langfuseImpl) sendEvents(ctx context.Context, events []IngestionEvent)
108104 }
109105 return nil
110106}
111-
112- func convertJinjaVariablesToGoTemplate (prompt string ) string {
113- re := regexp .MustCompile (`\{\{\s*([a-zA-Z0-9_]+)\s*\}\}` )
114- return re .ReplaceAllString (prompt , "{{.$1}}" )
115- }
0 commit comments