如何使用 Google Gemini API 为单个提示生成多个响应?

问题描述 投票:0回答:1

背景

我正在使用谷歌gemini-api。
使用他们的 Python SDK

目标

我正在尝试根据 docsAPI-reference

为单个提示生成多个可能的响应

预期结果 - 单个提示的多个响应
实际结果 - 单一响应

我试过的代码

# ... 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
  • resps
    list
    ,应包含超过 1 个响应。但里面只有 1 个响应。
  • 这里使用的提示符是演示提示符。但对于任何输入字符串,结果都是相同的。

如果需要更多信息,请在评论中询问。

python large-language-model google-gemini
1个回答
0
投票

当我看到Method: models.generateContent的官方文档GenerationConfig时,是这样说的。

candidateCount:整数 选修的。生成的要返回的响应数。 目前,该值只能设置为 1。如果未设置,则默认为 1。

看来在现阶段,

candidateCount
只有1。这和“v1”是一样的。 参考

但是,当我看到官方文档的google.ai.generativelanguage.GenerationConfig时,是这样写的。

candidate_count:int 选修的。生成的要返回的响应数。 该值必须介于 [1, 8] 之间(含)。如果未设置,则默认为 1。

由此看来,可以尝试通过脚本来改变

candidate_count
的值。从这个文档来看,当你的脚本修改后,就变成了如下。

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
  • 在这种情况下,请添加
    import google.ai.generativelanguage as glm
  • 当我用
    candidate_count=2
    测试上面的脚本时,出现了类似
    Only one candidate can be specified
    的错误。我想这个值可能会在未来的更新中改变。当大于1的值可以使用时,请测试上面修改的脚本。

参考资料:

© www.soinside.com 2019 - 2024. All rights reserved.