Skip to content

Commit fb96e54

Browse files
sairin1202ankaisenCopilot
authored
feat: add non-RAG retrieve solution (#84)
Co-authored-by: An Kaisen <51148505+ankaisen@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0fa721a commit fb96e54

File tree

8 files changed

+660
-21
lines changed

8 files changed

+660
-21
lines changed

src/memu/app/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from memu.app.service import MemoryUser
2-
from memu.app.settings import BlobConfig, DatabaseConfig, LLMConfig, MemorizeConfig
2+
from memu.app.settings import BlobConfig, DatabaseConfig, LLMConfig, MemorizeConfig, RetrieveConfig
33

4-
__all__ = ["BlobConfig", "DatabaseConfig", "LLMConfig", "MemorizeConfig", "MemoryUser"]
4+
__all__ = ["BlobConfig", "DatabaseConfig", "LLMConfig", "MemorizeConfig", "MemoryUser", "RetrieveConfig"]

src/memu/app/service.py

Lines changed: 496 additions & 19 deletions
Large diffs are not rendered by default.

src/memu/app/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ class DatabaseConfig(BaseModel):
5555
provider: str = Field(default="memory")
5656

5757

58+
class RetrieveConfig(BaseModel):
59+
method: str = Field(
60+
default="rag",
61+
description="Retrieval method: 'rag' for embedding-based vector search, 'llm' for LLM-based ranking.",
62+
)
63+
top_k: int = Field(
64+
default=5,
65+
description="Maximum number of results to return per category.",
66+
)
67+
68+
5869
class MemorizeConfig(BaseModel):
5970
category_assign_threshold: float = Field(default=0.25)
6071
default_summary_prompt: str = Field(default="Summarize the text in one short paragraph.")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
PROMPT = """Your task is to search through the provided categories and identify the most relevant ones for the given query.
2+
3+
Analyze the query and all available categories, then select and rank the top-{top_k} most relevant categories.
4+
5+
## Query:
6+
{query}
7+
8+
## Available Categories:
9+
{categories_data}
10+
11+
## Output Format:
12+
Provide your response as a JSON array of category IDs, ordered from most to least relevant:
13+
```json
14+
{{
15+
"categories": ["category_id_1", "category_id_2", "category_id_3"]
16+
}}
17+
```
18+
19+
Important:
20+
- Include up to {top_k} most relevant categories
21+
- Order matters: first ID should be most relevant
22+
- Only include categories that are actually relevant to the query
23+
- Empty array is acceptable if no relevant categories are found
24+
"""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
PROMPT = """Your task is to search through the provided memory items and identify the most relevant ones for the given query.
2+
3+
These memory items belong to the following relevant categories that were already identified:
4+
{relevant_categories}
5+
6+
Analyze the query and the available memory items, then select and rank the top-{top_k} most relevant items.
7+
8+
## Query:
9+
{query}
10+
11+
## Available Memory Items:
12+
{items_data}
13+
14+
## Output Format:
15+
Provide your response as a JSON array of item IDs, ordered from most to least relevant:
16+
```json
17+
{{
18+
"items": ["item_id_1", "item_id_2", "item_id_3"]
19+
}}
20+
```
21+
22+
Important:
23+
- Include up to {top_k} most relevant items
24+
- Order matters: first ID should be most relevant
25+
- Only include items that are actually relevant to the query
26+
- Empty array is acceptable if no relevant items are found
27+
"""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
PROMPT = """Your task is to search through the provided resources and identify the most relevant ones for the given query.
2+
3+
These resources are related to the following categories and items that were already identified:
4+
{context_info}
5+
6+
Analyze the query and the available resources, then select and rank the top-{top_k} most relevant resources.
7+
8+
## Query:
9+
{query}
10+
11+
## Available Resources:
12+
{resources_data}
13+
14+
## Output Format:
15+
Provide your response as a JSON array of resource IDs, ordered from most to least relevant:
16+
```json
17+
{{
18+
"resources": ["resource_id_1", "resource_id_2", "resource_id_3"]
19+
}}
20+
```
21+
22+
Important:
23+
- Include up to {top_k} most relevant resources
24+
- Order matters: first ID should be most relevant
25+
- Only include resources that are actually relevant to the query
26+
- Empty array is acceptable if no relevant resources are found
27+
"""
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
SYSTEM_PROMPT = """You are a retrieval decision assistant. Your task is to analyze whether a query requires retrieving information from memory or can be answered directly without retrieval.
2+
3+
Consider these scenarios that DON'T need retrieval:
4+
- Greetings, casual chat, acknowledgments
5+
- Questions about current conversation/context only
6+
- General knowledge questions
7+
- Requests for clarification
8+
- Meta-questions about the system itself
9+
10+
Consider these scenarios that NEED retrieval:
11+
- Questions about past events, conversations, or interactions
12+
- Queries about user preferences, habits, or characteristics
13+
- Requests to recall specific information
14+
- Questions that reference historical data"""
15+
16+
USER_PROMPT = """Analyze the following query in the context of the conversation to determine if memory retrieval is needed.
17+
18+
## Conversation History (Last 3 Turns):
19+
{conversation_history}
20+
21+
## Current Query:
22+
{query}
23+
24+
## Task:
25+
1. Determine if this query requires retrieving information from memory
26+
2. If retrieval is needed, rewrite the query to incorporate relevant context from the conversation history
27+
28+
## Output Format:
29+
<decision>
30+
[Either "RETRIEVE" or "NO_RETRIEVE"]
31+
</decision>
32+
33+
<rewritten_query>
34+
[If RETRIEVE: provide a rewritten query with context. If NO_RETRIEVE: return original query]
35+
</rewritten_query>"""
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
SYSTEM_PROMPT = """You are a query rewriting and retrieval sufficiency judge. You have two tasks:
2+
3+
1. **Query Rewriting**: Incorporate conversation context to make the query more specific and clear
4+
2. **Sufficiency Judgment**: Determine if the retrieved content is enough to answer the query
5+
6+
You should be conservative - only mark as "ENOUGH" when the content truly provides adequate information."""
7+
8+
USER_PROMPT = """Given the conversation history, current query, and retrieved content, perform two tasks:
9+
10+
## Conversation History (Last 3 Turns):
11+
{conversation_history}
12+
13+
## Original Query:
14+
{original_query}
15+
16+
## Retrieved Content So Far:
17+
{retrieved_content}
18+
19+
## Tasks:
20+
21+
### 1. Query Rewriting
22+
Rewrite the query to incorporate relevant context from the conversation history. Make it more specific and clear.
23+
24+
### 2. Sufficiency Judgment
25+
Analyze if the retrieved content is sufficient to answer the query. Consider:
26+
1. Does the retrieved content directly address the query?
27+
2. Is the information specific and detailed enough?
28+
3. Are there obvious gaps or missing details?
29+
4. Did the user explicitly ask to recall or remember more information?
30+
31+
## Output Format:
32+
<rewritten_query>
33+
[Provide the rewritten query with conversation context]
34+
</rewritten_query>
35+
36+
<judgement>
37+
[Either "ENOUGH" if sufficient, or "MORE" if additional information is needed]
38+
</judgement>"""

0 commit comments

Comments
 (0)