Files
backend/app/ai/services/memory_service.py
2026-01-30 19:31:38 -08:00

25 lines
954 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# services/memory_service.py
from langchain_google_genai import GoogleGenerativeAIEmbeddings
# 假设你使用的是 pgvector
async def search_memories(query: str, db_connection):
"""
1. 将 query 转化为 Embedding
2. 在数据库中执行向量相似度搜索
3. 返回最相关的 Top-K 条记忆
"""
# 模拟实现
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
query_vector = await embeddings.aembed_query(query)
# 这里执行 SQL: SELECT content FROM memories ORDER BY embedding <=> query_vector LIMIT 3
results = "用户此前提到过他在做 Gemini 相关的 Hackathon倾向于使用 Python。"
return results
async def save_to_memory(content: str, db_connection):
"""
这个函数由你的 '保存' 按钮触发。
"""
# 1. 提取 content 中的关键信息(可选,可以用 LLM 提取)
# 2. 生成 Embedding 并存入数据库
pass