Knowledge Tools
Use OpenAI embeddings with LanceDB for hybrid search to build a knowledge-augmented DashScope agent.
Here is a tool with reasoning capabilities to allow agents to search and analyze information from a knowledge base.
"""
Here is a tool with reasoning capabilities to allow agents to search and analyze information from a knowledge base.
1. Run: `uv pip install openai agno lancedb sqlalchemy` to install the dependencies
2. Export your OPENAI_API_KEY
3. Run: `cookbook/90_models/dashscope/knowledge_tools.py` to run the agent
"""
from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.dashscope import DashScope
from agno.tools.knowledge import KnowledgeTools
from agno.vectordb.lancedb import LanceDb, SearchType
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
# Create a knowledge containing information from a URL
agno_docs = Knowledge(
# Use LanceDB as the vector database and store embeddings in the `agno_docs` table
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
embedder=OpenAIEmbedder(id="text-embedding-3-small"),
),
)
# Add content to the knowledge
agno_docs.insert(url="https://docs.agno.com/llms-full.txt")
knowledge_tools = KnowledgeTools(
knowledge=agno_docs,
enable_think=True,
enable_search=True,
enable_analyze=True,
add_few_shot=True,
)
agent = Agent(
model=DashScope(id="qwen-plus"),
tools=[knowledge_tools],
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent.print_response(
"How do I build a team of agents in agno?",
markdown=True,
stream=True,
)Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno beautifulsoup4 lancedb openai pyarrowConfigure regional DashScope access
Activate Alibaba Cloud Model Studio and create an API key for your region and workspace. Agno's default endpoint is Singapore: https://dashscope-intl.aliyuncs.com/compatible-mode/v1.
export DASHSCOPE_API_KEY="your_singapore_api_key"For another region or a workspace-specific domain, pass the matching base_url to DashScope. Use the OpenAI-compatible endpoint guide to match the URL, key and available model. The documented Singapore default remains functional.
Export the embedding API key
The OpenAI embedder makes separate requests while indexing and searching.
export OPENAI_API_KEY="your_openai_api_key"Run the example
Save the code above as knowledge_tools.py, then run:
python knowledge_tools.pyFull source: cookbook/90_models/dashscope/knowledge_tools.py