25 lines
954 B
Python
25 lines
954 B
Python
# 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 |