|
28 | 28 | import json |
29 | 29 | import logging |
30 | 30 | import os |
31 | | -import textwrap |
32 | 31 |
|
33 | 32 | from agent_memory_client import ( |
34 | 33 | MemoryAPIClient, |
|
37 | 36 | from agent_memory_client.models import ( |
38 | 37 | WorkingMemory, |
39 | 38 | ) |
40 | | -from langchain_community.tools.tavily_search import TavilySearchResults |
41 | 39 | from langchain_core.callbacks.manager import CallbackManagerForToolRun |
42 | 40 | from langchain_openai import ChatOpenAI |
43 | 41 | from redis import Redis |
44 | 42 |
|
45 | 43 |
|
| 44 | +try: |
| 45 | + from langchain_community.tools.tavily_search import TavilySearchResults |
| 46 | +except ImportError as e: |
| 47 | + raise ImportError("Please install langchain-community for this demo.") from e |
| 48 | + |
| 49 | + |
46 | 50 | # Configure logging |
47 | | -logging.basicConfig(level=logging.INFO) |
| 51 | +logging.basicConfig(level=logging.WARNING) |
48 | 52 | logger = logging.getLogger(__name__) |
49 | 53 |
|
| 54 | +# Reduce third-party logging |
| 55 | +logging.getLogger("httpx").setLevel(logging.WARNING) |
| 56 | +logging.getLogger("openai").setLevel(logging.WARNING) |
| 57 | + |
| 58 | + |
50 | 59 | # Environment setup |
51 | 60 | MEMORY_SERVER_URL = os.getenv("MEMORY_SERVER_URL", "http://localhost:8000") |
52 | 61 | REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379") |
|
56 | 65 |
|
57 | 66 | SYSTEM_PROMPT = { |
58 | 67 | "role": "system", |
59 | | - "content": textwrap.dedent(""" |
60 | | - You are a helpful travel assistant. You can help with travel-related questions. |
61 | | - You have access to conversation history and memory management tools to provide |
62 | | - personalized responses. |
63 | | -
|
64 | | - Available tools: |
65 | | -
|
66 | | - 1. **web_search** (if available): Search for current travel information, weather, |
67 | | - events, or other up-to-date data when specifically needed. |
68 | | -
|
69 | | - 2. **Memory Management Tools** (always available): |
70 | | - - **search_memory**: Look up previous conversations and stored information |
71 | | - - **get_working_memory**: Check current session context |
72 | | - - **add_memory_to_working_memory**: Store important preferences or information |
73 | | - - **update_working_memory_data**: Save session-specific data |
74 | | -
|
75 | | - **Guidelines**: |
76 | | - - Answer the user's actual question first and directly |
77 | | - - When someone shares information (like "I like X"), simply acknowledge it naturally - don't immediately give advice or suggestions unless they ask |
78 | | - - Search memory or web when it would be helpful for the current conversation |
79 | | - - Don't assume the user is actively planning a trip unless they explicitly say so |
80 | | - - Be conversational and natural - respond to what the user actually says |
81 | | - - When sharing memories, simply state what you remember rather than turning it into advice |
82 | | - - Only offer suggestions, recommendations, or tips if the user explicitly asks for them |
83 | | - - Store preferences and important details, but don't be overly eager about it |
84 | | - - If someone shares a preference, respond like a friend would - acknowledge it, maybe ask a follow-up question, but don't launch into advice |
85 | | -
|
86 | | - Be helpful, friendly, and responsive. Mirror their conversational style - if they're just chatting, chat back. If they ask for help, then help. |
87 | | - """), |
| 68 | + "content": """ |
| 69 | + You are a helpful travel assistant. You can help with travel-related questions. |
| 70 | + You have access to conversation history and memory management tools to provide |
| 71 | + personalized responses. |
| 72 | +
|
| 73 | + Available tools: |
| 74 | +
|
| 75 | + 1. **web_search** (if available): Search for current travel information, weather, |
| 76 | + events, or other up-to-date data when specifically needed. |
| 77 | +
|
| 78 | + 2. **Memory Management Tools** (always available): |
| 79 | + - **search_memory**: Look up previous conversations and stored information |
| 80 | + - **get_working_memory**: Check current session context |
| 81 | + - **add_memory_to_working_memory**: Store important preferences or information |
| 82 | + - **update_working_memory_data**: Save session-specific data |
| 83 | +
|
| 84 | + **Guidelines**: |
| 85 | + - Answer the user's actual question first and directly |
| 86 | + - When someone shares information (like "I like X"), simply acknowledge it naturally - don't immediately give advice or suggestions unless they ask |
| 87 | + - Search memory or web when it would be helpful for the current conversation |
| 88 | + - Don't assume the user is actively planning a trip unless they explicitly say so |
| 89 | + - Be conversational and natural - respond to what the user actually says |
| 90 | + - When sharing memories, simply state what you remember rather than turning it into advice |
| 91 | + - Only offer suggestions, recommendations, or tips if the user explicitly asks for them |
| 92 | + - Store preferences and important details, but don't be overly eager about it |
| 93 | + - If someone shares a preference, respond like a friend would - acknowledge it, maybe ask a follow-up question, but don't launch into advice |
| 94 | +
|
| 95 | + Be helpful, friendly, and responsive. Mirror their conversational style - if they're just chatting, chat back. If they ask for help, then help. |
| 96 | + """, |
88 | 97 | } |
89 | 98 |
|
90 | 99 |
|
@@ -151,12 +160,12 @@ def _setup_llms(self): |
151 | 160 | # Define the web search tool function |
152 | 161 | web_search_function = { |
153 | 162 | "name": "web_search", |
154 | | - "description": textwrap.dedent(""" |
| 163 | + "description": """ |
155 | 164 | Search the web for current information about travel destinations, |
156 | 165 | requirements, weather, events, or any other travel-related |
157 | 166 | queries. Use this when you need up-to-date information that may |
158 | 167 | not be in your training data. |
159 | | - """), |
| 168 | + """, |
160 | 169 | "parameters": { |
161 | 170 | "type": "object", |
162 | 171 | "properties": { |
|
0 commit comments