Spaces:
Running
Running
File size: 4,613 Bytes
e91e2b4 c5d7dd0 e91e2b4 0b7c6b7 7d97a41 c5d7dd0 e91e2b4 0b7c6b7 e4574f6 0b7c6b7 e91e2b4 0b7c6b7 e91e2b4 0b7c6b7 42ff401 0b7c6b7 7d97a41 0b7c6b7 7d97a41 e91e2b4 0b7c6b7 e4574f6 e91e2b4 0b7c6b7 e4574f6 e91e2b4 e4574f6 0b7c6b7 e91e2b4 e4f4760 e91e2b4 0b7c6b7 7d97a41 0b7c6b7 7d97a41 0b7c6b7 c5d7dd0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import os
import requests
from datetime import datetime
MEMMACHINE_PORT = os.getenv("MEMORY_SERVER_URL")
BACKEND_API_KEY = os.getenv("BACKEND_API_KEY")
PROMPT = """You are a helpful AI assistant. Use the provided context and profile information to answer the user's question accurately and helpfully.
<CURRENT_DATE>
{current_date}
</CURRENT_DATE>
Instructions:
- Use the PROFILE and CONTEXT data provided to answer the user's question
- Be conversational and helpful in your responses
- If you don't have enough information to answer completely, say so and suggest what additional information might be helpful
- If the context contains relevant information, use it to provide a comprehensive answer
- If no relevant context is available, let the user know and offer to help in other ways
- Be concise but thorough in your responses
- Use markdown formatting when appropriate to make your response clear and readable
Data Guidelines:
- Don't invent information that isn't in the provided context
- If information is missing or unclear, acknowledge this
- Prioritize the most recent and relevant information when available
- If there are conflicting pieces of information, mention this and explain the differences
Response Format:
- Directly answer the user's question, without showing your thought process
- Provide supporting details from the context when available
- Use bullet points or numbered lists when appropriate
- End with any relevant follow-up questions or suggestions"""
def ingest_and_rewrite(user_id: str, query: str) -> str:
"""Pass a raw user message through the memory server and get context-aware response."""
print("entered ingest_and_rewrite")
headers = {
"user-id": user_id,
"group-id": user_id,
"session-id": user_id,
"agent-id": "agent",
}
# Add API key if configured
if BACKEND_API_KEY:
headers["x-api-key"] = BACKEND_API_KEY
requests.post(
f"{MEMMACHINE_PORT}/v1/memories",
json={"producer": user_id, "produced_for": "agent", "episode_content": query},
headers=headers,
timeout=5,
)
resp = requests.post(
f"{MEMMACHINE_PORT}/v1/memories/search",
headers=headers,
json={"query": query},
timeout=1000,
)
resp.raise_for_status()
return PROMPT + "\n\n" + resp.text + "\n\n" + "User Query: " + query
def get_memories(user_id: str) -> dict:
"""Fetch all memories for a given user_id"""
headers = {
"user-id": user_id,
"group-id": user_id,
"session-id": user_id,
"agent-id": "agent",
}
# Add API key if configured
if BACKEND_API_KEY:
headers["x-api-key"] = BACKEND_API_KEY
try:
resp = requests.get(
f"{MEMMACHINE_PORT}/v1/memories",
headers=headers,
timeout=10,
)
resp.raise_for_status()
return resp.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching memories: {e}")
return {}
def ingest_memories(user_id: str, memories_text: str) -> bool:
"""Ingest imported memories into MemMachine system.
Args:
user_id: The user identifier
memories_text: Text containing memories/conversations to ingest
Returns:
True if successful, False otherwise
"""
headers = {
"user-id": user_id,
"group-id": user_id,
"session-id": user_id,
"agent-id": "agent",
}
# Add API key if configured
if BACKEND_API_KEY:
headers["x-api-key"] = BACKEND_API_KEY
try:
# Ingest the memories as an episode
resp = requests.post(
f"{MEMMACHINE_PORT}/v1/memories",
json={
"producer": user_id,
"produced_for": "agent",
"episode_content": memories_text
},
headers=headers,
timeout=10,
)
resp.raise_for_status()
return True
except requests.exceptions.RequestException as e:
print(f"Error ingesting memories: {e}")
return False
def delete_profile(user_id: str) -> bool:
"""Delete the session for the given user_id"""
headers = {
"user-id": user_id,
"group-id": user_id,
"session-id": user_id,
"agent-id": "agent",
}
# Add API key if configured
if BACKEND_API_KEY:
headers["x-api-key"] = BACKEND_API_KEY
requests.delete(f"{MEMMACHINE_PORT}/v1/memories", headers=headers, json={})
return True |