Code example to match actual output

未关闭
#104 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
冷清
技术栈
python
领域
documentation

调研方向

从 python/count_tokens.py 开始,将文档中记录的预期输出与当前示例产生的输出进行比较。检查提议的 count_tokens 和 usage_metadata 展示方式是否只应应用于此处,还是也应应用于其他文档示例。示例及其显示的输出一致,并且预期范围已确认,即视为完成。

由索引模型根据 Issue 内容生成。

描述

Description of the feature request:

Hi everyone.

Issues

On the document, the code and expected output does not much.
For example, on python/count_tokens.py explains below.

    def test_tokens_text_only(self):
        # [START tokens_text_only]
        from google import genai

        client = genai.Client()
        prompt = "The quick brown fox jumps over the lazy dog."

        # Count tokens using the new client method.
        total_tokens = client.models.count_tokens(
            model="gemini-3.5-flash", contents=prompt
        )
        print("total_tokens: ", total_tokens)
        # ( e.g., total_tokens: 10 )

        response = client.models.generate_content(
            model="gemini-3.5-flash", contents=prompt
        )

        # The usage_metadata provides detailed token counts.
        print(response.usage_metadata)
        # ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
        # [END tokens_text_only]

However,

        print("total_tokens: ", total_tokens)
        # ( e.g., total_tokens: 10 )

does not reply as ( e.g. ) says.

Question and Proposal

Are ( e.g.)s intentionally written like above on the document?
If not and if we try to nicely write the document for beginners, we should write like below code.

    def test_tokens_text_only(self):
        # [START tokens_text_only]
        from google import genai

        client = genai.Client()
        prompt = "The quick brown fox jumps over the lazy dog."

        # Count tokens using the new client method.
-       total_tokens = client.models.count_tokens(
-           model="gemini-3.5-flash", contents=prompt
-       )
-       print("total_tokens: ", total_tokens)
+       response = client.models.count_tokens(
+           model="gemini-3.5-flash", contents=prompt
+       )
+       print("total_tokens: ", response.total_tokens)

        # ( e.g., total_tokens: 10 )

        response = client.models.generate_content(
            model="gemini-3.5-flash", contents=prompt
        )

        # The usage_metadata provides detailed token counts.
-        print(response.usage_metadata)
+        metadata = response.usage_metadata
+        print(f"prompt_token_count: {metadata.prompt_token_count}, candidates_token_count: {metadata.candidates_token_count}, total_token_count: {metadata.total_token_count}")
        # ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
        # [END tokens_text_only]

My concern

If we conduct this change, we have to change many parts on the document. Thus, I need your advices.
Thank you.

What problem are you trying to solve with this feature?

No response

Any other information you'd like to share?

No response

主要语言
Go
星标
143
派生
68
平均合并
10 小时 30 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google-gemini/api-examples 的其他 Issue

查看 google-gemini/api-examples 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。