Gemini 2.5 Pro और LlamaIndex की मदद से रिसर्च एजेंट

LlamaIndex, एक ऐसा फ़्रेमवर्क है जिसकी मदद से, एलएलएम का इस्तेमाल करके नॉलेज एजेंट बनाए जा सकते हैं. ये एलएलएम, आपके डेटा से कनेक्ट होते हैं. इस उदाहरण में, रिसर्च एजेंट के लिए मल्टी-एजेंट वर्कफ़्लो बनाने का तरीका बताया गया है. LlamaIndex में, Workflows, एजेंट या मल्टी-एजेंट सिस्टम के बिल्डिंग ब्लॉक होते हैं.

आपके पास Gemini API पासकोड होना चाहिए. अगर आपके पास पहले से कोई Gemini API कुंजी नहीं है, तो Google AI Studio में जाकर इसे पाएं. सबसे पहले, LlamaIndex की सभी ज़रूरी लाइब्रेरी इंस्टॉल करें.LlamaIndex, बैकग्राउंड में google-genai पैकेज का इस्तेमाल करता है.

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

LlamaIndex में Gemini 2.5 Pro को सेट अप करना

LlamaIndex एजेंट का इंजन, एक एलएलएम होता है. यह तर्क करने और टेक्स्ट को प्रोसेस करने का काम करता है. इस उदाहरण में Gemini 2.5 Pro का इस्तेमाल किया गया है. पक्का करें कि आपने अपने एपीआई पासकोड को एनवायरमेंट वैरिएबल के तौर पर सेट किया हो.

from llama_index.llms.google_genai import GoogleGenAI

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

बिल्ड टूल

एजेंट, बाहरी दुनिया से इंटरैक्ट करने के लिए टूल का इस्तेमाल करते हैं. जैसे, वेब पर खोजना या जानकारी सेव करना. LlamaIndex में मौजूद टूल, सामान्य Python फ़ंक्शन हो सकते हैं. इन्हें पहले से मौजूद ToolSpecs से इंपोर्ट भी किया जा सकता है. Gemini में Google Search का इस्तेमाल करने के लिए, एक टूल पहले से मौजूद होता है. इसका इस्तेमाल यहां किया जाता है.

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])
)

अब एलएलएम इंस्टेंस को ऐसी क्वेरी के साथ टेस्ट करें जिसके लिए खोज करने की ज़रूरत होती है:

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

रिसर्च एजेंट, टूल के तौर पर Python फ़ंक्शन का इस्तेमाल करेगा. इस टास्क को पूरा करने के लिए, सिस्टम बनाने के कई तरीके हैं. इस उदाहरण में, इनका इस्तेमाल किया जाएगा:

  1. search_web दिए गए विषय के बारे में जानकारी खोजने के लिए, Google Search के साथ Gemini का इस्तेमाल करता है.
  2. record_notes वेब पर मिली रिसर्च को सेव करता है, ताकि दूसरे टूल इसका इस्तेमाल कर सकें.
  3. write_report, ResearchAgent से मिली जानकारी का इस्तेमाल करके रिपोर्ट लिखता है
  4. review_report शिकायत की समीक्षा करता है और सुझाव देता है.

Context क्लास, एजेंट/टूल के बीच स्टेट को पास करती है. साथ ही, हर एजेंट के पास सिस्टम की मौजूदा स्थिति का ऐक्सेस होता है.

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."

मल्टी-एजेंट असिस्टेंट बनाना

मल्टी-एजेंट सिस्टम बनाने के लिए, एजेंट और उनके इंटरैक्शन तय किए जाते हैं. आपके सिस्टम में तीन एजेंट होंगे:

  1. ResearchAgent दिए गए विषय के बारे में जानकारी खोजने के लिए, वेब पर खोज करता है.
  2. WriteAgent, ResearchAgent से मिली जानकारी का इस्तेमाल करके रिपोर्ट लिखता है.
  3. ReviewAgent रिपोर्ट की समीक्षा करता है और सुझाव या राय देता है.

