Skip to content

Commit

Permalink
fix: tools
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Jun 5, 2024
1 parent 7d37e2d commit 5edf00c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 46 deletions.
6 changes: 4 additions & 2 deletions examples/assistant/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/henomis/lingoose/observer/langfuse"
"github.com/henomis/lingoose/thread"

humantool "github.com/henomis/lingoose/tool/human"
pythontool "github.com/henomis/lingoose/tool/python"
serpapitool "github.com/henomis/lingoose/tool/serpapi"
)
Expand All @@ -18,7 +19,7 @@ func main() {
ctx := context.Background()

langfuseObserver := langfuse.New(ctx)
trace, err := langfuseObserver.Trace(&observer.Trace{Name: "Average Temperature calculator"})
trace, err := langfuseObserver.Trace(&observer.Trace{Name: "Italian guests calculator"})
if err != nil {
panic(err)
}
Expand All @@ -31,6 +32,7 @@ func main() {
openai.New().WithModel(openai.GPT4o).WithToolChoice(&auto).WithTools(
pythontool.New(),
serpapitool.New(),
humantool.New(),
),
).WithParameters(
assistant.Parameters{
Expand All @@ -41,7 +43,7 @@ func main() {
).WithThread(
thread.New().AddMessages(
thread.NewUserMessage().AddContent(
thread.NewTextContent("Search the current temperature of New York, Rome, and Tokyo, then calculate the average temperature in Celsius."),
thread.NewTextContent("search the top 3 italian dishes and then their costs, then ask the user's budget in euros and calculate how many guests can be invited for each dish"),
),
),
).WithMaxIterations(10)
Expand Down
44 changes: 44 additions & 0 deletions tool/human/human.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package human

import (
"fmt"
)

type Tool struct {
}

func New() *Tool {
return &Tool{}
}

type Input struct {
Question string `json:"question" jsonschema:"description=the question to ask the human"`
}

type Output struct {
Error string `json:"error,omitempty"`
Result string `json:"result,omitempty"`
}

type FnPrototype = func(Input) Output

func (t *Tool) Name() string {
return "human"
}

func (t *Tool) Description() string {
return "A tool that asks a question to a human and returns the answer. Use it to interact with a human."
}

func (t *Tool) Fn() any {
return t.fn
}

func (t *Tool) fn(i Input) Output {
var answer string

fmt.Printf("\n\n%s > ", i.Question)
fmt.Scanln(&answer)

return Output{Result: answer}
}
4 changes: 2 additions & 2 deletions tool/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (t *Tool) WithPythonPath(pythonPath string) *Tool {
}

type Input struct {
PythonCode string `json:"python_code" jsonschema:"description=python code that prints the final result to stdout."`
PythonCode string `json:"python_code" jsonschema:"description=python code that uses print() to print the final result to stdout."`

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

View workflow job for this annotation

GitHub Actions / lint

line is 131 characters (lll)
}

type Output struct {
Expand All @@ -38,7 +38,7 @@ func (t *Tool) Name() string {

//nolint:lll
func (t *Tool) Description() string {
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."
return "Use this tool to solve calculations, manipulate data, or perform any other Python-related tasks. The code should use print() to print the final result to stdout."
}

func (t *Tool) Fn() any {
Expand Down
42 changes: 0 additions & 42 deletions tool/serpapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,48 +94,6 @@ func (r *response) Decode(body io.Reader) error {
return err
}

if r.apiResponse.AnswerBox != nil {
answerBox, errMarshall := json.Marshal(r.apiResponse.AnswerBox)
if errMarshall != nil {
return errMarshall
}

r.Results = append(r.Results, result{
Title: "Answer Box",
Info: string(answerBox),
})

return nil
}

if r.apiResponse.SportsResults != nil {
sportsResults, errMarshall := json.Marshal(r.apiResponse.SportsResults)
if errMarshall != nil {
return errMarshall
}

r.Results = append(r.Results, result{
Title: "Sports Results",
Info: string(sportsResults),
})

return nil
}

if r.apiResponse.KnowledgeGraph != nil {
knowledgeGraph, errMarshall := json.Marshal(r.apiResponse.KnowledgeGraph)
if errMarshall != nil {
return errMarshall
}

r.Results = append(r.Results, result{
Title: "Knowledge Graph",
Info: string(knowledgeGraph),
})

return nil
}

for _, res := range r.apiResponse.OrganicResults {
r.Results = append(r.Results, result{
Title: res.Title,
Expand Down

0 comments on commit 5edf00c

Please sign in to comment.