Spaces:
Running
Running
Update gateway_client.py
Browse files- gateway_client.py +54 -40
gateway_client.py
CHANGED
|
@@ -6,60 +6,74 @@ import requests
|
|
| 6 |
# Backend server URL - can be set via environment variable
|
| 7 |
# For Hugging Face Spaces: Set MEMORY_SERVER_URL in Space settings (Repository secrets)
|
| 8 |
# For local development: Set MEMORY_SERVER_URL in your .env file
|
| 9 |
-
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
"""Pass a raw user message through the memory server and get context-aware response."""
|
| 15 |
-
print("entered ingest_and_rewrite")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
timeout=1000,
|
| 21 |
-
)
|
| 22 |
-
resp.raise_for_status()
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
requests.post(
|
| 30 |
-
f"{
|
| 31 |
-
|
|
|
|
| 32 |
timeout=5,
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
f"{EXAMPLE_SERVER_PORT}/memory",
|
| 40 |
-
params={
|
| 41 |
-
"query": query,
|
| 42 |
-
"user_id": user_id,
|
| 43 |
-
"timestamp": datetime.now().isoformat(),
|
| 44 |
-
},
|
| 45 |
timeout=1000,
|
| 46 |
)
|
| 47 |
resp.raise_for_status()
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
if search_results.get("profile"):
|
| 52 |
-
return f"Based on your profile: {search_results['profile']}\n\nQuery: {query}"
|
| 53 |
-
else:
|
| 54 |
-
return f"Query: {query}"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def add_new_session_message(user_id: str, msg: str) -> None:
|
| 58 |
-
"""Alias for add_session_message for backward compatibility."""
|
| 59 |
-
add_session_message(user_id, msg)
|
| 60 |
|
| 61 |
|
| 62 |
def delete_profile(user_id: str) -> bool:
|
| 63 |
-
"""Delete
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# Backend server URL - can be set via environment variable
|
| 7 |
# For Hugging Face Spaces: Set MEMORY_SERVER_URL in Space settings (Repository secrets)
|
| 8 |
# For local development: Set MEMORY_SERVER_URL in your .env file
|
| 9 |
+
MEMMACHINE_PORT = os.getenv("MEMORY_SERVER_URL")
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
+
PROMPT = """You are a helpful AI assistant. Use the provided context and profile information to answer the user's question accurately and helpfully.
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
<CURRENT_DATE>
|
| 16 |
+
{current_date}
|
| 17 |
+
</CURRENT_DATE>
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
Instructions:
|
| 20 |
+
- Use the PROFILE and CONTEXT data provided to answer the user's question
|
| 21 |
+
- Be conversational and helpful in your responses
|
| 22 |
+
- If you don't have enough information to answer completely, say so and suggest what additional information might be helpful
|
| 23 |
+
- If the context contains relevant information, use it to provide a comprehensive answer
|
| 24 |
+
- If no relevant context is available, let the user know and offer to help in other ways
|
| 25 |
+
- Be concise but thorough in your responses
|
| 26 |
+
- Use markdown formatting when appropriate to make your response clear and readable
|
| 27 |
|
| 28 |
+
Data Guidelines:
|
| 29 |
+
- Don't invent information that isn't in the provided context
|
| 30 |
+
- If information is missing or unclear, acknowledge this
|
| 31 |
+
- Prioritize the most recent and relevant information when available
|
| 32 |
+
- If there are conflicting pieces of information, mention this and explain the differences
|
| 33 |
|
| 34 |
+
Response Format:
|
| 35 |
+
- Start with a direct answer to the user's question
|
| 36 |
+
- Provide supporting details from the context when available
|
| 37 |
+
- Use bullet points or numbered lists when appropriate
|
| 38 |
+
- End with any relevant follow-up questions or suggestions"""
|
| 39 |
+
|
| 40 |
+
def ingest_and_rewrite(user_id: str, query: str) -> str:
|
| 41 |
+
"""Pass a raw user message through the memory server and get context-aware response."""
|
| 42 |
+
print("entered ingest_and_rewrite")
|
| 43 |
+
headers = {
|
| 44 |
+
"user-id": user_id,
|
| 45 |
+
"group-id": user_id,
|
| 46 |
+
"session-id": user_id,
|
| 47 |
+
"agent-id": "agent",
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
requests.post(
|
| 51 |
+
f"{MEMMACHINE_PORT}/v1/memories",
|
| 52 |
+
json={"producer": user_id, "produced_for": "agent", "episode_content": query},
|
| 53 |
+
headers=headers,
|
| 54 |
timeout=5,
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
resp = requests.post(
|
| 58 |
+
f"{MEMMACHINE_PORT}/v1/memories/search",
|
| 59 |
+
headers=headers,
|
| 60 |
+
json={"query": query},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
timeout=1000,
|
| 62 |
)
|
| 63 |
resp.raise_for_status()
|
| 64 |
|
| 65 |
+
return PROMPT + "\n\n" + resp.text + "\n\n" + "User Query: " + query
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def delete_profile(user_id: str) -> bool:
|
| 69 |
+
"""Delete the session for the given user_id"""
|
| 70 |
+
headers = {
|
| 71 |
+
"user-id": user_id,
|
| 72 |
+
"group-id": user_id,
|
| 73 |
+
"session-id": user_id,
|
| 74 |
+
"agent-id": "agent",
|
| 75 |
+
}
|
| 76 |
+
requests.delete(f"{MEMMACHINE_PORT}/v1/memories", headers=headers, json={})
|
| 77 |
+
return True
|
| 78 |
+
Shared in
|
| 79 |
+
|