From 863342d6786232bc6690dd0c01d67d8dde4920cb Mon Sep 17 00:00:00 2001 From: Simone Vellei Date: Wed, 22 May 2024 15:40:18 +0200 Subject: [PATCH] chore: fix example --- examples/llm/openai/tools/rag/main.go | 63 +++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 examples/llm/openai/tools/rag/main.go diff --git a/examples/llm/openai/tools/rag/main.go b/examples/llm/openai/tools/rag/main.go new file mode 100644 index 00000000..9bc147da --- /dev/null +++ b/examples/llm/openai/tools/rag/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "context" + "fmt" + "os" + + openaiembedder "github.com/henomis/lingoose/embedder/openai" + "github.com/henomis/lingoose/index" + "github.com/henomis/lingoose/index/vectordb/jsondb" + "github.com/henomis/lingoose/llm/openai" + "github.com/henomis/lingoose/rag" + "github.com/henomis/lingoose/thread" + "github.com/henomis/lingoose/tools/duckduckgo" + ragtool "github.com/henomis/lingoose/tools/rag" +) + +func main() { + + rag := rag.New( + index.New( + jsondb.New().WithPersist("index.json"), + openaiembedder.New(openaiembedder.AdaEmbeddingV2), + ), + ).WithChunkSize(1000).WithChunkOverlap(0) + + _, err := os.Stat("index.json") + if os.IsNotExist(err) { + err = rag.AddSources(context.Background(), "state_of_the_union.txt") + if err != nil { + panic(err) + } + } + + newStr := func(str string) *string { + return &str + } + llm := openai.New().WithModel(openai.GPT4o).WithToolChoice(newStr("auto")).WithTools( + ragtool.New(rag, "US covid vaccines"), + duckduckgo.New().WithMaxResults(5), + ) + + topics := []string{ + "how many covid vaccine doses US has donated to other countries", + "apple stock price", + } + + for _, topic := range topics { + t := thread.New().AddMessage( + thread.NewUserMessage().AddContent( + thread.NewTextContent("I would like to know something about " + topic + "."), + ), + ) + + llm.Generate(context.Background(), t) + if t.LastMessage().Role == thread.RoleTool { + llm.Generate(context.Background(), t) + } + + fmt.Println(t) + } + +}