-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_backend.py
46 lines (33 loc) · 1.44 KB
/
simple_backend.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
import os
from dotenv import load_dotenv
from together import Together
load_dotenv()
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
SYSTEM_PROMPT = os.getenv("SYSTEM_PROMPT", "")
MODEL_NAME = "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo"
client = Together(api_key=TOGETHER_API_KEY)
# Maintain a conversation history in memory
conversation_history = [{"role": "system", "content": SYSTEM_PROMPT}]
def chat_with_llm(user_message: str):
# Add the user message to the conversation history
conversation_history.append({"role": "user", "content": user_message})
# Generate a response based on the conversation history
response = client.chat.completions.create(
model=MODEL_NAME,
messages=conversation_history,
)
# Get the AI's response and add it to the conversation history
ai_message = response.choices[0].message.content
conversation_history.append({"role": "assistant", "content": ai_message})
return ai_message
if __name__ == "__main__":
print("Welcome to the conversation with Rabbi Snow! Type 'exit' to quit.")
while True:
print("This is the beginning of your conversation with Rabbi Snow. Type 'exit' to end the conversation.")
user_input = input("\nYou: ")
if user_input.lower() == "exit":
print("Rabbi Snow out. Goodbye")
break
print("Rabbi Snow:")
response = chat_with_llm(user_input)
print(response)