LangChain SearchScope for Searching Messages and Summaries

Posted: Nov 24, 2024.

SearchScope is a LangChain enum class used to specify which documents to search in chat history - either individual messages or generated summaries. It's particularly useful when working with chat history retrievers like ZepRetriever to control the scope of your semantic searches.

What is SearchScope?

SearchScope is an enumeration class that defines two possible search scopes when querying chat history:

  1. Searching through individual chat messages
  2. Searching through generated summaries of conversations

This allows you to control whether you want to search through granular message-level content or higher-level summaries that capture the key points of conversations.

Reference

The SearchScope enum has two values:

ValueDescription
messagesSearch through individual chat history messages
summarySearch through generated summaries of chat conversations

How to Use SearchScope

Here are examples of how to use SearchScope with a chat history retriever:

Searching Through Messages

To search through individual chat messages:

from langchain_community.retrievers.zep import SearchScope, ZepRetriever

retriever = ZepRetriever(
    session_id="user123",
    url="http://localhost:8000",
    search_scope=SearchScope.messages # Search through individual messages
)

# This will search through individual messages
results = await retriever.ainvoke("What did we discuss about AI?")

Searching Through Summaries

To search through conversation summaries instead:

from langchain_community.retrievers.zep import SearchScope, ZepRetriever

retriever = ZepRetriever(
    session_id="user123", 
    url="http://localhost:8000",
    search_scope=SearchScope.summary # Search through conversation summaries
)

# This will search through generated summaries
results = await retriever.ainvoke("What were the key points about machine learning?")

Benefits of Using Summaries

Searching through summaries can be advantageous when you want to:

  • Get a high-level overview of conversations
  • Search through condensed versions of long chat histories
  • Find key points without getting lost in conversation details
  • Reduce the number of search results to process

Example combining with other retriever options:

retriever = ZepRetriever(
    session_id="user123",
    url="http://localhost:8000",
    top_k=3, # Return top 3 results
    search_scope=SearchScope.summary,
    search_type=SearchType.mmr, # Use maximal marginal relevance
    mmr_lambda=0.5 # Balance between relevance and diversity
)

# This will return 3 diverse summaries ranked by relevance
results = await retriever.ainvoke("What did we discuss about natural language processing?")

By choosing the appropriate SearchScope, you can optimize your chat history searches based on whether you need detailed message-level information or high-level conversation summaries.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs