-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add count token samples #3280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdomingr
wants to merge
1
commit into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-counttoken-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
genai/api/GenAI.Samples.Tests/CountTokens/CountTokenComputeWithTxtTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using Google.GenAI.Types; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class CountTokenComputeWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly CountTokenComputeWithTxt _sample; | ||
|
|
||
| public CountTokenComputeWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new CountTokenComputeWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestCountTokenComputeWithTxt() | ||
| { | ||
| List<TokensInfo> tokensInfos = await _sample.ComputeTokens(_fixture.ProjectId); | ||
| Assert.NotEmpty(tokensInfos); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
genai/api/GenAI.Samples.Tests/CountTokens/CountTokenRespWithTxtTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using Google.GenAI.Types; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class CountTokenRespWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly CountTokenRespWithTxt _sample; | ||
|
|
||
| public CountTokenRespWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new CountTokenRespWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestCountTokenRespWithTxt() | ||
| { | ||
| GenerateContentResponseUsageMetadata usageMetadata = await _sample.CountTokens(_fixture.ProjectId); | ||
| Assert.NotNull(usageMetadata); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/CountTokens/CountTokenWithTxtTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class CountTokenWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly CountTokenWithTxt _sample; | ||
|
|
||
| public CountTokenWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new CountTokenWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestCountTokenWithTxt() | ||
| { | ||
| int totalTokens = await _sample.CountTokens(_fixture.ProjectId); | ||
| Assert.True(totalTokens > 0); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/CountTokens/CountTokenWithTxtVidTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class CountTokenWithTxtVidTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly CountTokenWithTxtVid _sample; | ||
|
|
||
| public CountTokenWithTxtVidTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new CountTokenWithTxtVid(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestCountTokenWithTxtVid() | ||
| { | ||
| int totalTokens = await _sample.CountTokens(_fixture.ProjectId); | ||
| Assert.True(totalTokens > 0); | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
genai/api/GenAI.Samples/CountTokens/CountTokenComputeWithTxt.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_counttoken_compute_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class CountTokenComputeWithTxt | ||
| { | ||
| public async Task<List<TokensInfo>> ComputeTokens( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| ComputeTokensResponse response = await client.Models.ComputeTokensAsync( | ||
| model: model, | ||
| contents: "What's the longest word in the English language?"); | ||
|
|
||
| List<TokensInfo> tokensInfo = response.TokensInfo ?? new List<TokensInfo>(); | ||
|
|
||
| foreach (TokensInfo tokenInfo in tokensInfo) | ||
| { | ||
| Console.WriteLine($"Role: {tokenInfo.Role}"); | ||
| Console.WriteLine($"Token Ids: {string.Join(", ", tokenInfo.TokenIds)}"); | ||
|
|
||
| Console.WriteLine("Tokens:"); | ||
| foreach (byte[] token in tokenInfo.Tokens) | ||
| { | ||
| Console.WriteLine(Encoding.UTF8.GetString(token)); | ||
| } | ||
| } | ||
| // Example output: | ||
| // Role: user | ||
| // Token Ids: 3689, 236789, 236751, 506, 27801, 3658, 528, 506, 5422, 5192, 236881 | ||
| // Tokens: | ||
| // What | ||
| // ' | ||
| // s | ||
| // the | ||
| // longest | ||
| // ... | ||
| // ? | ||
| return tokensInfo; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_counttoken_compute_with_txt] |
53 changes: 53 additions & 0 deletions
53
genai/api/GenAI.Samples/CountTokens/CountTokenRespWithTxt.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_counttoken_resp_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class CountTokenRespWithTxt | ||
| { | ||
| public async Task<GenerateContentResponseUsageMetadata> CountTokens( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "Why is the sky blue?"); | ||
|
|
||
| GenerateContentResponseUsageMetadata usageMetadata = response.UsageMetadata; | ||
| Console.WriteLine(usageMetadata); | ||
| // Example response: | ||
| // GenerateContentResponseUsageMetadata { CacheTokensDetails = , | ||
| // CachedContentTokenCount = , CandidatesTokenCount = 589, | ||
| // ... | ||
| // ThoughtsTokenCount = 945, ToolUsePromptTokenCount = , | ||
| // ToolUsePromptTokensDetails = , TotalTokenCount = 1540, | ||
| // TrafficType = ON_DEMAND } | ||
| return usageMetadata; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_counttoken_resp_with_txt] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright 2025 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_counttoken_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class CountTokenWithTxt | ||
| { | ||
| public async Task<int> CountTokens( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| CountTokensResponse response = await client.Models.CountTokensAsync( | ||
| model: model, | ||
| contents: "What's the highest mountain in Africa?"); | ||
|
|
||
| int totalTokens = response.TotalTokens ?? 0; | ||
| Console.WriteLine($"Total tokens: {totalTokens}"); | ||
| // Example response: | ||
| // Total tokens: 9 | ||
| return totalTokens; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_counttoken_with_txt] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.