mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-16 17:53:37 +00:00
mistralai[patch]: Surface http errors (#20555)
Do not swallow errors when streaming with httpx. Update affected code if this PR gets merged to httpx: https://github.com/florimondmanca/httpx-sse/pull/25/files
This commit is contained in:
parent
3f156e0ece
commit
f257909699
@ -116,11 +116,38 @@ def _convert_mistral_chat_message_to_message(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _raise_on_error(response: httpx.Response) -> None:
|
||||||
|
"""Raise an error if the response is an error."""
|
||||||
|
if httpx.codes.is_error(response.status_code):
|
||||||
|
error_message = response.read().decode("utf-8")
|
||||||
|
raise httpx.HTTPStatusError(
|
||||||
|
f"Error response {response.status_code} "
|
||||||
|
f"while fetching {response.url}: {error_message}",
|
||||||
|
request=response.request,
|
||||||
|
response=response,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def _araise_on_error(response: httpx.Response) -> None:
|
||||||
|
"""Raise an error if the response is an error."""
|
||||||
|
if httpx.codes.is_error(response.status_code):
|
||||||
|
error_message = (await response.aread()).decode("utf-8")
|
||||||
|
raise httpx.HTTPStatusError(
|
||||||
|
f"Error response {response.status_code} "
|
||||||
|
f"while fetching {response.url}: {error_message}",
|
||||||
|
request=response.request,
|
||||||
|
response=response,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _aiter_sse(
|
async def _aiter_sse(
|
||||||
event_source_mgr: AsyncContextManager[EventSource],
|
event_source_mgr: AsyncContextManager[EventSource],
|
||||||
) -> AsyncIterator[Dict]:
|
) -> AsyncIterator[Dict]:
|
||||||
"""Iterate over the server-sent events."""
|
"""Iterate over the server-sent events."""
|
||||||
async with event_source_mgr as event_source:
|
async with event_source_mgr as event_source:
|
||||||
|
# TODO(Team): Remove after this is fixed in httpx dependency
|
||||||
|
# https://github.com/florimondmanca/httpx-sse/pull/25/files
|
||||||
|
await _araise_on_error(event_source._response)
|
||||||
async for event in event_source.aiter_sse():
|
async for event in event_source.aiter_sse():
|
||||||
if event.data == "[DONE]":
|
if event.data == "[DONE]":
|
||||||
return
|
return
|
||||||
@ -144,10 +171,10 @@ async def acompletion_with_retry(
|
|||||||
event_source = aconnect_sse(
|
event_source = aconnect_sse(
|
||||||
llm.async_client, "POST", "/chat/completions", json=kwargs
|
llm.async_client, "POST", "/chat/completions", json=kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
return _aiter_sse(event_source)
|
return _aiter_sse(event_source)
|
||||||
else:
|
else:
|
||||||
response = await llm.async_client.post(url="/chat/completions", json=kwargs)
|
response = await llm.async_client.post(url="/chat/completions", json=kwargs)
|
||||||
|
await _araise_on_error(response)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
return await _completion_with_retry(**kwargs)
|
return await _completion_with_retry(**kwargs)
|
||||||
@ -298,6 +325,9 @@ class ChatMistralAI(BaseChatModel):
|
|||||||
with connect_sse(
|
with connect_sse(
|
||||||
self.client, "POST", "/chat/completions", json=kwargs
|
self.client, "POST", "/chat/completions", json=kwargs
|
||||||
) as event_source:
|
) as event_source:
|
||||||
|
# TODO(Team): Remove after this is fixed in httpx dependency
|
||||||
|
# https://github.com/florimondmanca/httpx-sse/pull/25/files
|
||||||
|
_raise_on_error(event_source._response)
|
||||||
for event in event_source.iter_sse():
|
for event in event_source.iter_sse():
|
||||||
if event.data == "[DONE]":
|
if event.data == "[DONE]":
|
||||||
return
|
return
|
||||||
@ -305,7 +335,9 @@ class ChatMistralAI(BaseChatModel):
|
|||||||
|
|
||||||
return iter_sse()
|
return iter_sse()
|
||||||
else:
|
else:
|
||||||
return self.client.post(url="/chat/completions", json=kwargs).json()
|
response = self.client.post(url="/chat/completions", json=kwargs)
|
||||||
|
_raise_on_error(response)
|
||||||
|
return response.json()
|
||||||
|
|
||||||
rtn = _completion_with_retry(**kwargs)
|
rtn = _completion_with_retry(**kwargs)
|
||||||
return rtn
|
return rtn
|
||||||
|
Loading…
Reference in New Issue
Block a user