Skip to content

Commit

Permalink
cleaning some examples a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bam4d committed Dec 9, 2023
1 parent dfc3827 commit b021a56
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
5 changes: 4 additions & 1 deletion examples/async_chat_no_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
8 changes: 7 additions & 1 deletion examples/async_chat_with_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion examples/chat_no_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
3 changes: 2 additions & 1 deletion examples/chat_with_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
4 changes: 3 additions & 1 deletion src/mistralai/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ def __init__(
timeout=timeout,
)

async def close(self) -> None:
await self._backend.close()

async def _request(
self,
method: str,
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/mistralai/client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mistralai/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/mistralai/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

RETRY_STATUS_CODES = {429, 500, 502, 503, 504}

ENDPOINT = "http://api.mistral.ai"
ENDPOINT = "https://api.mistral.ai"

0 comments on commit b021a56

Please sign in to comment.