T-K-O-H commited on
Commit
d2e53aa
·
1 Parent(s): 5474c75

Add chat history support

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -17,6 +17,7 @@ from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
17
  from langchain.schema import SystemMessage
18
  import logging
19
  import sys
 
20
 
21
  # LangChain
22
  from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
@@ -49,8 +50,14 @@ app = FastAPI()
49
  def search_web(query: str) -> str:
50
  """Search the web for information on a given query."""
51
  logger.info(f"Searching web for query: {query}")
52
- # In a real implementation, this would connect to a search API
53
- return f"Found information about {query}: This is a simulated web search result for '{query}'."
 
 
 
 
 
 
54
 
55
 
56
  @tool
@@ -143,18 +150,29 @@ Use these tools to assist the user with their requests. When a tool is needed, c
143
  return prompt | model.bind_tools(tools=tools)
144
 
145
 
146
- # WebSocket for real-time communication
147
  @app.websocket("/ws")
148
  async def websocket_endpoint(websocket: WebSocket):
149
  logger.info("New WebSocket connection established")
150
  await websocket.accept()
 
151
  try:
152
  while True:
153
  data = await websocket.receive_text()
154
  logger.info(f"Received message: {data}")
155
  try:
156
- # Process the message with the agent
157
- response = agent_executor.invoke({"input": data, "chat_history": []})
 
 
 
 
 
 
 
 
 
 
158
  logger.info(f"Agent response: {response['output']}")
159
  await websocket.send_json({"type": "ai_message", "content": response["output"]})
160
  except Exception as e:
 
17
  from langchain.schema import SystemMessage
18
  import logging
19
  import sys
20
+ from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
21
 
22
  # LangChain
23
  from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
 
50
  def search_web(query: str) -> str:
51
  """Search the web for information on a given query."""
52
  logger.info(f"Searching web for query: {query}")
53
+ try:
54
+ search = DuckDuckGoSearchAPIWrapper()
55
+ results = search.run(query)
56
+ logger.info(f"Search results: {results}")
57
+ return results
58
+ except Exception as e:
59
+ logger.error(f"Error in web search: {str(e)}")
60
+ return f"Error searching the web: {str(e)}"
61
 
62
 
63
  @tool
 
150
  return prompt | model.bind_tools(tools=tools)
151
 
152
 
153
+ # WebSocket endpoint for real-time communication
154
  @app.websocket("/ws")
155
  async def websocket_endpoint(websocket: WebSocket):
156
  logger.info("New WebSocket connection established")
157
  await websocket.accept()
158
+ chat_history = [] # Initialize chat history
159
  try:
160
  while True:
161
  data = await websocket.receive_text()
162
  logger.info(f"Received message: {data}")
163
  try:
164
+ # Add user message to chat history
165
+ chat_history.append(HumanMessage(content=data))
166
+
167
+ # Process the message with the agent, including chat history
168
+ response = agent_executor.invoke({
169
+ "input": data,
170
+ "chat_history": chat_history
171
+ })
172
+
173
+ # Add AI response to chat history
174
+ chat_history.append(AIMessage(content=response["output"]))
175
+
176
  logger.info(f"Agent response: {response['output']}")
177
  await websocket.send_json({"type": "ai_message", "content": response["output"]})
178
  except Exception as e: