Skip to content

Commit

Permalink
fix: assistant observer
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Jun 2, 2024
1 parent a86c0e3 commit 1689742
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
11 changes: 11 additions & 0 deletions assistant/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assistant

import (
"context"
"fmt"
"strings"

obs "github.com/henomis/lingoose/observer"
Expand Down Expand Up @@ -94,11 +95,21 @@ func (a *Assistant) Run(ctx context.Context) error {
}

for i := 0; i < int(a.maxIterations); i++ {
ctx, spanIteration, err := a.startObserveSpan(ctx, fmt.Sprintf("iteration-%d", i+1))

Check failure on line 98 in assistant/assistant.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "ctx" shadows declaration at line 78 (govet)
if err != nil {
return err
}

err = a.llm.Generate(ctx, a.thread)
if err != nil {
return err
}

err = a.stopObserveSpan(ctx, spanIteration)
if err != nil {
return err
}

if a.thread.LastMessage().Role != thread.RoleTool {
break
}
Expand Down
2 changes: 1 addition & 1 deletion assistant/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const (
//nolint:lll
baseRAGPrompt = "Use the following pieces of retrieved context to answer the question.\n\nQuestion: {{.question}}\nContext:\n{{range .results}}{{.}}\n\n{{end}}"
//nolint:lll
systemPrompt = "You name is {{.assistantName}}, and you are {{.assistantIdentity}} {{if ne .companyName \"\" }}at {{.companyName}}{{end}}{{if ne .companyDescription \"\" }}, {{.companyDescription}}{{end}}. Your task is to assist humans {{.assistantScope}}."
systemPrompt = "{{if ne .assistantName \"\"}}You name is {{.assistantName}}, {{end}}{{if ne .assistantIdentity \"\"}}you are {{.assistantIdentity}}.{{end}} {{if ne .companyName \"\" }}at {{.companyName}}{{end}}{{if ne .companyDescription \"\" }}, {{.companyDescription}}.{{end}} Your task is to assist humans {{.assistantScope}}."

defaultAssistantName = "AI assistant"
defaultAssistantIdentity = "a helpful and polite assistant"
Expand Down
26 changes: 11 additions & 15 deletions examples/assistant/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,41 @@ import (
func main() {
ctx := context.Background()

o := langfuse.New(ctx)
trace, err := o.Trace(&observer.Trace{Name: "state of the union"})
langfuseObserver := langfuse.New(ctx)
trace, err := langfuseObserver.Trace(&observer.Trace{Name: "Average Temperature calculator"})
if err != nil {
panic(err)
}

ctx = observer.ContextWithObserverInstance(ctx, o)
ctx = observer.ContextWithObserverInstance(ctx, langfuseObserver)
ctx = observer.ContextWithTraceID(ctx, trace.ID)

auto := "auto"
a := assistant.New(
myAssistant := assistant.New(
openai.New().WithModel(openai.GPT4o).WithToolChoice(&auto).WithTools(
pythontool.New(),
serpapitool.New(),
),
).WithParameters(
assistant.Parameters{
AssistantName: "AI Assistant",
AssistantIdentity: "an helpful assistant",
AssistantScope: "with their questions.",
CompanyName: "",
CompanyDescription: "",
AssistantName: "AI Assistant",
AssistantIdentity: "a helpful assistant",
AssistantScope: "answering questions",
},
).WithThread(
thread.New().AddMessages(
thread.NewUserMessage().AddContent(
thread.NewTextContent("calculate the average temperature in celsius degrees of New York, Rome, and Tokyo."),
thread.NewTextContent("Search the current temperature of New York, Rome, and Tokyo, then calculate the average temperature in Celsius."),
),
),
).WithMaxIterations(10)

err = a.Run(ctx)
err = myAssistant.Run(ctx)
if err != nil {
panic(err)
}

fmt.Println("----")
fmt.Println(a.Thread())
fmt.Println("----")
fmt.Println(myAssistant.Thread())

o.Flush(ctx)
langfuseObserver.Flush(ctx)
}
8 changes: 7 additions & 1 deletion tool/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (t *Tool) Name() string {
}

func (t *Tool) Description() string {
return "A tool that runs Python code using the Python interpreter. The code should print the final result to stdout."
return "A tool that runs Python code using the Python interpreter. Use this tool to solve calculations, manipulate data, or perform any other Python-related tasks. The code should print the final result to stdout."

Check failure on line 40 in tool/python/python.go

View workflow job for this annotation

GitHub Actions / lint

line is 215 characters (lll)
}

func (t *Tool) Fn() any {
Expand All @@ -63,6 +63,12 @@ func (t *Tool) fn(i Input) Output {
}
}

if out.String() == "" {
return Output{
Error: "no output from script, script must print the final result to stdout",
}
}

// Return the output as a string.
return Output{Result: out.String()}
}

0 comments on commit 1689742

Please sign in to comment.