Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -302,6 +302,110 @@ def bot(history, choice, tts_choice, retrieval_mode):
|
|
302 |
|
303 |
history.append([response, None]) # Ensure the response is added in the correct format
|
304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
|
306 |
|
307 |
def add_message(history, message):
|
|
|
302 |
|
303 |
history.append([response, None]) # Ensure the response is added in the correct format
|
304 |
|
305 |
+
# Langchain imports
|
306 |
+
from langchain.agents import tool
|
307 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
308 |
+
from langchain_core.messages import AIMessage, HumanMessage
|
309 |
+
from langchain_core.tools import OpenAIToolsAgentOutputParser
|
310 |
+
from langchain_core.tools import format_to_openai_tool_messages
|
311 |
+
from langchain import OpenAI
|
312 |
+
|
313 |
+
|
314 |
+
|
315 |
+
# Step 1: Define the restaurant tool
|
316 |
+
@tool
|
317 |
+
def fetch_restaurant_info(query: str) -> str:
|
318 |
+
"""
|
319 |
+
Fetches restaurant-related information from SERP API based on the given query.
|
320 |
+
"""
|
321 |
+
from serpapi.google_search import GoogleSearch
|
322 |
+
|
323 |
+
# Define parameters for SERP API
|
324 |
+
params = {
|
325 |
+
"engine": "yelp",
|
326 |
+
"find_desc": query,
|
327 |
+
"find_loc": "Birmingham, AL, USA",
|
328 |
+
"api_key": os.getenv("SERP_API")
|
329 |
+
}
|
330 |
+
|
331 |
+
# Fetch data from SERP API
|
332 |
+
search = GoogleSearch(params)
|
333 |
+
results = search.get_dict()
|
334 |
+
organic_results = results.get("organic_results", [])
|
335 |
+
|
336 |
+
# Prepare the output in plain text
|
337 |
+
if organic_results:
|
338 |
+
response = ""
|
339 |
+
for result in organic_results:
|
340 |
+
name = result.get("title", "No name")
|
341 |
+
rating = result.get("rating", "No rating")
|
342 |
+
reviews = result.get("reviews", "No reviews")
|
343 |
+
phone = result.get("phone", "Not Available")
|
344 |
+
snippet = result.get("snippet", "Not Available")
|
345 |
+
services = result.get("service_options", "Not Known")
|
346 |
+
|
347 |
+
if isinstance(services, list):
|
348 |
+
services = ", ".join(services)
|
349 |
+
elif isinstance(services, dict):
|
350 |
+
services = ", ".join([f"{key}: {value}" for key, value in services.items()])
|
351 |
+
|
352 |
+
link = result.get("link", "#")
|
353 |
+
response += f"Name: {name}\nRating: {rating}\nReviews: {reviews}\nPhone: {phone}\nSnippet: {snippet}\nServices: {services}\nLink: {link}\n\n"
|
354 |
+
|
355 |
+
return response
|
356 |
+
else:
|
357 |
+
return "No restaurant information found."
|
358 |
+
|
359 |
+
# Step 2: Integrate the tool with the agent
|
360 |
+
tools = [fetch_restaurant_info]
|
361 |
+
|
362 |
+
# Define the prompt template
|
363 |
+
MEMORY_KEY = "chat_history"
|
364 |
+
prompt = ChatPromptTemplate.from_messages(
|
365 |
+
[
|
366 |
+
(
|
367 |
+
"system",
|
368 |
+
"You are a very powerful assistant, but you only respond with restaurant information when asked about restaurants.",
|
369 |
+
),
|
370 |
+
MessagesPlaceholder(variable_name=MEMORY_KEY),
|
371 |
+
("user", "{input}"),
|
372 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
373 |
+
]
|
374 |
+
)
|
375 |
+
|
376 |
+
# Define the chat history
|
377 |
+
chat_history = []
|
378 |
+
|
379 |
+
# Create the agent with the tool
|
380 |
+
llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), temperature=0, model="gpt-4o")
|
381 |
+
agent = (
|
382 |
+
{
|
383 |
+
"input": lambda x: x["input"],
|
384 |
+
"agent_scratchpad": lambda x: format_to_openai_tool_messages(x["intermediate_steps"]),
|
385 |
+
"chat_history": lambda x: x["chat_history"],
|
386 |
+
}
|
387 |
+
| prompt
|
388 |
+
| llm
|
389 |
+
| OpenAIToolsAgentOutputParser(tools=tools)
|
390 |
+
)
|
391 |
+
|
392 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
393 |
+
|
394 |
+
# Step 3: Update the chatbot function
|
395 |
+
def chatbot_response(user_input, history, choice, tts_choice, retrieval_mode):
|
396 |
+
# Check if the user input is related to restaurants
|
397 |
+
if "restaurant" in user_input.lower():
|
398 |
+
result = agent_executor.invoke({"input": user_input, "chat_history": history})
|
399 |
+
history.append([user_input, result["output"]])
|
400 |
+
return history, None
|
401 |
+
else:
|
402 |
+
# Use the existing logic for non-restaurant-related queries
|
403 |
+
response, addresses = generate_answer(user_input, choice, retrieval_mode)
|
404 |
+
history.append([user_input, response])
|
405 |
+
return history, None
|
406 |
+
|
407 |
+
|
408 |
+
|
409 |
|
410 |
|
411 |
def add_message(history, message):
|