LlamaIndex, verilerinize bağlı LLM'leri kullanarak bilgi aracıları oluşturmaya yönelik bir çerçevedir. Bu örnekte, bir Araştırma Ajanı için çoklu ajan iş akışının nasıl oluşturulacağı gösterilmektedir. LlamaIndex'te
Workflows, temsilci ve çoklu temsilci sistemlerinin yapı taşlarıdır.
Gemini API anahtarına ihtiyacınız vardır. Henüz bir hesabınız yoksa Google AI Studio'da hesap oluşturabilirsiniz.
Öncelikle, gerekli tüm LlamaIndex kitaplıklarını yükleyin. LlamaIndex, arka planda google-genai paketini kullanır.
pip install llama-index llama-index-utils-workflow llama-index-llms-google-genai llama-index-tools-google
LlamaIndex'te Gemini'ı kurma
Herhangi bir LlamaIndex aracının motoru, akıl yürütme ve metin işlemeyi gerçekleştiren bir LLM'dir. Bu örnekte Gemini 3 Flash kullanılmaktadır. API anahtarınızı ortam değişkeni olarak ayarladığınızdan emin olun.
import os
from llama_index.llms.google_genai import GoogleGenAI
# Set your API key in the environment elsewhere, or with os.environ['GEMINI_API_KEY'] = '...'
assert 'GEMINI_API_KEY' in os.environ
llm = GoogleGenAI(model="gemini-3.6-flash")
Derleme araçları
Aracı, web'de arama yapmak veya bilgi depolamak gibi dış dünyayla etkileşim kurmak için araçları kullanır. LlamaIndex'teki araçlar
normal Python işlevleri olabilir veya önceden var olan ToolSpecs'dan içe aktarılabilir.
Gemini, Google Arama'yı kullanmak için yerleşik bir araçla birlikte gelir. Burada bu araç kullanılır.
from google.genai import types
google_search_tool = types.Tool(
google_search=types.GoogleSearch()
)
llm_with_search = GoogleGenAI(
model="gemini-3.6-flash",
generation_config=types.GenerateContentConfig(tools=[google_search_tool])
)
Şimdi LLM örneğini arama gerektiren bir sorguyla test edin. Bu kılavuzda, çalışan bir etkinlik döngüsü (ör. python -m asyncio veya Google Colab) olduğu varsayılır.
response = await llm_with_search.acomplete("What's the weather like today in Biarritz?")
print(response)
Araştırma Aracısı, Python işlevlerini araç olarak kullanır. Bu görevi gerçekleştirecek bir sistem oluşturmanın birçok yolu vardır. Bu örnekte aşağıdakileri kullanacaksınız:
search_web, verilen konuyla ilgili bilgileri web'de aramak için Google Arama ile Gemini'ı kullanır.record_notes, web'de bulunan araştırmaları diğer araçların kullanabilmesi için duruma kaydeder.write_report,ResearchAgenttarafından bulunan bilgileri kullanarak raporu yazar.review_reportraporu inceler ve geri bildirim sağlar.
Context sınıfı, durumları aracılar/araçlar arasında aktarır ve her aracı, sistemin mevcut durumuna erişebilir.
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."
Birden çok temsilcinin yer aldığı bir asistan oluşturma
Çoklu temsilci sistemi oluşturmak için temsilcileri ve etkileşimlerini tanımlarsınız. Sisteminizde üç temsilci bulunur:
ResearchAgent, verilen konuyla ilgili bilgi için web'de arama yapar.WriteAgent,ResearchAgenttarafından bulunan bilgileri kullanarak raporu yazar.- Bir
ReviewAgentraporu inceler ve geri bildirim sağlar.
Bu örnekte, AgentWorkflow sınıfı kullanılarak bu aracıları sırayla yürütecek çok aracılı bir sistem oluşturuluyor. Her aracı, ne yapması gerektiğini söyleyen ve diğer aracılarla nasıl çalışılacağını öneren bir system_prompt alır.
İsteğe bağlı olarak, can_handoff_to kullanarak çoklu aracı sisteminizin hangi diğer aracılarla konuşabileceğini belirterek sisteminize yardımcı olabilirsiniz (Aksi takdirde, sistem bunu kendi başına bulmaya çalışır).
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."