diff --git a/README.md b/README.md index 3a8f107..592d69b 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ poetry run python chat_no_streaming.py ### Using poetry shell ```bash -cd examples poetry shell +cd examples >> python chat_no_streaming.py ``` diff --git a/examples/async_chat_no_streaming.py b/examples/async_chat_no_streaming.py index 743e8fa..9c8be87 100644 --- a/examples/async_chat_no_streaming.py +++ b/examples/async_chat_no_streaming.py @@ -15,7 +15,10 @@ async def main(): model=model, messages=[ChatMessage(role="user", content="What is the best French cheese?")], ) - print(chat_response) + + print(chat_response.choices[0].message.content) + + await client.close() if __name__ == "__main__": diff --git a/examples/async_chat_with_streaming.py b/examples/async_chat_with_streaming.py index e67bc6f..e239e03 100644 --- a/examples/async_chat_with_streaming.py +++ b/examples/async_chat_with_streaming.py @@ -11,11 +11,17 @@ async def main(): client = MistralAsyncClient(api_key=api_key) + print("Chat response:") async for chunk in client.chat_stream( model=model, messages=[ChatMessage(role="user", content="What is the best French cheese?")], ): - print(chunk) + if chunk.choices[0].delta.content is not None: + print(chunk.choices[0].delta.content, end="") + + print("\n") + + await client.close() if __name__ == "__main__": diff --git a/examples/chat_no_streaming.py b/examples/chat_no_streaming.py index 8d17cb0..e385773 100644 --- a/examples/chat_no_streaming.py +++ b/examples/chat_no_streaming.py @@ -14,7 +14,7 @@ def main(): model=model, messages=[ChatMessage(role="user", content="What is the best French cheese?")], ) - print(chat_response) + print(chat_response.choices[0].message.content) if __name__ == "__main__": diff --git a/examples/chat_with_streaming.py b/examples/chat_with_streaming.py index 1152161..21a12c1 100644 --- a/examples/chat_with_streaming.py +++ b/examples/chat_with_streaming.py @@ -14,7 +14,8 @@ def main(): model=model, messages=[ChatMessage(role="user", content="What is the best French cheese?")], ): - print(chunk) + if chunk.choices[0].delta.content is not None: + print(chunk.choices[0].delta.content, end="") if __name__ == "__main__": diff --git a/src/mistralai/async_client.py b/src/mistralai/async_client.py index 4ceeb4b..5cd4a56 100644 --- a/src/mistralai/async_client.py +++ b/src/mistralai/async_client.py @@ -174,6 +174,9 @@ def __init__( timeout=timeout, ) + async def close(self) -> None: + await self._backend.close() + async def _request( self, method: str, @@ -266,7 +269,6 @@ async def chat_stream( async with async_response as response: async for line in response.content: - self._logger.debug(f"Received line: {line.decode()}") if line == b"\n": continue diff --git a/src/mistralai/client.py b/src/mistralai/client.py index caae137..74a2e5c 100644 --- a/src/mistralai/client.py +++ b/src/mistralai/client.py @@ -1,7 +1,8 @@ +import os import posixpath from json import JSONDecodeError from typing import Any, Dict, Iterable, List, Optional, Union -import os + import orjson import requests from requests import Response diff --git a/src/mistralai/client_base.py b/src/mistralai/client_base.py index ede14d8..0568355 100644 --- a/src/mistralai/client_base.py +++ b/src/mistralai/client_base.py @@ -35,7 +35,7 @@ def _make_chat_request( request_data: Dict[str, Any] = { "model": model, "messages": [msg.model_dump() for msg in messages], - "safe_mode": safe_mode, + "safe_prompt": safe_mode, } if temperature is not None: request_data["temperature"] = temperature diff --git a/src/mistralai/constants.py b/src/mistralai/constants.py index 8816371..b274a4c 100644 --- a/src/mistralai/constants.py +++ b/src/mistralai/constants.py @@ -2,4 +2,4 @@ RETRY_STATUS_CODES = {429, 500, 502, 503, 504} -ENDPOINT = "http://api.mistral.ai" +ENDPOINT = "https://api.mistral.ai"