Google GenAI send_message_stream多轮对话后无法返回完整响应块的问题求助
Google GenAI send_message_stream多轮对话后无法返回完整响应块的问题求助
我是一名Python老师,正在做一个示例项目,想给学生展示如何在项目中运用AI,而不只是让ChatGPT帮忙写代码。
下面是我写的代码(它不是最高效的版本,但因为是给低年级孩子看的,所以效率不是重点,主要是让他们接触更进阶的Python内容):
from google import genai from google.genai import types import time # AI Connection client = genai.Client(api_key="[KEY_HERE]") # My attempt to deal with a "rate" issue (which turned out to not do anything useful) print("Please wait...") for x in range (0, 60): percent = round(x/60 * 100) print(str(percent) + "%") time.sleep(1) # Function to make calls to AI more "modular" def askAIStream(personality, question, history): # Variable to track history within this function call newHistory = "" chat = client.chats.create( model="gemini-2.0-flash", config = types.GenerateContentConfig( system_instruction=personality, ), history=history ) response = chat.send_message_stream(question) for chunk in response: newHistory += chunk.text print(chunk.text, end="") # Return the response if we need it for a later prompt return newHistory personality = "No special instructions" history = [] response = askAIStream(personality, "Give me a random 1-word response that can be used as a topic for a joke", history) history.append(response) response = askAIStream(personality, "Tell me a joke about " + response, history) history.append(response) response = askAIStream(personality, "I don't get it, can you explain this joke to me?", history) history.append(response) # Start the history variable over to keep track of what's going on history.clear() # Generate the personality of the AI personality = """You are a classic storyteller who has a way with words. You are weaving a story that you will get responses to that you will then use to continue the story. All your responses should include a couple of options that the user can choose and end with 'What do you do?' to help the story continue. You will find a way to end the story in 5 responses and then end the story with a classic 'The End'.""" # Start the story from the "askAIStream" function and attach the response to the history list response = askAIStream(personality, "Start a random story", history) history.append(response) # Start a loop for the story to continue for 5 more responses ## This is the where my responses eventually break for i in range(5): # Prompt the user to say something user = input("") # Call the "askAIStream" function and attach the response to the history list response = askAIStream(personality, user, history) history.append(response)
遇到的问题
在使用send_message_stream时,AI的响应文本会突然中断,程序直接等待用户的下一次输入(这种情况会在末尾循环的不同阶段发生,但最终一定会出现)——具体表现就是AI的句子还没说完,就停住等着用户输入了。
如果我改用send_message()就不会有这个问题,但我更喜欢stream的方式,因为它和学生们平时玩的ChatGPT聊天框一样,是流式输出的。
我排查过的方向
一开始我以为是速率限制的问题(我有Google One订阅,但没单独付费使用Gemini),所以加了60秒的等待倒计时,但现在想想这很蠢——如果真的是速率限制,应该会有明确的“速率超限”错误提示,而且send_message()也会受到影响,但实际上后者完全正常。
我知道我的代码存在一些效率问题(打算在给学生看之前整理好),但我觉得这些问题不是导致响应中断的原因。
有没有人遇到过类似的情况?能不能给我一些排查的方向或者解决方案?
提前谢谢大家了!
备注:内容来源于stack exchange,提问作者Willowvayle




