zenaight commited on
Commit ·
fe33f65
1
Parent(s): 90f252d
chat with memory removed
Browse files- ai_chat.py +8 -18
ai_chat.py
CHANGED
|
@@ -3,39 +3,29 @@ from langchain_core.runnables import RunnableLambda
|
|
| 3 |
from typing import TypedDict
|
| 4 |
from config import llm, OPENAI_API_KEY
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def chat_with_memory(state):
|
| 10 |
-
"""Chat function with memory and user context"""
|
| 11 |
user_message = state["user_message"]
|
| 12 |
user_info = state.get("user_info", {})
|
| 13 |
|
| 14 |
-
# Add user message to history
|
| 15 |
-
chat_history.append({"role": "user", "content": user_message})
|
| 16 |
-
|
| 17 |
-
# Keep only last 6 messages for context
|
| 18 |
-
recent_messages = chat_history[-6:] if len(chat_history) > 6 else chat_history
|
| 19 |
-
|
| 20 |
# Add system message with user context
|
| 21 |
system_message = "You are a helpful and concise assistant."
|
| 22 |
if user_info.get("name") and user_info["name"] != "Unknown":
|
| 23 |
system_message += f" The user's name is {user_info['name']}."
|
| 24 |
|
| 25 |
-
messages = [
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
try:
|
| 28 |
if not OPENAI_API_KEY:
|
| 29 |
return {"response": "Sorry, AI chat is not available. Please check your OpenAI API key configuration."}
|
| 30 |
|
| 31 |
response = llm.invoke(messages)
|
| 32 |
-
|
| 33 |
-
# Add AI response to history
|
| 34 |
-
chat_history.append({"role": "assistant", "content": response.content})
|
| 35 |
-
|
| 36 |
return {"response": response.content}
|
| 37 |
except Exception as e:
|
| 38 |
-
print(f"Error in
|
| 39 |
return {"response": "Sorry, something went wrong: " + str(e)}
|
| 40 |
|
| 41 |
class ChatState(TypedDict):
|
|
@@ -45,7 +35,7 @@ class ChatState(TypedDict):
|
|
| 45 |
|
| 46 |
# --- Build LangGraph ---
|
| 47 |
graph = StateGraph(ChatState)
|
| 48 |
-
graph.add_node("chat", RunnableLambda(
|
| 49 |
graph.set_entry_point("chat")
|
| 50 |
graph.add_edge("chat", END)
|
| 51 |
chat_graph = graph.compile()
|
|
|
|
| 3 |
from typing import TypedDict
|
| 4 |
from config import llm, OPENAI_API_KEY
|
| 5 |
|
| 6 |
+
def chat_without_memory(state):
|
| 7 |
+
"""Chat function without memory - stateless responses"""
|
|
|
|
|
|
|
|
|
|
| 8 |
user_message = state["user_message"]
|
| 9 |
user_info = state.get("user_info", {})
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Add system message with user context
|
| 12 |
system_message = "You are a helpful and concise assistant."
|
| 13 |
if user_info.get("name") and user_info["name"] != "Unknown":
|
| 14 |
system_message += f" The user's name is {user_info['name']}."
|
| 15 |
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content": system_message},
|
| 18 |
+
{"role": "user", "content": user_message}
|
| 19 |
+
]
|
| 20 |
|
| 21 |
try:
|
| 22 |
if not OPENAI_API_KEY:
|
| 23 |
return {"response": "Sorry, AI chat is not available. Please check your OpenAI API key configuration."}
|
| 24 |
|
| 25 |
response = llm.invoke(messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return {"response": response.content}
|
| 27 |
except Exception as e:
|
| 28 |
+
print(f"Error in chat_without_memory: {e}")
|
| 29 |
return {"response": "Sorry, something went wrong: " + str(e)}
|
| 30 |
|
| 31 |
class ChatState(TypedDict):
|
|
|
|
| 35 |
|
| 36 |
# --- Build LangGraph ---
|
| 37 |
graph = StateGraph(ChatState)
|
| 38 |
+
graph.add_node("chat", RunnableLambda(chat_without_memory))
|
| 39 |
graph.set_entry_point("chat")
|
| 40 |
graph.add_edge("chat", END)
|
| 41 |
chat_graph = graph.compile()
|