zenaight commited on
Commit
2d92890
·
1 Parent(s): 618e187

bug fixes

Browse files
Files changed (1) hide show
  1. main.py +27 -11
main.py CHANGED
@@ -7,7 +7,9 @@ from pydantic import BaseModel
7
  from typing import Optional
8
  from langgraph.graph import StateGraph, END
9
  from langchain_core.runnables import RunnableLambda
10
- from langchain.memory import ConversationBufferMemory
 
 
11
  from langchain_openai import ChatOpenAI
12
 
13
  # --- App and Config ---
@@ -19,7 +21,9 @@ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
19
 
20
  # --- OpenAI Client Setup ---
21
  llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-4o-mini")
22
- memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
 
 
23
 
24
  # Initialize LLM only if API key is available
25
  if not OPENAI_API_KEY:
@@ -28,29 +32,41 @@ if not OPENAI_API_KEY:
28
  # --- LangGraph Setup ---
29
  def chat_with_memory(state):
30
  user_message = state["user_message"]
31
- memory.chat_memory.add_user_message(user_message)
32
-
33
- messages = memory.chat_memory.messages[-6:] # Use last few messages
34
- messages = [{"role": msg.type, "content": msg.content} for msg in messages]
35
-
36
- messages.insert(0, {"role": "system", "content": "You are a helpful and concise assistant."})
 
 
 
37
 
38
  try:
39
  if not OPENAI_API_KEY:
40
  return {"response": "Sorry, AI chat is not available. Please check your OpenAI API key configuration."}
41
 
42
  response = llm.invoke(messages)
43
- memory.chat_memory.add_ai_message(response.content)
 
 
 
44
  return {"response": response.content}
45
  except Exception as e:
46
  print(f"Error in chat_with_memory: {e}")
47
  return {"response": "Sorry, something went wrong: " + str(e)}
48
 
49
  # --- Build LangGraph ---
50
- graph = StateGraph()
 
 
 
 
 
 
51
  graph.add_node("chat", RunnableLambda(chat_with_memory))
52
  graph.set_entry_point("chat")
53
- graph.set_finish_point("chat")
54
  chat_graph = graph.compile()
55
 
56
  # --- Webhook Verification ---
 
7
  from typing import Optional
8
  from langgraph.graph import StateGraph, END
9
  from langchain_core.runnables import RunnableLambda
10
+ from langchain_core.memory import BaseMemory
11
+ from langchain_core.chat_history import BaseChatMessageHistory
12
+ from langchain_core.messages import HumanMessage, AIMessage
13
  from langchain_openai import ChatOpenAI
14
 
15
  # --- App and Config ---
 
21
 
22
  # --- OpenAI Client Setup ---
23
  llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-4o-mini")
24
+
25
+ # Simple in-memory chat history (no deprecation warnings)
26
+ chat_history = []
27
 
28
  # Initialize LLM only if API key is available
29
  if not OPENAI_API_KEY:
 
32
  # --- LangGraph Setup ---
33
  def chat_with_memory(state):
34
  user_message = state["user_message"]
35
+
36
+ # Add user message to history
37
+ chat_history.append({"role": "user", "content": user_message})
38
+
39
+ # Keep only last 6 messages for context
40
+ recent_messages = chat_history[-6:] if len(chat_history) > 6 else chat_history
41
+
42
+ # Add system message at the beginning
43
+ messages = [{"role": "system", "content": "You are a helpful and concise assistant."}] + recent_messages
44
 
45
  try:
46
  if not OPENAI_API_KEY:
47
  return {"response": "Sorry, AI chat is not available. Please check your OpenAI API key configuration."}
48
 
49
  response = llm.invoke(messages)
50
+
51
+ # Add AI response to history
52
+ chat_history.append({"role": "assistant", "content": response.content})
53
+
54
  return {"response": response.content}
55
  except Exception as e:
56
  print(f"Error in chat_with_memory: {e}")
57
  return {"response": "Sorry, something went wrong: " + str(e)}
58
 
59
  # --- Build LangGraph ---
60
+ from typing import TypedDict
61
+
62
+ class ChatState(TypedDict):
63
+ user_message: str
64
+ response: str
65
+
66
+ graph = StateGraph(ChatState)
67
  graph.add_node("chat", RunnableLambda(chat_with_memory))
68
  graph.set_entry_point("chat")
69
+ graph.add_edge("chat", END)
70
  chat_graph = graph.compile()
71
 
72
  # --- Webhook Verification ---