File size: 2,818 Bytes
e91e2b4
c5d7dd0
e91e2b4
 
 
0b7c6b7
7d97a41
c5d7dd0
e91e2b4
0b7c6b7
 
 
e4574f6
0b7c6b7
 
 
 
 
 
 
 
e91e2b4
0b7c6b7
 
 
 
 
e91e2b4
0b7c6b7
 
 
 
 
 
 
 
 
 
 
 
 
7d97a41
0b7c6b7
 
7d97a41
 
 
 
e91e2b4
0b7c6b7
 
 
e4574f6
e91e2b4
0b7c6b7
 
 
 
 
e4574f6
e91e2b4
 
e4574f6
0b7c6b7
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
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:
- Start with a direct answer to the user's question
- 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 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