Overview

Valyu integrates with CrewAI to provide your AI agents with high-quality context from proprietary datasets and web sources, enabling more informed and accurate task execution.

Installation

Install both the Valyu SDK and CrewAI:

pip install valyu crewai

Add Your API Key to your environment

VALYU_API_KEY=<your_api_key>

Basic Usage

Here’s a simple example of using Valyu with CrewAI:

from valyu import Valyu
from crewai import Agent, Task, Crew
from crewai_tools import ValyuContextTool
from langchain_openai import ChatOpenAI

# Initialize Valyu tool
valyu_tool = ValyuContextTool(
    search_type="both",
    max_num_results=5,
    max_price=50
)

# Create a researcher agent with Valyu context
researcher = Agent(
    role='Research Analyst',
    goal='Provide accurate research using enriched context',
    backstory='Expert researcher with access to proprietary datasets',
    tools=[valyu_tool],
    llm=ChatOpenAI(model="gpt-4")
)

# Create a writer agent
writer = Agent(
    role='Technical Writer',
    goal='Write clear, accurate content based on research',
    backstory='Expert at synthesizing complex information',
    llm=ChatOpenAI(model="gpt-4")
)

# Define tasks
research_task = Task(
    description="Research quantum computing breakthroughs",
    agent=researcher
)

writing_task = Task(
    description="Write a summary of the findings",
    agent=writer
)

# Create and run the crew
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, writing_task]
)

result = crew.kickoff()

Advanced Usage

Custom Tool Configuration

Configure the ValyuContextTool with specific parameters:

# Create specialized research tools
proprietary_research = ValyuContextTool(
    search_type="proprietary",
    max_num_results=5,
    max_price=50,
    query_rewrite=True,
    similarity_threshold=0.4,
    data_sources=["academic_papers", "research_reports"]
)

web_research = ValyuContextTool(
    search_type="web",
    max_num_results=10,
    max_price=30
)

# Use the tools with agents
data_researcher = Agent(
    role='Data Researcher',
    goal='Find relevant data and research papers',
    tools=[proprietary_research]
)

web_researcher = Agent(
    role='Web Researcher',
    goal='Find recent developments and news',
    tools=[web_research]
)

Example Applications

Research and Analysis Crew

# Create specialized agents
data_researcher = Agent(
    role='Data Researcher',
    goal='Find relevant data and research papers',
    tools=[ValyuResearchTool(
        valyu,
        search_type="proprietary"  # Use proprietary datasets
    ).research]
)

web_researcher = Agent(
    role='Web Researcher',
    goal='Find recent developments and news',
    tools=[ValyuResearchTool(
        valyu,
        search_type="web"  # Use web sources
    ).research]
)

analyst = Agent(
    role='Research Analyst',
    goal='Synthesize findings into actionable insights'
)

# Define sequential tasks
tasks = [
    Task(
        description="Find academic papers on quantum computing",
        agent=data_researcher
    ),
    Task(
        description="Research recent industry developments",
        agent=web_researcher
    ),
    Task(
        description="Analyze and synthesize all findings",
        agent=analyst
    )
]

# Execute research pipeline
crew = Crew(
    agents=[data_researcher, web_researcher, analyst],
    tasks=tasks
)

result = crew.kickoff()

Best Practices

1. Cost Management

# Create a cost-aware research tool
valyu_tool = ValyuContextTool(
    max_price=5,  # Maximum credits per 1000 results
    max_num_results=3,
    search_type="both"
)

2. Error Handling

def handle_research(query: str, valyu_tool: ValyuContextTool) -> str:
    result = valyu_tool.run(query)
    if result["success"]:
        return "\n".join([
            f"Source: {r['source']}\nContent: {r['content']}"
            for r in result["results"]
        ])
    else:
        return f"Error accessing Valyu: {result['error']}. Proceeding with backup sources."

Additional Resources