generative-ai-go icon indicating copy to clipboard operation
generative-ai-go copied to clipboard

Potential Token Counting Issue in SDK Version [v0.19.0]

Open lldxflwb opened this issue 10 months ago • 3 comments

Description of the bug:

version is github.com/google/generative-ai-go v0.19.0

Image

Description: Hello, I encountered an issue with the current SDK version:

PromptTokenCount equals TotalTokenCount, implying there are zero tokens allocated for output. All Candidates tokens consistently show 0 in this scenario. This appears to contradict expected behavior, as successful responses should logically include output tokens. Could you confirm whether this is intentional or a potential bug?

Thank you for your attention to this matter. Let me know if additional details or testing would be helpful.

This version:v0.19.0

Actual vs expected behavior:

TotalTokenCount should be greater than PromptTokenCount. And each Candidates' TokenCount field should have a value:Candidates

Any other information you'd like to share?

No response

lldxflwb avatar Feb 18 '25 07:02 lldxflwb

The context code for the related "resp" is:


func ReadFileTest(filepath string) (string, error) {
	fmt.Println("Starting ReadFileTest with filepath:", filepath)
	model := client.GenerativeModel("gemini-1.5-flash")
	model.SetTemperature(1)
	model.SetTopK(40)
	model.SetTopP(0.95)
	model.SetMaxOutputTokens(8192)
	model.ResponseMIMEType = "text/plain"
	model.SystemInstruction = &genai.Content{
		Parts: []genai.Part{genai.Text("Extract audio to text")},
	}
	fmt.Println("Model configuration set.")
	audio, err := os.ReadFile(filepath)
	if err != nil {
		fmt.Println("Error reading file:", err)
		return "", err
	}
	fmt.Println("File read successfully. File size:", len(audio))

	fileURIs := []string{
		uploadToGemini(context.Background(), client, audio, mimetype.Detect(audio).String()),
	}
	fmt.Println("Uploaded to Gemini, URI:", fileURIs[0])

	session := model.StartChat()
	fmt.Println("Chat session started.")

	resp, err := session.SendMessage(context.Background(), genai.FileData{
		MIMEType: mimetype.Detect(audio).String(),
		URI:      fileURIs[0],
	}, genai.Text("Extract audio to text"))
	if err != nil {
		fmt.Printf("Error sending message: %v\n", err)
		return "", fmt.Errorf("Error sending message: %v", err)
	}
	fmt.Println("Message sent. Usage metadata:", resp.UsageMetadata.TotalTokenCount, resp.UsageMetadata.PromptTokenCount)

	allText := ""

	for _, part := range resp.Candidates[0].Content.Parts {
		fmt.Printf("%v\n", part)
		allText += fmt.Sprintf("%v\n", part)
	}
	fmt.Println("All text extracted.")
	return allText, nil
}

lldxflwb avatar Feb 18 '25 07:02 lldxflwb

Thanks @lldxflwb for raising this issue. To clarify, are there candidates in the response? Is this happening for all requests?

Annhiluc avatar Mar 25 '25 00:03 Annhiluc

Thanks @lldxflwb for raising this issue. To clarify, are there candidates in the response? Is this happening for all requests?

yes,output_token_count is 0 in my last 100 requests.

lldxflwb avatar Mar 27 '25 02:03 lldxflwb