-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroq.py
59 lines (51 loc) · 1.77 KB
/
groq.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
if __name__ == '__main__':
import asyncio
from groq import AsyncGroq
history = []
async def main(message):
client = AsyncGroq()
chat_completion = await client.chat.completions.create(
messages=[
{
"role": "system",
"content": "you are a helpful assistant."
},
{
"role": "user",
"content": message,
}
],
model="mixtral-8x7b-32768",
temperature=0.5,
max_tokens=1024,
top_p=1,
stream=False,
)
response_content = chat_completion.choices[0].message.content
print('<<< Jarvis: ', end="", flush=True)
print(response_content)
return response_content
def update_history(user_message, response):
if user_message:
history.append({"role": "user", "content": user_message})
if response:
history.append({"role": "assistant", "content": response})
def prepare_message_with_history():
combined_message = ""
for message in history[-10:]:
combined_message += f"{message['role']}: {message['content']}\n"
return combined_message
async def chat():
while True:
user_text = input('>>> User: ')
message_with_history = prepare_message_with_history() + user_text
response = await main(message_with_history)
update_history(user_text, response)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(chat())
except KeyboardInterrupt:
print("Chat session ended.")
finally:
loop.close()