mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-04-01 00:18:18 +02:00
Catch LLM Generation Failure (#1712)
This commit is contained in:
parent
0d814939ee
commit
20c4cdbdda
@ -5,6 +5,7 @@ from typing import Any
|
||||
from typing import cast
|
||||
|
||||
import litellm # type: ignore
|
||||
from httpx import RemoteProtocolError
|
||||
from langchain.schema.language_model import LanguageModelInput
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.messages import AIMessageChunk
|
||||
@ -338,17 +339,23 @@ class DefaultMultiLLM(LLM):
|
||||
|
||||
output = None
|
||||
response = self._completion(prompt, tools, tool_choice, True)
|
||||
for part in response:
|
||||
if len(part["choices"]) == 0:
|
||||
continue
|
||||
delta = part["choices"][0]["delta"]
|
||||
message_chunk = _convert_delta_to_message_chunk(delta, output)
|
||||
if output is None:
|
||||
output = message_chunk
|
||||
else:
|
||||
output += message_chunk
|
||||
try:
|
||||
for part in response:
|
||||
if len(part["choices"]) == 0:
|
||||
continue
|
||||
delta = part["choices"][0]["delta"]
|
||||
message_chunk = _convert_delta_to_message_chunk(delta, output)
|
||||
if output is None:
|
||||
output = message_chunk
|
||||
else:
|
||||
output += message_chunk
|
||||
|
||||
yield message_chunk
|
||||
yield message_chunk
|
||||
|
||||
except RemoteProtocolError:
|
||||
raise RuntimeError(
|
||||
"The AI model failed partway through generation, please try again."
|
||||
)
|
||||
|
||||
if LOG_ALL_MODEL_INTERACTIONS and output:
|
||||
content = output.content or ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user