-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to fix Gemini stream mode? #8998
Comments
To address the issue with the Gemini stream mode not resulting in stream output, you should focus on the
Here is a snippet of the relevant code for reference: def _handle_chat_generate_stream_response(
self,
model: str,
prompt_messages: list[PromptMessage],
credentials: dict,
response: Iterator,
) -> Generator:
for line in response:
if not line:
continue
line = line.decode("utf-8")
# remove the first `data: ` prefix
if line.startswith("data:"):
line = line[5:].strip()
try:
data = json.loads(line)
except Exception as e:
if line.strip() == "[DONE]":
return
choices = data.get("choices", [])
stop_reason = ""
for choice in choices:
if choice.get("finish_reason"):
stop_reason = choice["finish_reason"]
if len(choice["delta"]["content"]) == 0:
continue
yield LLMResultChunk(
model=model,
prompt_messages=prompt_messages,
delta=LLMResultChunkDelta(
index=0,
message=AssistantPromptMessage(content=choice["delta"]["content"], tool_calls=[]),
finish_reason=stop_reason,
),
) Ensure that the response from the model is correctly formatted and that the streaming logic is correctly implemented in the |
def _handle_invoke_result(
self, invoke_result: LLMResult | Generator
) -> Generator[RunEvent | ModelInvokeCompleted, None, None]:
"""
Handle invoke result
:param invoke_result: invoke result
:return:
"""
if isinstance(invoke_result, LLMResult):
return Hmm, okay, I found the place to handle it. They didn't consider the |
I don't quite understand these. Can you fix this issue? @Hisir0909 |
Hi @Hisir0909, I am just adding my one cent here, dify/api/core/model_runtime/model_providers/google/llm/llm.py Lines 217 to 220 in 7121afd
|
What do you mean? When |
I see, my bad. I misunderstood your previous statement |
@CXwudi 在config_kwargs中设置
···
config_kwargs.pop("stream", None)
····
Although it can solve the error reporting problem, it is still not stream output. The code _handle_generate_stream_response seems to have a problem.
Originally posted by @AAEE86 in #8678 (comment)
The text was updated successfully, but these errors were encountered: