forked from judymath/mistral-genies-hackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
34 lines (25 loc) · 908 Bytes
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
from pathlib import Path
from dotenv import load_dotenv
# Define BASEDIR for the Project
BASEDIR = Path(__file__).parents[1]
# Load environment variables locally
if os.path.isfile(os.path.join(BASEDIR, ".env")):
load_dotenv(os.path.join(BASEDIR, ".env"))
def mistral(user_message,
model,
is_json=False):
client = MistralClient(api_key=os.environ.get("MISTRAL_API_KEY", None))
messages = [ChatMessage(role="user", content=user_message)]
if is_json:
chat_response = client.chat(
model=model,
messages=messages,
response_format={"type": "json_object"})
else:
chat_response = client.chat(
model=model,
messages=messages)
return chat_response.choices[0].message.content