File size: 1,641 Bytes
f45999f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38ccc4e
f45999f
 
 
38ccc4e
f45999f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_core.tools import tool
from langchain_core.runnables import RunnableConfig

from .vectorstore import vector_store
from .memory_client import memory_client
from .utils import format_docs, format_memories

from core.config import get_settings

settings = get_settings()

@tool
def search_vectorstore(
    query: str,
    limit: int = settings.TOP_K,
    link: str = None,
) -> str:
    """
    Searches the vectorstore for relevant documents.
    Args:
        query (str): A detailed, descriptive query in **English language**.
        limit (int, optional): Number of Documents to retrieve. It should be between 2-8. Defaults to 4.
        link (str, optional): A document-specific link used to filter results 
            to only include results from that source video. e.g. https://youtu.be/I-QWB9z0l9k?si=XgxcKklmE_cNGneD

    Returns:
        str: A string representation of the retrieved documents, 
            each wrapped in a `<Document>` XML tag
    """
    limit = max(min(limit, 8), 2)
    results = vector_store.similarity_search(
        query, k=limit, filter={"link": link} if link else None
    )
    return format_docs(results)

@tool
def search_memories(query: str, config: RunnableConfig):
    """
    Search memories about the user.
    Args:
        query (str): A natural language query in **English language**.

    Returns:
        str: A string representation of the user memories.
    """
    user_id = config.get("configurable", {}).get("user_id")
    memories = memory_client.search(
        query, version="v2", filters={"AND": [{"user_id": user_id}]}
    )

    return format_memories(memories)