Curious Learner
Curious Learner

Reputation: 1474

How to generate Multiple Responses for single prompt with Google Gemini API?

Context

I am using google gemini-api.
Using their Python SDK

Goal

I am trying to generate multiple possible responses for a single prompt according to the docs and API-reference

Expected result - multiple response for a single prompt
Actual result - single response

Code I have tried

# ... more code above
model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest", system_instruction=system_instruction)

response = model.generate_content("What is the meaning of life?")
resps = response.candidates

If any more information is needed please ask in the comments.

Upvotes: 2

Views: 1833

Answers (1)

Tanaike
Tanaike

Reputation: 201553

When I saw the official document GenerationConfig of Method: models.generateContent, it says as follows.

candidateCount: integer Optional. Number of generated responses to return. Currently, this value can only be set to 1. If unset, this will default to 1.

It seems that in the current stage, candidateCount is only 1. This is the same with "v1". Ref

But, when I saw google.ai.generativelanguage.GenerationConfig of the official document, it says as follows.

candidate_count: int Optional. Number of generated responses to return. This value must be between [1, 8], inclusive. If unset, this will default to 1.

From this, it seems that it is possible to try to change the value of candidate_count by the script. From this document, when your script is modified, it becomes as follows.

model = genai.GenerativeModel(
    model_name="gemini-1.5-pro-latest",
    system_instruction=system_instruction,
    generation_config=glm.GenerationConfig(candidate_count=2),
)

response = model.generate_content("What is the meaning of life?")
resps = response.candidates
  • In this case, please add import google.ai.generativelanguage as glm.
  • When I tested the above script with candidate_count=2, an error like Only one candidate can be specified occurred. I guess that this value might be able to be changed in the future update. When a value of more than 1 got to be able to be used, please test the above-modified script.

References:

Upvotes: 1

Related Questions