From cfbb9e66dff37edd98afe2b4ce7ae93d39c2437f Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 9 May 2024 10:43:51 -0700 Subject: [PATCH 01/10] [Go] update dotprompt model strings to use provider/model Also update coffee-shop to use GOOGLE_API_KEY rather than GEMINI_API_KEY. --- go/ai/generator.go | 4 ++-- go/genkit/dotprompt/genkit.go | 6 +++++- go/samples/coffee-shop/main.go | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/go/ai/generator.go b/go/ai/generator.go index 8833cd009f..423669557c 100644 --- a/go/ai/generator.go +++ b/go/ai/generator.go @@ -74,8 +74,8 @@ type generatorActionType = genkit.Action[*GenerateRequest, *GenerateResponse, *C // LookupGeneratorAction looks up an action registered by [RegisterGenerator] // and returns a generator that invokes the action. -func LookupGeneratorAction(name string) (Generator, error) { - action := genkit.LookupAction(genkit.ActionTypeModel, name, name) +func LookupGeneratorAction(provider, name string) (Generator, error) { + action := genkit.LookupAction(genkit.ActionTypeModel, provider, name) if action == nil { return nil, fmt.Errorf("LookupGeneratorAction: no generator action named %q", name) } diff --git a/go/genkit/dotprompt/genkit.go b/go/genkit/dotprompt/genkit.go index a44cf2c25b..841c1acdb9 100644 --- a/go/genkit/dotprompt/genkit.go +++ b/go/genkit/dotprompt/genkit.go @@ -184,8 +184,12 @@ func (p *Prompt) Execute(ctx context.Context, input *ActionInput) (*ai.GenerateR if model == "" { return nil, errors.New("dotprompt action: model not specified") } + provider, name, found := strings.Cut(model, "/") + if !found { + return nil, errors.New("dotprompt model not in provider/name format") + } - generator, err = ai.LookupGeneratorAction(model) + generator, err = ai.LookupGeneratorAction(provider, name) if err != nil { return nil, err } diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index f17186a47c..8a304483f7 100644 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -72,9 +72,9 @@ type testAllCoffeeFlowsOutput struct { } func main() { - apiKey := os.Getenv("GEMINI_API_KEY") + apiKey := os.Getenv("GOOGLE_API_KEY") if apiKey == "" { - fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GEMINI_API_KEY in the environment.") + fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GOOGLE_API_KEY in the environment.") fmt.Fprintln(os.Stderr, "You can get an API key at https://ai.google.dev.") os.Exit(1) } @@ -86,7 +86,7 @@ func main() { simpleGreetingPrompt, err := dotprompt.Define("simpleGreeting", &dotprompt.Frontmatter{ Name: "simpleGreeting", - Model: "google-genai", + Model: "google-genai/gemini-1.0-pro", Input: dotprompt.FrontmatterInput{ Schema: jsonschema.Reflect(simpleGreetingInput{}), }, @@ -121,7 +121,7 @@ func main() { greetingWithHistoryPrompt, err := dotprompt.Define("greetingWithHistory", &dotprompt.Frontmatter{ Name: "greetingWithHistory", - Model: "google-genai", + Model: "google-genai/gemini-1.0-pro", Input: dotprompt.FrontmatterInput{ Schema: jsonschema.Reflect(customerTimeAndHistoryInput{}), }, From 9b0872f58691bbcb0ca4207cfc569d88de2dce90 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 9 May 2024 10:47:49 -0700 Subject: [PATCH 02/10] change to GOOGLE_GENAI_API_KEY --- go/samples/coffee-shop/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index 8a304483f7..953eb8fe5e 100644 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -72,9 +72,9 @@ type testAllCoffeeFlowsOutput struct { } func main() { - apiKey := os.Getenv("GOOGLE_API_KEY") + apiKey := os.Getenv("GOOGLE_GENAI_API_KEY") if apiKey == "" { - fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GOOGLE_API_KEY in the environment.") + fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GOOGLE_GENAI_API_KEY in the environment.") fmt.Fprintln(os.Stderr, "You can get an API key at https://ai.google.dev.") os.Exit(1) } From d3c98f5d0bb8d31db9732a9d8f1db669a17a802c Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Thu, 23 May 2024 16:18:23 +0000 Subject: [PATCH 03/10] WIP go rag --- go/samples/coffee-shop/main.go | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) mode change 100644 => 100755 go/samples/coffee-shop/main.go diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go old mode 100644 new mode 100755 index 73171c3979..3108a9b345 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -32,6 +32,7 @@ import ( "github.com/firebase/genkit/go/genkit" "github.com/firebase/genkit/go/genkit/dotprompt" "github.com/firebase/genkit/go/plugins/googleai" + "github.com/firebase/genkit/go/plugins/localvec" "github.com/invopop/jsonschema" ) @@ -41,10 +42,22 @@ A regular customer named {{customerName}} enters. Greet the customer in one sentence, and recommend a coffee drink. ` +const simpleQaPromptTemplate = ` +You're a helpful agent that answers the user's common questions based on your knowledge. + +Here is the user's query: {{question}} + +Please provide the best answer you can. +` + type simpleGreetingInput struct { CustomerName string `json:"customerName"` } +type simpleQaInput struct { + Question string `json:"question"` +} + const greetingWithHistoryPromptTemplate = ` {{role "user"}} Hi, my name is {{customerName}}. The time is {{currentTime}}. Who are you? @@ -111,6 +124,56 @@ func main() { return text, nil }) + simpleQaPrompt, err := dotprompt.Define("simpleQaPrompt", + simpleQaPromptTemplate, + &dotprompt.Config{ + Model: "google-genai/gemini-1.0-pro", + InputSchema: jsonschema.Reflect(simpleQaInput{}), + OutputFormat: ai.OutputFormatText, + }, + ) + if err != nil { + log.Fatal(err) + } + + genkit.DefineFlow("simpleQaFlow", func(ctx context.Context, input *simpleQaInput, _ genkit.NoStream) (string, error) { + fmt.Println("Input recieved!!!!!") + fmt.Println(input) + + d1 := ai.DocumentFromText("Paris is the capital of France", nil) + d2 := ai.DocumentFromText("USA is the largest importer of coffee", nil) + d3 := ai.DocumentFromText("Water exists in 3 states - solid, liquid and gas", nil) + + embedder, err := googleai.NewEmbedder(ctx, "embedding-001", apiKey) + if err != nil { + return "", err + } + localDb, err := localvec.New(ctx, "/tmp/", "simpleQa", embedder, nil) + if err != nil { + return "", err + } + + indexerReq := &ai.IndexerRequest{ + Documents: []*ai.Document{d1, d2, d3}, + } + localDb.Index(ctx, indexerReq) + + vars, err := simpleGreetingPrompt.BuildVariables(input) + if err != nil { + return "", err + } + ai := &dotprompt.ActionInput{Variables: vars} + resp, err := simpleQaPrompt.Execute(ctx, ai) + if err != nil { + return "", err + } + text, err := resp.Text() + if err != nil { + return "", fmt.Errorf("simpleQa: %v", err) + } + return text, nil + }) + greetingWithHistoryPrompt, err := dotprompt.Define("greetingWithHistory", greetingWithHistoryPromptTemplate, &dotprompt.Config{ Model: "google-genai/gemini-1.0-pro", From 0524954ddecb6a5d2ac1e61fe9e057aa6ca3777d Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 14:31:25 +0000 Subject: [PATCH 04/10] new sample --- go/samples/coffee-shop/main.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index 3108a9b345..93d64321b3 100755 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -137,9 +137,6 @@ func main() { } genkit.DefineFlow("simpleQaFlow", func(ctx context.Context, input *simpleQaInput, _ genkit.NoStream) (string, error) { - fmt.Println("Input recieved!!!!!") - fmt.Println(input) - d1 := ai.DocumentFromText("Paris is the capital of France", nil) d2 := ai.DocumentFromText("USA is the largest importer of coffee", nil) d3 := ai.DocumentFromText("Water exists in 3 states - solid, liquid and gas", nil) From e6a43d0af705fd72544c3772637888a1af5a4909 Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 16:59:29 +0000 Subject: [PATCH 05/10] Cleaner sample --- go/samples/coffee-shop/main.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index b279a98f09..b0603cb234 100755 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -19,11 +19,8 @@ // // go run . & // -// Tell it to run an action. Here are some examples: -// -// Flow without input: +// Tell it to run a flow: // -// curl -d '{"key":"/flow/testAllCoffeeFlows/testAllCoffeeFlows", "input":{"start": {"input":null}}}' http://localhost:3100/api/runAction // Flow with input: // // curl -d '{"key":"/flow/simpleGreeting/simpleGreeting", "input":{"start": {"input":{"customerName": "John Doe"}}}}' http://localhost:3100/api/runAction From 5ad10d634f75b478a77f0f85918aa97bee7ff476 Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 17:28:31 +0000 Subject: [PATCH 06/10] Update run instruction --- go/samples/flow-sample1/main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/go/samples/flow-sample1/main.go b/go/samples/flow-sample1/main.go index a0107f9eb9..274d240d92 100644 --- a/go/samples/flow-sample1/main.go +++ b/go/samples/flow-sample1/main.go @@ -17,9 +17,19 @@ // // go run . & // -// Tell it to run an action: +// Tell it to run a flow: // -// curl -d '{"key":"/flow/parent/parent", "input":{"start": {"input":null}}}' http://localhost:3100/api/runAction +// curl -d '{"key":"/flow/parent/parent", "input":{"start": {"input":null}}}' http://localhost:3100/api/runAction +// +// In production mode (GENKIT_ENV missing or set to "prod"): +// Start the server listening on port 3400: +// +// go run . & +// +// Tell it to run a flow: +// +// curl -d '{}' http://localhost:3400/parent + package main import ( From 0aed3cdd208c49d931bf5c81ebca41aa3062a039 Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 19:14:43 +0000 Subject: [PATCH 07/10] Finish RAG sample --- go/samples/coffee-shop/main.go | 36 ++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index b0603cb234..ec478ca7bb 100755 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -21,8 +21,6 @@ // // Tell it to run a flow: // -// Flow with input: -// // curl -d '{"key":"/flow/simpleGreeting/simpleGreeting", "input":{"start": {"input":{"customerName": "John Doe"}}}}' http://localhost:3100/api/runAction // // In production mode (GENKIT_ENV missing or set to "prod"): @@ -57,9 +55,11 @@ Greet the customer in one sentence, and recommend a coffee drink. ` const simpleQaPromptTemplate = ` -You're a helpful agent that answers the user's common questions based on your knowledge. +You're a helpful agent that answers the user's common questions based on the context provided. + +Here is the user's query: {{query}} -Here is the user's query: {{question}} +Here is the context you should use: {{context}} Please provide the best answer you can. ` @@ -72,6 +72,11 @@ type simpleQaInput struct { Question string `json:"question"` } +type simpleQaPromptInput struct { + Query string `json:"query"` + Context string `json:"context"` +} + const greetingWithHistoryPromptTemplate = ` {{role "user"}} Hi, my name is {{customerName}}. The time is {{currentTime}}. Who are you? @@ -142,7 +147,7 @@ func main() { simpleQaPromptTemplate, &dotprompt.Config{ Model: "google-genai/gemini-1.0-pro", - InputSchema: jsonschema.Reflect(simpleQaInput{}), + InputSchema: jsonschema.Reflect(simpleQaPromptInput{}), OutputFormat: ai.OutputFormatText, }, ) @@ -169,7 +174,26 @@ func main() { } localDb.Index(ctx, indexerReq) - vars, err := simpleGreetingPrompt.BuildVariables(input) + dRequest := ai.DocumentFromText(input.Question, nil) + retrieverReq := &ai.RetrieverRequest{ + Document: dRequest, + } + response, err := localDb.Retrieve(ctx, retrieverReq) + if err != nil { + return "", err + } + + var context string + for _, d := range response.Documents { + context += d.Content[0].Text() + "\n" + } + + promptInput := &simpleQaPromptInput{ + Query: input.Question, + Context: context, + } + + vars, err := simpleQaPrompt.BuildVariables(promptInput) if err != nil { return "", err } From 301082e3f8dbc8fd57fd15c40f4d172b5a8ddbe5 Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 19:23:12 +0000 Subject: [PATCH 08/10] Separate RAG sample --- go/samples/rag/main.go | 151 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 go/samples/rag/main.go diff --git a/go/samples/rag/main.go b/go/samples/rag/main.go new file mode 100644 index 0000000000..cddc7c4537 --- /dev/null +++ b/go/samples/rag/main.go @@ -0,0 +1,151 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This program can be manually tested like so: +// +// In development mode (with the environment variable GENKIT_ENV="dev"): +// Start the server listening on port 3100: +// +// go run . & +// +// Tell it to run a flow: +// +// curl -d '{"key":"/flow/simpleQaFlow/simpleQaFlow", "input":{"start": {"input":{"question": "What is the capital of UK?"}}}}' http://localhost:3100/api/runAction +// +// In production mode (GENKIT_ENV missing or set to "prod"): +// Start the server listening on port 3400: +// +// go run . & +// +// Tell it to run a flow: +// +// curl -d '{"question": "What is the capital of UK?"}' http://localhost:3400/simpleQaFlow + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "github.com/firebase/genkit/go/ai" + "github.com/firebase/genkit/go/genkit" + "github.com/firebase/genkit/go/plugins/dotprompt" + "github.com/firebase/genkit/go/plugins/googleai" + "github.com/firebase/genkit/go/plugins/localvec" + "github.com/invopop/jsonschema" +) + +const simpleQaPromptTemplate = ` +You're a helpful agent that answers the user's common questions based on the context provided. + +Here is the user's query: {{query}} + +Here is the context you should use: {{context}} + +Please provide the best answer you can. +` + +type simpleQaInput struct { + Question string `json:"question"` +} + +type simpleQaPromptInput struct { + Query string `json:"query"` + Context string `json:"context"` +} + +func main() { + apiKey := os.Getenv("GOOGLE_GENAI_API_KEY") + if apiKey == "" { + fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GOOGLE_GENAI_API_KEY in the environment.") + fmt.Fprintln(os.Stderr, "You can get an API key at https://ai.google.dev.") + os.Exit(1) + } + + if err := googleai.Init(context.Background(), "gemini-1.0-pro", apiKey); err != nil { + log.Fatal(err) + } + + simpleQaPrompt, err := dotprompt.Define("simpleQaPrompt", + simpleQaPromptTemplate, + &dotprompt.Config{ + Model: "google-genai/gemini-1.0-pro", + InputSchema: jsonschema.Reflect(simpleQaPromptInput{}), + OutputFormat: ai.OutputFormatText, + }, + ) + if err != nil { + log.Fatal(err) + } + + embedder, err := googleai.NewEmbedder(context.Background(), "embedding-001", apiKey) + if err != nil { + log.Fatal(err) + } + localDb, err := localvec.New(context.Background(), "/tmp/", "simpleQa", embedder, nil) + if err != nil { + log.Fatal(err) + } + + genkit.DefineFlow("simpleQaFlow", func(ctx context.Context, input *simpleQaInput, _ genkit.NoStream) (string, error) { + d1 := ai.DocumentFromText("Paris is the capital of France", nil) + d2 := ai.DocumentFromText("USA is the largest importer of coffee", nil) + d3 := ai.DocumentFromText("Water exists in 3 states - solid, liquid and gas", nil) + + indexerReq := &ai.IndexerRequest{ + Documents: []*ai.Document{d1, d2, d3}, + } + localDb.Index(ctx, indexerReq) + + dRequest := ai.DocumentFromText(input.Question, nil) + retrieverReq := &ai.RetrieverRequest{ + Document: dRequest, + } + response, err := localDb.Retrieve(ctx, retrieverReq) + if err != nil { + return "", err + } + + var context string + for _, d := range response.Documents { + context += d.Content[0].Text() + "\n" + } + + promptInput := &simpleQaPromptInput{ + Query: input.Question, + Context: context, + } + + vars, err := simpleQaPrompt.BuildVariables(promptInput) + if err != nil { + return "", err + } + ai := &dotprompt.ActionInput{Variables: vars} + resp, err := simpleQaPrompt.Execute(ctx, ai) + if err != nil { + return "", err + } + text, err := resp.Text() + if err != nil { + return "", fmt.Errorf("simpleQa: %v", err) + } + return text, nil + }) + + if err := genkit.StartFlowServer(""); err != nil { + log.Fatal(err) + } +} From 19213856fb9f38049a4f813bf0f4cd0f86ed73ba Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Tue, 28 May 2024 19:48:57 +0000 Subject: [PATCH 09/10] More fixes --- go/samples/coffee-shop/main.go | 86 ---------------------------------- 1 file changed, 86 deletions(-) diff --git a/go/samples/coffee-shop/main.go b/go/samples/coffee-shop/main.go index ec478ca7bb..6399844474 100755 --- a/go/samples/coffee-shop/main.go +++ b/go/samples/coffee-shop/main.go @@ -44,7 +44,6 @@ import ( "github.com/firebase/genkit/go/genkit" "github.com/firebase/genkit/go/plugins/dotprompt" "github.com/firebase/genkit/go/plugins/googleai" - "github.com/firebase/genkit/go/plugins/localvec" "github.com/invopop/jsonschema" ) @@ -54,29 +53,10 @@ A regular customer named {{customerName}} enters. Greet the customer in one sentence, and recommend a coffee drink. ` -const simpleQaPromptTemplate = ` -You're a helpful agent that answers the user's common questions based on the context provided. - -Here is the user's query: {{query}} - -Here is the context you should use: {{context}} - -Please provide the best answer you can. -` - type simpleGreetingInput struct { CustomerName string `json:"customerName"` } -type simpleQaInput struct { - Question string `json:"question"` -} - -type simpleQaPromptInput struct { - Query string `json:"query"` - Context string `json:"context"` -} - const greetingWithHistoryPromptTemplate = ` {{role "user"}} Hi, my name is {{customerName}}. The time is {{currentTime}}. Who are you? @@ -143,72 +123,6 @@ func main() { return text, nil }) - simpleQaPrompt, err := dotprompt.Define("simpleQaPrompt", - simpleQaPromptTemplate, - &dotprompt.Config{ - Model: "google-genai/gemini-1.0-pro", - InputSchema: jsonschema.Reflect(simpleQaPromptInput{}), - OutputFormat: ai.OutputFormatText, - }, - ) - if err != nil { - log.Fatal(err) - } - - genkit.DefineFlow("simpleQaFlow", func(ctx context.Context, input *simpleQaInput, _ genkit.NoStream) (string, error) { - d1 := ai.DocumentFromText("Paris is the capital of France", nil) - d2 := ai.DocumentFromText("USA is the largest importer of coffee", nil) - d3 := ai.DocumentFromText("Water exists in 3 states - solid, liquid and gas", nil) - - embedder, err := googleai.NewEmbedder(ctx, "embedding-001", apiKey) - if err != nil { - return "", err - } - localDb, err := localvec.New(ctx, "/tmp/", "simpleQa", embedder, nil) - if err != nil { - return "", err - } - - indexerReq := &ai.IndexerRequest{ - Documents: []*ai.Document{d1, d2, d3}, - } - localDb.Index(ctx, indexerReq) - - dRequest := ai.DocumentFromText(input.Question, nil) - retrieverReq := &ai.RetrieverRequest{ - Document: dRequest, - } - response, err := localDb.Retrieve(ctx, retrieverReq) - if err != nil { - return "", err - } - - var context string - for _, d := range response.Documents { - context += d.Content[0].Text() + "\n" - } - - promptInput := &simpleQaPromptInput{ - Query: input.Question, - Context: context, - } - - vars, err := simpleQaPrompt.BuildVariables(promptInput) - if err != nil { - return "", err - } - ai := &dotprompt.ActionInput{Variables: vars} - resp, err := simpleQaPrompt.Execute(ctx, ai) - if err != nil { - return "", err - } - text, err := resp.Text() - if err != nil { - return "", fmt.Errorf("simpleQa: %v", err) - } - return text, nil - }) - greetingWithHistoryPrompt, err := dotprompt.Define("greetingWithHistory", greetingWithHistoryPromptTemplate, &dotprompt.Config{ Model: "google-genai/gemini-1.0-pro", From 517ccd96bd82cdf121cc9b5d0e87de18f50bf339 Mon Sep 17 00:00:00 2001 From: Samuel Bushi Date: Wed, 29 May 2024 16:33:58 +0000 Subject: [PATCH 10/10] Feedback --- go/samples/rag/main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/go/samples/rag/main.go b/go/samples/rag/main.go index cddc7c4537..d049b48d9d 100644 --- a/go/samples/rag/main.go +++ b/go/samples/rag/main.go @@ -39,6 +39,7 @@ import ( "fmt" "log" "os" + "strings" "github.com/firebase/genkit/go/ai" "github.com/firebase/genkit/go/genkit" @@ -70,7 +71,7 @@ type simpleQaPromptInput struct { func main() { apiKey := os.Getenv("GOOGLE_GENAI_API_KEY") if apiKey == "" { - fmt.Fprintln(os.Stderr, "coffee-shop example requires setting GOOGLE_GENAI_API_KEY in the environment.") + fmt.Fprintln(os.Stderr, "rag example requires setting GOOGLE_GENAI_API_KEY in the environment.") fmt.Fprintln(os.Stderr, "You can get an API key at https://ai.google.dev.") os.Exit(1) } @@ -95,7 +96,7 @@ func main() { if err != nil { log.Fatal(err) } - localDb, err := localvec.New(context.Background(), "/tmp/", "simpleQa", embedder, nil) + localDb, err := localvec.New(context.Background(), os.TempDir(), "simpleQa", embedder, nil) if err != nil { log.Fatal(err) } @@ -108,7 +109,10 @@ func main() { indexerReq := &ai.IndexerRequest{ Documents: []*ai.Document{d1, d2, d3}, } - localDb.Index(ctx, indexerReq) + err := localDb.Index(ctx, indexerReq) + if err != nil { + return "", err + } dRequest := ai.DocumentFromText(input.Question, nil) retrieverReq := &ai.RetrieverRequest{ @@ -119,14 +123,15 @@ func main() { return "", err } - var context string + var sb strings.Builder for _, d := range response.Documents { - context += d.Content[0].Text() + "\n" + sb.WriteString(d.Content[0].Text()) + sb.WriteByte('\n') } promptInput := &simpleQaPromptInput{ Query: input.Question, - Context: context, + Context: sb.String(), } vars, err := simpleQaPrompt.BuildVariables(promptInput)