You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add query optimization for vector search with configurable models
- Add SLOW_MODEL and FAST_MODEL config settings with OpenAI defaults
- Create optimize_query_for_vector_search() function using FAST_MODEL
- Add optimize_query parameter to search functions (default True for API, False for MCP/tools)
- Update all docstrings to refer to "query for vector search" terminology
- Comprehensive test coverage with 27+ specific tests for query optimization
- Robust error handling with graceful fallbacks when optimization fails
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
"description": "Search long-term memory for relevant information based on a query. Use this when you need to recall past conversations, user preferences, or previously stored information. Note: This searches only long-term memory, not current working memory.",
840
+
"description": "Search long-term memory for relevant information using a query for vector search. Use this when you need to recall past conversations, user preferences, or previously stored information. Note: This searches only long-term memory, not current working memory.",
832
841
"parameters": {
833
842
"type": "object",
834
843
"properties": {
835
844
"query": {
836
845
"type": "string",
837
-
"description": "The search query describing what information you're looking for",
846
+
"description": "The query for vector search describing what information you're looking for",
Copy file name to clipboardExpand all lines: agent_memory_server/llms.py
+74Lines changed: 74 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -423,3 +423,77 @@ async def get_model_client(
423
423
raiseValueError(f"Unsupported model provider: {model_config.provider}")
424
424
425
425
return_model_clients[model_name]
426
+
427
+
428
+
asyncdefoptimize_query_for_vector_search(
429
+
query: str,
430
+
model_name: str|None=None,
431
+
) ->str:
432
+
"""
433
+
Optimize a user query for vector search using a fast model.
434
+
435
+
This function takes a natural language query and rewrites it to be more effective
436
+
for semantic similarity search. It uses a fast, small model to improve search
437
+
performance while maintaining query intent.
438
+
439
+
Args:
440
+
query: The original user query to optimize
441
+
model_name: Model to use for optimization (defaults to settings.fast_model)
442
+
443
+
Returns:
444
+
Optimized query string better suited for vector search
445
+
"""
446
+
ifnotqueryornotquery.strip():
447
+
returnquery
448
+
449
+
# Use fast model from settings if not specified
450
+
effective_model=model_nameorsettings.fast_model
451
+
452
+
# Create optimization prompt
453
+
optimization_prompt=f"""Transform this natural language query into an optimized version for semantic search. The goal is to make it more effective for finding semantically similar content while preserving the original intent.
454
+
455
+
Guidelines:
456
+
- Keep the core meaning and intent
457
+
- Use more specific and descriptive terms
458
+
- Remove unnecessary words like "tell me", "I want to know", "can you"
0 commit comments