Agente de pesquisa com Gemini 2.5 Pro e LlamaIndex

O LlamaIndex é um framework para criar agentes de conhecimento usando LLMs conectados aos seus dados. Este exemplo mostra como criar um fluxo de trabalho de vários agentes para um agente de pesquisa. No LlamaIndex, os Workflows são os elementos básicos de sistemas de agentes ou multiagentes.

Você precisa de uma chave da API Gemini. Se você ainda não tiver uma, crie uma no Google AI Studio. Primeiro, instale todas as bibliotecas necessárias do LlamaIndex.O LlamaIndex usa o pacote google-genai nos bastidores.

pip install llama-index llama-index-utils-workflow llama-index-llms-google-genai llama-index-tools-google

Configurar o Gemini 2.5 Pro no LlamaIndex

O mecanismo de qualquer agente do LlamaIndex é um LLM que processa raciocínio e texto. Este exemplo usa o Gemini 2.5 Pro. Defina sua chave de API como uma variável de ambiente.

from llama_index.llms.google_genai import GoogleGenAI

llm = GoogleGenAI(model="gemini-2.5-pro")

Ferramentas de desenvolvimento

Os agentes usam ferramentas para interagir com o mundo externo, como pesquisar na Web ou armazenar informações. As ferramentas no LlamaIndex podem ser funções regulares do Python ou importadas de ToolSpecs preexistentes. O Gemini vem com uma ferramenta integrada para usar a Pesquisa Google, que é usada aqui.

from google.genai import types

google_search_tool = types.Tool(
    google_search=types.GoogleSearch()
)

llm_with_search = GoogleGenAI(
    model="gemini-2.5-pro",
    generation_config=types.GenerateContentConfig(tools=[google_search_tool])
)

Agora teste a instância do LLM com uma consulta que exija pesquisa:

response = llm_with_search.complete("What's the weather like today in Biarritz?")
print(response)

O agente de pesquisa vai usar funções Python como ferramentas. Há muitas maneiras de criar um sistema para realizar essa tarefa. Neste exemplo, você vai usar o seguinte:

  1. O search_web usa o Gemini com a Pesquisa Google para pesquisar informações sobre o assunto na Web.
  2. O record_notes salva a pesquisa encontrada na Web no estado para que as outras ferramentas possam usá-la.
  3. O write_report grava o relatório usando as informações encontradas pelo ResearchAgent.
  4. review_report analisa o relatório e envia feedback.

A classe Context transmite o estado entre agentes/ferramentas, e cada agente tem acesso ao estado atual do sistema.

from llama_index.core.workflow import Context

async def search_web(ctx: Context, query: str) -> str:
    """Useful for searching the web about a specific query or topic"""
    response = await llm_with_search.acomplete(f"""Please research given this query or topic,
    and return the result\n<query_or_topic>{query}</query_or_topic>""")
    return response

async def record_notes(ctx: Context, notes: str, notes_title: str) -> str:
    """Useful for recording notes on a given topic."""
    current_state = await ctx.store.get("state")
    if "research_notes" not in current_state:
        current_state["research_notes"] = {}
    current_state["research_notes"][notes_title] = notes
    await ctx.store.set("state", current_state)
    return "Notes recorded."

async def write_report(ctx: Context, report_content: str) -> str:
    """Useful for writing a report on a given topic."""
    current_state = await ctx.store.get("state")
    current_state["report_content"] = report_content
    await ctx.store.set("state", current_state)
    return "Report written."

async def review_report(ctx: Context, review: str) -> str:
    """Useful for reviewing a report and providing feedback."""
    current_state = await ctx.store.get("state")
    current_state["review"] = review
    await ctx.store.set("state", current_state)
    return "Report reviewed."

Criar um assistente com vários agentes

Para criar um sistema multiagente, você define os agentes e as interações deles. Seu sistema terá três agentes:

  1. Um ResearchAgent pesquisa na Web informações sobre o tema especificado.
  2. Um WriteAgent grava o relatório usando as informações encontradas pelo ResearchAgent.
  3. Um ReviewAgent analisa o relatório e envia feedback.

Este exemplo usa a classe AgentWorkflow para criar um sistema multiagente que vai executar esses agentes em ordem. Cada agente recebe um system_prompt que informa o que ele precisa fazer e sugere como trabalhar com os outros agentes.

Se quiser, você pode ajudar seu sistema multiagente especificando com quais outros agentes ele pode conversar usando can_handoff_to. Caso contrário, ele vai tentar descobrir isso sozinho.

from llama_index.core.agent.workflow import (
    AgentInput,
    AgentOutput,
    ToolCall,
    ToolCallResult,
    AgentStream,
)
from llama_index.core.agent.workflow import FunctionAgent, ReActAgent

