LangChain Desearch Integration
This project integrates the Desearch API with LangChain tools to enable various search and data-fetching functionalities, such as web searches, Twitter data retrieval, and AI-powered searches.
Features
- Grouped Tools:
- Search Tools: General-purpose search tools for AI, web, and Twitter searches.
- Twitter Tools: Tools specifically for Twitter-related operations.
Installation
Install the package using pip:
pip install langchain-desearch
Usage
Grouped Tools
Search Tools
The search_tools group contains tools for general-purpose searches:
DesearchTool: Perform AI searches, web link searches, and Twitter post searches.BasicWebSearchTool: Conduct basic web searches.BasicTwitterSearchTool: Perform advanced Twitter searches with filters.
Twitter Tools
The twitter_tools group contains tools specifically for Twitter-related operations:
BasicTwitterSearchTool: Perform a basic Twitter search using Desearch.FetchTweetsByUrlsTool: Retrieve tweets from specific URLs.FetchTweetsByIdTool: Fetch tweets using their unique IDs.FetchLatestTweetsTool: Get the latest tweets from a specific user.FetchTweetsAndRepliesByUserTool: Retrieve tweets and replies from a user.FetchRepliesByPostTool: Fetch replies to a specific Twitter post.FetchRetweetsByPostTool: Retrieve retweets of a specific post.FetchTwitterUserTool: Get detailed information about a Twitter user.
Examples
Using Tools
from langchain_desearch.tools import DesearchTool, BasicWebSearchTool, BasicTwitterSearchTool
from dotenv import load_dotenv
load_dotenv()
# Example 1: Using DesearchTool
tool = DesearchTool()
result = tool._run(
prompt="Bittensor",
tool=['web'],
model="NOVA",
date_filter="PAST_24_HOURS",
streaming=False
)
print(result)
# Example 2: Using BasicWebSearchTool
tool = BasicWebSearchTool()
result = tool._run(
query="Latest news on AI",
num=5,
start=1
)
print(result)
# Example 3: Using BasicTwitterSearchTool
tool = BasicTwitterSearchTool()
result = tool._run(
query="AI trends",
sort="Top",
count=5
)
print(result)
Using RAG (Retrieval-Augmented Generation)
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough, RunnableParallel
from langchain_core.output_parsers import StrOutputParser
from langchain_desearch.tools import DesearchTool
from langchain_deepseek import ChatDeepSeek
# Setup Desearch Tool
desearch_tool = DesearchTool()
# Template to wrap Desearch output
document_prompt = PromptTemplate.from_template("""
<source>
<result>{result}</result>
</source>
""")
# Retrieval chain using DesearchTool
def get_desearch_context(prompt: str) -> str:
return desearch_tool._run(prompt=prompt, tool="desearch_web", model="NOVA")
retrieval_chain = RunnableLambda(lambda query: {
"result": get_desearch_context(query)
}) | document_prompt | (lambda docs: docs.text)
# Prompt for RAG generation
generation_prompt = ChatPromptTemplate.from_messages([
("system", "You are an expert research assistant. You use xml-formatted context to research people's questions."),
("human", """
Please answer the following query based on the provided context. Please cite your sources at the end of your response.:
Query: {query}
---
<context>
{context}
</context>
""")
])
# Use DeepSeek for LLM
llm = ChatDeepSeek(
model="deepseek-chat",
temperature=0.7,
max_tokens=None,
timeout=None,
max_retries=2
)
output_parser = StrOutputParser()
# Final chain
chain = RunnableParallel({
"query": RunnablePassthrough(),
"context": retrieval_chain,
}) | generation_prompt | llm | output_parser
# Run it!
query = "Recent trends in AI safety research"
result = chain.invoke(query)
print(result)
Using the LangChain Agent
from langchain_desearch.agent import create_search_agent
from langchain_deepseek import ChatDeepSeek
# Create a DeepSeek LLM instance
llm = ChatDeepSeek(
model="deepseek-chat",
temperature=0.7,
max_tokens=None,
timeout=None,
max_retries=2
)
# Initialize the search agent
search_agent = create_search_agent(llm=llm)
# Use the agent to perform a task
state = {
"input_message": "What's the latest news on AI?",
}
response = search_agent.invoke(state)
print(f"Agent Response: {response['output']}")
Running Tests
Dummy Tests
Run the dummy tests to verify the tools' functionality with mocked data:
pytest tests/test_tools.py
Real API Tests
Run the real tests to verify the tools' functionality with the Desearch API:
pytest tests/test_tools_real.py
Note: Ensure you have a valid
DESEARCH_API_KEYin your.envfile before running real tests.
Project Structure
langchain_desearch/
├── langchain_desearch/
│ ├── __init__.py
│ ├── tools.py
│ ├── search_tools.py
│ ├── agent.py
├── examples/
│ ├── tools.py
│ ├── RAG.py
│ ├── agent.py
├── tests/
│ ├── test_tools.py
│ ├── test_tools_real.py
├── .env
├── README.md
├── setup.py
├── requirements.txt
Contributing
- Fork the repository.
- Create a new branch for your feature or bug fix:
git checkout -b feature-name
- Commit your changes:
git commit -m "Add feature-name"
- Push to your branch:
git push origin feature-name
- Create a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For questions or support, please contact [your-email@example.com].
Metadata
Release files for langchain-desearch 1.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| langchain_desearch-1.0.6.tar.gz | 8.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_desearch-1.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.4 kB
Release files / langchain_desearch-1.0.6.tar.gz
| Download URL | langchain_desearch-1.0.6.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b9d7873d8e0fee191b0348ca65f4fc5b8141e8f8cb6efe0fb37e6b7e6304e84
|
|
BLAKE2b-256 checksum How to use checksums |
160fd319f547f95174a5458fb33d04defd4202b24d7f85d86c55fd27e03c74a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / langchain_desearch-1.0.6-py3-none-any.whl
| Download URL | langchain_desearch-1.0.6-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05e7b0c3af497ebeb4b1ec06109509e433ad09c4ed2e081ba4f88c61e628b849
|
|
BLAKE2b-256 checksum How to use checksums |
dfc062dc4342f253e0bc74ddc25987ac269993f541d8ae47198368d345915e5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Security for langchain-desearch 1.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.