Skip to content

Commit

Permalink
genai: add sample for tokens_tools
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Jul 19, 2024
1 parent 7f8e8fe commit b9197be
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
47 changes: 41 additions & 6 deletions genai/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ func ExampleGenerativeModel_GenerateContentStream() {
}
defer client.Close()

// START [text_gen_text_only_prompt_streaming]
model := client.GenerativeModel("gemini-1.5-flash")
iter := model.GenerateContentStream(ctx, genai.Text("Write a story about a magic backpack."))
for {
Expand All @@ -316,7 +315,7 @@ func ExampleGenerativeModel_GenerateContentStream() {
}
printResponse(resp)
}
// END [text_gen_text_only_prompt_streaming]

}

func ExampleGenerativeModel_GenerateContentStream_imagePrompt() {
Expand All @@ -327,7 +326,6 @@ func ExampleGenerativeModel_GenerateContentStream_imagePrompt() {
}
defer client.Close()

// START [text_gen_multimodal_one_image_prompt_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

imgData, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
Expand All @@ -347,7 +345,7 @@ func ExampleGenerativeModel_GenerateContentStream_imagePrompt() {
}
printResponse(resp)
}
// END [text_gen_multimodal_one_image_prompt_streaming]

}

func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
Expand All @@ -358,7 +356,6 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
}
defer client.Close()

// START [text_gen_multimodal_video_prompt_streaming]
model := client.GenerativeModel("gemini-1.5-flash")

file, err := uploadFile(ctx, client, filepath.Join(testDataDir, "earth.mp4"), "")
Expand All @@ -380,7 +377,7 @@ func ExampleGenerativeModel_GenerateContentStream_videoPrompt() {
}
printResponse(resp)
}
// END [text_gen_multimodal_video_prompt_streaming]

}

func ExampleGenerativeModel_CountTokens_contextWindow() {
Expand Down Expand Up @@ -436,6 +433,44 @@ func ExampleGenerativeModel_CountTokens_textOnly() {

}

func ExampleGenerativeModel_CountTokens_tools() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()

model := client.GenerativeModel("gemini-1.5-flash-001")
prompt := "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"

tokResp, err := model.CountTokens(ctx, genai.Text(prompt))
if err != nil {
log.Fatal(err)
}

fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 23 )

tools := []*genai.Tool{
&genai.Tool{FunctionDeclarations: []*genai.FunctionDeclaration{
{Name: "add"},
{Name: "subtract"},
{Name: "multiply"},
{Name: "divide"},
}}}

model.Tools = tools
tokResp, err = model.CountTokens(ctx, genai.Text(prompt))
if err != nil {
log.Fatal(err)
}

fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 99 )

}

func ExampleGenerativeModel_CountTokens_cachedContent() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down
40 changes: 40 additions & 0 deletions genai/internal/samples/docs-snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,46 @@ func ExampleGenerativeModel_CountTokens_textOnly() {
// [END tokens_text_only]
}

func ExampleGenerativeModel_CountTokens_tools() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()

// [START tokens_tools]
model := client.GenerativeModel("gemini-1.5-flash-001")
prompt := "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"

tokResp, err := model.CountTokens(ctx, genai.Text(prompt))
if err != nil {
log.Fatal(err)
}

fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 23 )

tools := []*genai.Tool{
&genai.Tool{FunctionDeclarations: []*genai.FunctionDeclaration{
{Name: "add"},
{Name: "subtract"},
{Name: "multiply"},
{Name: "divide"},
}}}

model.Tools = tools
tokResp, err = model.CountTokens(ctx, genai.Text(prompt))
if err != nil {
log.Fatal(err)
}

fmt.Println("total_tokens:", tokResp.TotalTokens)
// ( total_tokens: 99 )

// [END tokens_tools]
}

func ExampleGenerativeModel_CountTokens_cachedContent() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
Expand Down

0 comments on commit b9197be

Please sign in to comment.