Skip to content

Commit 4272fec

Browse files
authored
Add example of explicitly setting History on ChatSession (#76)
1 parent d205e88 commit 4272fec

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

genai/example_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,39 @@ func ExampleChatSession() {
178178
printResponse(res)
179179
}
180180

181+
// This example shows how to set the History field on ChatSession explicitly.
182+
func ExampleChatSession_history() {
183+
ctx := context.Background()
184+
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
185+
if err != nil {
186+
log.Fatal(err)
187+
}
188+
defer client.Close()
189+
model := client.GenerativeModel("gemini-1.0-pro")
190+
cs := model.StartChat()
191+
192+
cs.History = []*genai.Content{
193+
&genai.Content{
194+
Parts: []genai.Part{
195+
genai.Text("Hello, I have 2 dogs in my house."),
196+
},
197+
Role: "user",
198+
},
199+
&genai.Content{
200+
Parts: []genai.Part{
201+
genai.Text("Great to meet you. What would you like to know?"),
202+
},
203+
Role: "model",
204+
},
205+
}
206+
207+
res, err := cs.SendMessage(ctx, genai.Text("How many paws are in my house?"))
208+
if err != nil {
209+
log.Fatal(err)
210+
}
211+
printResponse(res)
212+
}
213+
181214
func ExampleEmbeddingModel_EmbedContent() {
182215
ctx := context.Background()
183216
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))

0 commit comments

Comments
 (0)