RAG with LangChain x Desearch

    RAG with LangChain x Desearch

    The langchain-desearch package gives LangChain chains and agents access to real-time web and Twitter search through Desearch.

    Installation

    1. Install the package:

      bash
      pip install langchain-desearch
    2. Store your Desearch API key in a .env file:

      .env DESEARCH_API_KEY=your_api_key_here

    Tools Overview

    Search Tools

    • DesearchTool: search across web, AI, and Twitter posts.
    • BasicWebSearchTool: web search only.
    • BasicTwitterSearchTool: tweet search with filtering options.

    Twitter Tools

    • FetchTweetsByUrlsTool: retrieve tweets by URL.
    • FetchTweetsByIdTool: fetch tweets by ID.
    • FetchLatestTweetsTool: latest tweets from a user or topic.
    • FetchTweetsAndRepliesByUserTool: tweets and replies from a user.
    • FetchRepliesByPostTool: replies to a tweet.
    • FetchRetweetsByPostTool: retweets of a tweet.
    • FetchTwitterUserTool: details about a Twitter user.

    Quick Examples

    Basic Usage

    python
    from langchain_desearch.tools import DesearchTool tool = DesearchTool() response = tool._run( prompt="Bittensor network activity", tool=["web"], model="NOVA", ) print(response)

    Search Twitter

    python
    from langchain_desearch.tools import BasicTwitterSearchTool tool = BasicTwitterSearchTool() response = tool._run(query="AI governance", sort="Top", count=5) print(response)

    Building a RAG Chain

    Use Desearch to fetch context, then pass it to a prompt and LLM:

    python
    from langchain_desearch.tools import DesearchTool from langchain_core.prompts import PromptTemplate, ChatPromptTemplate from langchain_deepseek import ChatDeepSeek from langchain_core.runnables import RunnableParallel, RunnablePassthrough from langchain_core.output_parsers import StrOutputParser tool = DesearchTool() def fetch_context(query): return tool._run(prompt=query, tool="desearch_web", model="NOVA") prompt = ChatPromptTemplate.from_messages([ ("system", "You are a research assistant."), ("human", "Answer this using the provided context:\n\n<context>{context}</context>") ]) llm = ChatDeepSeek(model="deepseek-chat", temperature=0.7) parser = StrOutputParser() chain = RunnableParallel({ "query": RunnablePassthrough(), "context": lambda q: fetch_context(q["query"]), }) | prompt | llm | parser result = chain.invoke("What is the latest update on the EU AI Act?") print(result)

    Using Desearch in a LangChain Agent

    python
    from langchain_desearch.agent import create_search_agent from langchain_deepseek import ChatDeepSeek llm = ChatDeepSeek(model="deepseek-chat", temperature=0.7) agent = create_search_agent(llm=llm) response = agent.invoke({"input_message": "Summarize current AI regulation trends"}) print(response["output"])

    Testing

    Unit Tests (Mocked)

    bash
    pytest tests/test_tools.py

    Live API Tests

    bash
    pytest tests/test_tools_real.py

    ⚠️ Note: The DESEARCH_API_KEY must be set in your .env file to execute live tests.

    Contact

    For questions or feedback, open an issue on the repository or reach out via the Desearch contact page.