Category: LangChain

  • Build Local RAG with DeepSeek models using LangChain

    Build Local RAG with DeepSeek models using LangChain

    Could DeepSeek be a game-changer in the AI landscape? There’s a buzz in the tech world about DeepSeek outperforming models like ChatGPT. With its DeepSeek-V3 boasting 671 billion parameters and a development cost of just $5.6 million, it’s definitely turning heads. Interestingly, Sam Altman himself has acknowledged some challenges with ChatGPT, which is priced at a $200 subscription, while DeepSeek remains free. This makes the integration of DeepSeek with LangChain even more exciting, opening up a world of possibilities for building sophisticated AI-powered solutions without breaking the bank. Let’s explore how you can get started.

    DeepSeek with LangChain

    What is DeepSeek?

    DeepSeek provides a range of open-source AI models that can be deployed locally or through various inference providers. These models are known for their high performance and versatility, making them a valuable asset for any AI project. You can utilize these models for a variety of tasks such as text generation, translation, and more.

    Why use LangChain with DeepSeek?

    LangChain simplifies the development of applications using large language models (LLMs), and using it with DeepSeek provides the following benefits:

    • Simplified Workflow: LangChain abstracts away complexities, making it easier to interact with DeepSeek models.
    • Chaining Capabilities: Chain operations like prompting and translation to create sophisticated AI applications.
    • Seamless Integration: A consistent interface for various LLMs, including DeepSeek, for smooth transitions and experiments.

    Setting Up DeepSeek with LangChain

    To begin, create a DeepSeek account and obtain an API key:

    1. Get an API Key: Visit DeepSeek’s API Key page to sign up and generate your API key.
    2. Set Environment Variables: Set the DEEPSEEK_API_KEY environment variable.
    import getpass
    import os
    
    if not os.getenv("DEEPSEEK_API_KEY"):
        os.environ["DEEPSEEK_API_KEY"] = getpass.getpass("Enter your DeepSeek API key: ")
    
    # Optional LangSmith tracing
    # os.environ["LANGSMITH_TRACING"] = "true"
    # os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

    3. Install the Integration Package: Install the langchain-deepseek-official package.

    pip install -qU langchain-deepseek-official

    Instantiating and Using ChatDeepSeek

    Instantiate ChatDeepSeek model:

    from langchain_deepseek import ChatDeepSeek
    
    llm = ChatDeepSeek(
        model="deepseek-chat",
        temperature=0,
        max_tokens=None,
        timeout=None,
        max_retries=2,
        # other params...
    )

    Invoke the model:

    messages = [
        (
            "system",
            "You are a helpful assistant that translates English to French. Translate the user sentence.",
        ),
        ("human", "I love programming."),
    ]
    ai_msg = llm.invoke(messages)
    print(ai_msg.content)

    This will output the translated sentence in french.

    Chaining DeepSeek with LangChain Prompts

    Use ChatPromptTemplate to create a translation chain:

    from langchain_core.prompts import ChatPromptTemplate
    
    prompt = ChatPromptTemplate(
        [
            (
                "system",
                "You are a helpful assistant that translates {input_language} to {output_language}.",
            ),
            ("human", "{input}"),
        ]
    )
    
    chain = prompt | llm
    result = chain.invoke(
        {
            "input_language": "English",
            "output_language": "German",
            "input": "I love programming.",
        }
    )
    print(result.content)

    This demonstrates how easily you can configure language translation using prompt templates and DeepSeek models.

    Integrating DeepSeek using LangChain allows you to create advanced AI applications with ease and efficiency, and offers a potential alternative to other expensive models in the market. By following this guide, you can set up, use, and chain DeepSeek models to perform various tasks. Explore the API Reference for more detailed information.