Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +0,0 @@
|
|
1 |
-
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
2 |
-
from langchain_openai import ChatOpenAI
|
3 |
-
|
4 |
-
|
5 |
-
model = ChatOpenAI(api_key='sk-proj-U4WDruCnBYaUFJHaStvAT3BlbkFJ8buf9OOPTbFmmuGomSqN')
|
6 |
-
|
7 |
-
messages = [
|
8 |
-
SystemMessage(content="Solve the following math problems"),
|
9 |
-
HumanMessage(content="What is 81 divided by 9?"),
|
10 |
-
]
|
11 |
-
|
12 |
-
# Invoke the model with messages
|
13 |
-
result = model.invoke(messages)
|
14 |
-
print(f"Answer from AI: {result.content}")
|
15 |
-
|
16 |
-
|
17 |
-
chat_history = [] # Use a list to store messages
|
18 |
-
|
19 |
-
# Set an initial system message (optional)
|
20 |
-
system_message = SystemMessage(content="You are a helpful AI assistant.")
|
21 |
-
chat_history.append(system_message) # Add system message to chat history
|
22 |
-
|
23 |
-
# Chat loop
|
24 |
-
while True:
|
25 |
-
query = input("You: ")
|
26 |
-
if query.lower() == "exit":
|
27 |
-
break
|
28 |
-
chat_history.append(HumanMessage(content=query)) # Add user message
|
29 |
-
|
30 |
-
# Get AI response using history
|
31 |
-
result = model.invoke(chat_history)
|
32 |
-
response = result.content
|
33 |
-
chat_history.append(AIMessage(content=response)) # Add AI message
|
34 |
-
|
35 |
-
print(f"AI: {response}")
|
36 |
-
|
37 |
-
|
38 |
-
print("---- Message History ----")
|
39 |
-
print(chat_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|