diff --git a/assistant/assistant.go b/assistant/assistant.go index 6b4764b9..fb3b9dce 100644 --- a/assistant/assistant.go +++ b/assistant/assistant.go @@ -2,6 +2,7 @@ package assistant import ( "context" + "fmt" "strings" obs "github.com/henomis/lingoose/observer" @@ -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)) + 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 } diff --git a/assistant/prompt.go b/assistant/prompt.go index 29f04df8..c6f14b08 100644 --- a/assistant/prompt.go +++ b/assistant/prompt.go @@ -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" diff --git a/examples/assistant/agent/main.go b/examples/assistant/agent/main.go index dac59c26..5c0e723b 100644 --- a/examples/assistant/agent/main.go +++ b/examples/assistant/agent/main.go @@ -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) } diff --git a/tool/python/python.go b/tool/python/python.go index 2ac686f2..ff677735 100644 --- a/tool/python/python.go +++ b/tool/python/python.go @@ -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." } func (t *Tool) Fn() any { @@ -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()} }