Code example to match actual output

Aperta
#104 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Documentazione
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
python
Ambito
documentation

Direzione di ricerca

Inizia con python/count_tokens.py e confronta l'output atteso documentato con l'output prodotto dall'esempio attuale. Verifica se la presentazione proposta di count_tokens e usage_metadata debba essere applicata solo qui o anche ad altri esempi della documentazione. Il lavoro è completato quando l'esempio e l'output visualizzato corrispondono e l'ambito previsto è stato confermato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Go
Stelle
143
Fork
68
Merge medio
10h 30m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google-gemini/api-examples

Tutte le issue di google-gemini/api-examples

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.