इस उदाहरण में, AgentWorkflow क्लास का इस्तेमाल करके एक मल्टी-एजेंट सिस्टम बनाया गया है. यह सिस्टम, इन एजेंट को क्रम से लागू करेगा. हर एजेंट एक system_prompt लेता है, जिससे उसे पता चलता है कि उसे क्या करना है. साथ ही, इससे उसे यह भी पता चलता है कि अन्य एजेंट के साथ कैसे काम करना है.

आपके पास यह तय करने का विकल्प होता है कि मल्टी-एजेंट सिस्टम, can_handoff_to का इस्तेमाल करके किन अन्य एजेंट से बात कर सकता है. अगर ऐसा नहीं किया जाता है, तो सिस्टम खुद ही इसका पता लगाने की कोशिश करेगा.

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"],
)

एजेंट तय कर दिए गए हैं. अब 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.",
    },
)

वर्कफ़्लो के लागू होने के दौरान, इवेंट, टूल कॉल, और अपडेट को कंसोल पर स्ट्रीम किया जा सकता है.

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}")

वर्कफ़्लो पूरा होने के बाद, रिपोर्ट का फ़ाइनल आउटपुट प्रिंट किया जा सकता है. साथ ही, समीक्षा करने वाले एजेंट से मिली समीक्षा की फ़ाइनल स्थिति भी प्रिंट की जा सकती है.

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

कस्टम वर्कफ़्लो की मदद से, ज़्यादा काम करें

AgentWorkflow, मल्टी-एजेंट सिस्टम का इस्तेमाल शुरू करने का एक बेहतरीन तरीका है. हालांकि, अगर आपको ज़्यादा कंट्रोल की ज़रूरत हो, तो क्या करें? आपके पास नए सिरे से वर्कफ़्लो बनाने का विकल्प होता है. अपना खुद का वर्कफ़्लो बनाने की कुछ वजहें यहां दी गई हैं:

  • प्रोसेस पर ज़्यादा कंट्रोल: आपके पास यह तय करने का विकल्प होता है कि आपके एजेंट किस तरीके से काम करेंगे. इसमें लूप बनाना, कुछ पॉइंट पर फ़ैसले लेना या एजेंटों को अलग-अलग टास्क पर एक साथ काम करने के लिए कहना शामिल है.
  • जटिल डेटा का इस्तेमाल करें: सिर्फ़ टेक्स्ट का इस्तेमाल न करें. कस्टम वर्कफ़्लो की मदद से, इनपुट और आउटपुट के लिए ज़्यादा स्ट्रक्चर्ड डेटा का इस्तेमाल किया जा सकता है. जैसे, JSON ऑब्जेक्ट या कस्टम क्लास.
  • अलग-अलग मीडिया फ़ॉर्मैट के साथ काम करना: ऐसे एजेंट बनाएं जो न सिर्फ़ टेक्स्ट को समझ सकें और उसे प्रोसेस कर सकें, बल्कि इमेज, ऑडियो, और वीडियो को भी समझ सकें और उन्हें प्रोसेस कर सकें.
  • बेहतर प्लानिंग: ऐसा वर्कफ़्लो डिज़ाइन किया जा सकता है जो एजेंटों के काम शुरू करने से पहले, एक विस्तृत प्लान तैयार करे. यह उन मुश्किल कामों के लिए मददगार है जिन्हें पूरा करने के लिए कई चरणों की ज़रूरत होती है.
  • खुद से सुधार करने की सुविधा चालू करें: ऐसे एजेंट बनाएं जो अपने काम की समीक्षा कर सकें. अगर आउटपुट सही नहीं है, तो एजेंट फिर से कोशिश कर सकता है. इससे, नतीजे को बेहतर बनाने का एक लूप बन जाता है. यह तब तक चलता रहता है, जब तक नतीजा सही नहीं हो जाता.

LlamaIndex Workflows के बारे में ज़्यादा जानने के लिए, LlamaIndex Workflows का दस्तावेज़ देखें.