LangChain Community

repository·main·Indexed 43 days ago

https://github.com/langchain-ai/langchain-community

A collection of third-party integrations for the LangChain ecosystem, including various LLM caching implementations such as InMemoryCache, RedisCache, and semantic caches like OpenSearchSemanticCache. Note: version 0.4.2 of the langchain-community package has been sunset; developers are encouraged to migrate to standalone integration packages.

Tokens
4.4K
Snippets
15
Records
22
Agent score
52%

What's inside LangChain Community

  1. Overview of LangChain Community integrations

    main

    The langchain-community package contains community-maintained integrations for the LangChain ecosystem.

    Important Notice: langchain-community has been sunset. Developers should refer to the official guidance in the repository issues for migration or future integration paths.

  2. What is LangChain Community?

    main

    LangChain Community contains third-party integrations that implement the base interfaces defined in LangChain Core. These integrations are designed to be ready-to-use within any LangChain application.

    WARNING

    langchain-community has been sunset. Users should refer to the project's official guidance regarding the sunsetting of this package.

  3. How LangChain Cache works

    main

    Cache provides an optional layer for LLMs to reduce API costs and improve application speed by storing and reusing previous completions.

    Class Hierarchy: All cache implementations inherit from BaseCache. Common implementations include:

    • InMemoryCache: Stores data in local memory.
    • SQLAlchemyCache / SQLiteCache: Uses SQL databases (like SQLite) as a backend.
    • RedisCache / UpstashRedisCache: Uses Redis for distributed caching.
    • RedisSemanticCache: Uses a vector store backend for similarity-based lookups.
    • GPTCache: Uses the GPTCache library.
    • MomentoCache: Uses the Momento service.

    Note: Cache directly competes with Memory. Refer to LangChain documentation for pros and cons of each.

    BaseCache --> <name>Cache  # Examples: InMemoryCache, RedisCache, GPTCache
  4. How semantic caches work

    main

    Semantic caches (like OpenSearchSemanticCache and SingleStoreDBSemanticCache) differ from standard key-value caches by using vector similarity. Instead of looking for an exact string match, they use an embedding model to convert prompts into vectors and perform a similarity search.

    Workflow:

    1. Lookup: When a prompt is provided, the cache performs a similarity search (e.g., similarity_search or similarity_search_with_score) to find the most similar previously seen prompt. If the similarity score exceeds a defined score_threshold (or search_threshold), the cached generation is returned.
    2. Update: If a cache miss occurs, the LLM generates a response. The cache then stores the prompt and the serialized Generation objects (using dumps) in the backend.
    3. Clear: Caches can be cleared either globally (like MemcachedCache) or for a specific llm_string (like OpenSearchSemanticCache), which typically involves deleting the specific index or container associated with that LLM.
  5. Migrate from langchain-community to standalone integration packages

    main
    The langchain-community package has been sunset and is no longer actively maintained. Developers should migrate to standalone integration packages (e.g., langchain-openai, langchain-anthropic, etc.) to ensure continued support and updates. For detailed migration guidance and information regarding the sunsetting of this package, refer to the official discussion: https://github.com/langchain-ai/langchain-community/issues/674.
  6. Use OpenSearchSemanticCache for semantic caching

    main

    The OpenSearchSemanticCache uses an OpenSearch vector store backend to perform semantic lookups. It stores prompts as text and their corresponding LLM generations in metadata. When a new prompt is received, it performs a similarity search to find the closest matching prompt in the cache.

    Key Parameters:

    • opensearch_url (str): The URL to connect to your OpenSearch instance.
    • embedding (Embeddings): An embedding provider used to encode prompts for semantic search.
    • score_threshold (float, default: 0.2): The minimum similarity score required for a result to be returned from the cache.
    • **kwargs: Additional connection arguments for the OpenSearch client.
    import langchain
    from langchain_community.cache import OpenSearchSemanticCache
    from langchain_openai import OpenAIEmbeddings
    
    langchain.llm_cache = OpenSearchSemanticCache(
        opensearch_url="http://localhost:9200",
        embedding=OpenAIEmbeddings()
    )
  7. Use MemcachedCache for LLM caching

    main

    The MemcachedCache uses a Memcached backend via the pymemcache library to cache LLM generations. It uses a hashed key derived from the prompt and the llm_string to store and retrieve values.

    Requirements:

    • You must have the pymemcache package installed: pip install -U pymemcache.

    Initialization: Pass an instance of a pymemcache client (such as Client, PooledClient, HashClient, or RetryingClient) to the constructor.

    Methods:

    • clear(**kwargs): Clears the entire cache. Supports optional arguments:
      • delay (int): Seconds to wait before flushing (default: 0).
      • noreply (bool): If True, does not wait for the reply.
    from langchain_community.cache import MemcachedCache
    from pymemcache.client.base import Client
    from langchain_openai import OpenAI
    from langchain.globals import set_llm_cache
    
    llm = OpenAI(model="gpt-3.5-turbo-instruct", n=2, best_of=2)
    set_llm_cache(MemcachedCache(Client('localhost')))
    
    # First call: Cache miss
    llm.invoke("Which city is the most crowded city in the USA?")
    
    # Second call: Cache hit
    llm.invoke("Which city is the most crowded city in the USA?")
  8. Use SQLiteCache for persistent local caching

    main

    SQLiteCache provides a persistent caching layer using a SQLite database. It is a subclass of SQLAlchemyCache and is ideal for local development where you want cache to persist across application restarts.

    from langchain_community.cache import SQLiteCache
    
    # Defaults to creating '.langchain.db' in the current directory
    cache = SQLiteCache(database_path=".my_custom_cache.db")
  9. Use SingleStoreDBSemanticCache (Deprecated)

    main

    The SingleStoreDBSemanticCache uses SingleStore DB as a backend.

    ⚠️ Deprecation Warning: This class is pending deprecation. Users should migrate to SingleStoreSemanticCache in the langchain_singlestore package.

    Key Configuration Options:

    • embedding (Embeddings): A text embedding model.
    • cache_table_prefix (str, default: "cache_"): Prefix for the cache table name.
    • search_threshold (float, default: 0.2): Minimum similarity score for a match.
    • distance_strategy: Determines how distance is calculated. Options are DOT_PRODUCT (default) or EUCLIDEAN_DISTANCE.
    • use_vector_index (bool, default: False): Toggles vector index usage (requires SingleStoreDB 8.5+).
    • vector_size (int): Required if use_vector_index is True.
    • host, user, password, port, database: Standard database connection parameters.
    import langchain
    from langchain_community.cache import SingleStoreDBSemanticCache
    from langchain_openai import OpenAIEmbeddings
    
    # Basic Usage
    langchain.llm_cache = SingleStoreDBSemanticCache(
        embedding=OpenAIEmbeddings(),
        host="https://user:password@127.0.0.1:3306/database"
    )
    
    # Advanced Usage
    langchain.llm_cache = SingleStoreDBSemanticCache(
        embedding=OpenAIEmbeddings(),
        use_vector_index=True,
        host="127.0.0.1",
        port=3306,
        user="user",
        password="password",
        database="db",
        table_name="my_custom_table",
        pool_size=10,
        timeout=60,
    )