-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
518 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: "Observer" | ||
description: | ||
linkTitle: "Observer" | ||
menu: { main: { parent: 'reference', weight: -92 } } | ||
--- | ||
|
||
## Observer | ||
|
||
The `Observer` interface helps to observe, debug, and analyze LLM applications. This component tracks metrics (e.g. LLM cost, latency, quality) and gains insights from external dashboards and data exports. To enable tracing an LLM application, create an observer and pass it to the LLM instance. | ||
|
||
### Supported platform | ||
|
||
* [Langfuse](https://langfuse.com/) | ||
|
||
### Usage | ||
|
||
```go | ||
|
||
o := langfuse.New(context.Background()) | ||
trace, err := o.Trace(&observer.Trace{Name: "Who are you"}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
openaillm := openai.New().WithObserver(o, trace.ID) | ||
|
||
t := thread.New().AddMessage( | ||
thread.NewUserMessage().AddContent( | ||
thread.NewTextContent("Hello, who are you?"), | ||
), | ||
) | ||
|
||
err = openaillm.Generate(context.Background(), t) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
o.Flush(context.Background()) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/henomis/lingoose/observer" | ||
"github.com/henomis/lingoose/observer/langfuse" | ||
"github.com/henomis/lingoose/thread" | ||
"github.com/henomis/lingoose/types" | ||
) | ||
|
||
func main() { | ||
l := langfuse.New(context.Background()) | ||
|
||
trace, err := l.Trace( | ||
&observer.Trace{ | ||
Name: "trace", | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
span, err := l.Span( | ||
&observer.Span{ | ||
Name: "span", | ||
TraceID: trace.ID, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
generation, err := l.Generation( | ||
&observer.Generation{ | ||
ParentID: span.ID, | ||
TraceID: trace.ID, | ||
Name: "generation", | ||
Model: "gpt-3.5-turbo", | ||
ModelParameters: types.M{ | ||
"maxTokens": "1000", | ||
"temperature": "0.9", | ||
}, | ||
Input: []*thread.Message{ | ||
{ | ||
Role: thread.RoleSystem, | ||
Contents: []*thread.Content{ | ||
{ | ||
Type: thread.ContentTypeText, | ||
Data: "You are a helpful assistant.", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Role: thread.RoleUser, | ||
Contents: []*thread.Content{ | ||
{ | ||
Type: thread.ContentTypeText, | ||
Data: "Please generate a summary of the following documents \nThe engineering department defined the following OKR goals...\nThe marketing department defined the following OKR goals...", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Metadata: types.M{ | ||
"key": "value", | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = l.Event( | ||
&observer.Event{ | ||
ParentID: generation.ID, | ||
TraceID: trace.ID, | ||
Name: "event", | ||
Metadata: types.M{ | ||
"key": "value", | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
generation.Output = &thread.Message{ | ||
Role: thread.RoleAssistant, | ||
Contents: []*thread.Content{ | ||
{ | ||
Type: thread.ContentTypeText, | ||
Data: "The Q3 OKRs contain goals for multiple teams...", | ||
}, | ||
}, | ||
} | ||
|
||
_, err = l.GenerationEnd(generation) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = l.Score( | ||
&observer.Score{ | ||
TraceID: trace.ID, | ||
Name: "score", | ||
Value: 0.9, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = l.SpanEnd(span) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
l.Flush(context.Background()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/henomis/lingoose/llm/openai" | ||
"github.com/henomis/lingoose/observer" | ||
"github.com/henomis/lingoose/observer/langfuse" | ||
"github.com/henomis/lingoose/thread" | ||
) | ||
|
||
func main() { | ||
o := langfuse.New(context.Background()) | ||
trace, err := o.Trace(&observer.Trace{Name: "Who are you"}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
openaillm := openai.New().WithObserver(o, trace.ID) | ||
|
||
t := thread.New().AddMessage( | ||
thread.NewUserMessage().AddContent( | ||
thread.NewTextContent("Hello, who are you?"), | ||
), | ||
) | ||
|
||
err = openaillm.Generate(context.Background(), t) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
o.Flush(context.Background()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.