research_agent = FunctionAgent(
    name="ResearchAgent",
    description="Useful for searching the web for information on a given topic and recording notes on the topic.",
    system_prompt=(
        "You are the ResearchAgent that can search the web for information on a given topic and record notes on the topic. "
        "Once notes are recorded and you are satisfied, you should hand off control to the WriteAgent to write a report on the topic."
    ),
    llm=llm,
    tools=[search_web, record_notes],
    can_handoff_to=["WriteAgent"],
)

write_agent = FunctionAgent(
    name="WriteAgent",
    description="Useful for writing a report on a given topic.",
    system_prompt=(
        "You are the WriteAgent that can write a report on a given topic. "
        "Your report should be in a markdown format. The content should be grounded in the research notes. "
        "Once the report is written, you should get feedback at least once from the ReviewAgent."
    ),
    llm=llm,
    tools=[write_report],
    can_handoff_to=["ReviewAgent", "ResearchAgent"],
)

review_agent = FunctionAgent(
    name="ReviewAgent",
    description="Useful for reviewing a report and providing feedback.",
    system_prompt=(
        "You are the ReviewAgent that can review a report and provide feedback. "
        "Your feedback should either approve the current report or request changes for the WriteAgent to implement."
    ),
    llm=llm,
    tools=[review_report],
    can_handoff_to=["ResearchAgent","WriteAgent"],
)

Os agentes estão definidos. Agora você pode criar e executar o AgentWorkflow.

from llama_index.core.agent.workflow import AgentWorkflow

agent_workflow = AgentWorkflow(
    agents=[research_agent, write_agent, review_agent],
    root_agent=research_agent.name,
    initial_state={
        "research_notes": {},
        "report_content": "Not written yet.",
        "review": "Review required.",
    },
)

Durante a execução do fluxo de trabalho, é possível transmitir eventos, chamadas de ferramentas e atualizações para o console.

from llama_index.core.agent.workflow import (
    AgentInput,
    AgentOutput,
    ToolCall,
    ToolCallResult,
    AgentStream,
)

research_topic = """Write me a report on the history of the web.
Briefly describe the history of the world wide web, including
the development of the internet and the development of the web,
including 21st century developments"""

handler = agent_workflow.run(
    user_msg=research_topic
)

current_agent = None
current_tool_calls = ""
async for event in handler.stream_events():
    if (
        hasattr(event, "current_agent_name")
        and event.current_agent_name != current_agent
    ):
        current_agent = event.current_agent_name
        print(f"\n{'='*50}")
        print(f"🤖 Agent: {current_agent}")
        print(f"{'='*50}\n")
    elif isinstance(event, AgentOutput):
        if event.response.content:
            print("📤 Output:", event.response.content)
        if event.tool_calls:
            print(
                "🛠️  Planning to use tools:",
                [call.tool_name for call in event.tool_calls],
            )
    elif isinstance(event, ToolCallResult):
        print(f"🔧 Tool Result ({event.tool_name}):")
        print(f"  Arguments: {event.tool_kwargs}")
        print(f"  Output: {event.tool_output}")
    elif isinstance(event, ToolCall):
        print(f"🔨 Calling Tool: {event.tool_name}")
        print(f"  With arguments: {event.tool_kwargs}")

Depois que o fluxo de trabalho for concluído, você poderá imprimir a saída final do relatório, bem como o estado final da revisão do agente de revisão.

state = await handler.ctx.store.get("state")
print("Report Content:\n", state["report_content"])
print("\n------------\nFinal Review:\n", state["review"])

Vá mais longe com fluxos de trabalho personalizados

O AgentWorkflow é uma ótima maneira de começar a usar sistemas multiagentes. Mas e se você precisar de mais controle? É possível criar um fluxo de trabalho do zero. Confira alguns motivos para criar seu próprio fluxo de trabalho:

  • Mais controle sobre o processo: você pode decidir o caminho exato que seus agentes vão seguir. Isso inclui criar loops, tomar decisões em determinados pontos ou fazer com que os agentes trabalhem em paralelo em tarefas diferentes.
  • Use dados complexos: vá além do texto simples. Com os fluxos de trabalho personalizados, você pode usar dados mais estruturados, como objetos JSON ou classes personalizadas, para entradas e saídas.
  • Trabalhe com diferentes mídias: crie agentes que possam entender e processar não apenas texto, mas também imagens, áudio e vídeo.
  • Planejamento mais inteligente: você pode criar um fluxo de trabalho que primeiro cria um plano detalhado antes de os agentes começarem a trabalhar. Isso é útil para tarefas complexas que exigem várias etapas.
  • Ativar a autocorreção: crie agentes que podem revisar o próprio trabalho. Se a saída não for boa o suficiente, o agente poderá tentar de novo, criando um ciclo de melhoria até que o resultado seja perfeito.

Para saber mais sobre os fluxos de trabalho do LlamaIndex, consulte a documentação dos fluxos de trabalho do LlamaIndex.