LlamaIndex হলো আপনার ডেটার সাথে সংযুক্ত LLM ব্যবহার করে নলেজ এজেন্ট তৈরির একটি ফ্রেমওয়ার্ক। এই উদাহরণটি দেখায় কিভাবে একটি রিসার্চ এজেন্টের জন্য একটি মাল্টি-এজেন্ট ওয়ার্কফ্লো তৈরি করতে হয়। LlamaIndex-এ, Workflows হলো এজেন্ট এবং মাল্টি-এজেন্ট সিস্টেমের মূল ভিত্তি।
আপনার একটি জেমিনি এপিআই কী (Gemini API key) প্রয়োজন। যদি আপনার কাছে আগে থেকে এটি না থাকে, তবে আপনি গুগল এআই স্টুডিও (Google AI Studio) থেকে একটি সংগ্রহ করতে পারেন। প্রথমে, প্রয়োজনীয় সমস্ত লামা ইনডেক্স (LlamaIndex) লাইব্রেরি ইনস্টল করুন। লামা ইনডেক্স অভ্যন্তরীণভাবে google-genai প্যাকেজটি ব্যবহার করে।
pip install llama-index llama-index-utils-workflow llama-index-llms-google-genai llama-index-tools-google
LlamaIndex-এ Gemini সেট আপ করুন
যেকোনো LlamaIndex এজেন্টের ইঞ্জিন হলো একটি LLM, যা যুক্তি ও পাঠ্য প্রক্রিয়াকরণের কাজ করে। এই উদাহরণে Gemini 3 Flash ব্যবহার করা হয়েছে। আপনার API কী-টি একটি এনভায়রনমেন্ট ভেরিয়েবল হিসেবে সেট করে নিন।
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-flash-preview")
সরঞ্জাম তৈরি করুন
এজেন্টরা বাইরের জগতের সাথে যোগাযোগ করার জন্য বিভিন্ন টুল ব্যবহার করে, যেমন ওয়েবে অনুসন্ধান করা বা তথ্য সংরক্ষণ করা। LlamaIndex-এর টুলগুলো সাধারণ পাইথন ফাংশন হতে পারে, অথবা আগে থেকে বিদ্যমান ToolSpecs থেকে ইম্পোর্ট করা যেতে পারে। Gemini-তে গুগল সার্চ ব্যবহারের জন্য একটি বিল্ট-ইন টুল রয়েছে, যা এখানে ব্যবহার করা হয়েছে।
from google.genai import types
google_search_tool = types.Tool(
google_search=types.GoogleSearch()
)
llm_with_search = GoogleGenAI(
model="gemini-3-flash-preview",
generation_config=types.GenerateContentConfig(tools=[google_search_tool])
)
এখন অনুসন্ধান প্রয়োজন এমন একটি কোয়েরি দিয়ে LLM ইনস্ট্যান্সটি পরীক্ষা করুন। এই নির্দেশিকাটি একটি চলমান ইভেন্ট লুপ (যেমন python -m asyncio বা Google Colab) ধরে নিচ্ছে।
response = await llm_with_search.acomplete("What's the weather like today in Biarritz?")
print(response)
রিসার্চ এজেন্ট টুল হিসেবে পাইথন ফাংশন ব্যবহার করবে। এই কাজটি করার জন্য একটি সিস্টেম তৈরি করার অনেক উপায় আছে। এই উদাহরণে, আপনি নিম্নলিখিতগুলি ব্যবহার করবেন:
-
search_webপ্রদত্ত বিষয়ে তথ্য খোঁজার জন্য গুগল সার্চের সাথে জেমিনি ব্যবহার করে। -
record_notesওয়েব থেকে প্রাপ্ত গবেষণা তথ্য স্টেট-এ সংরক্ষণ করে, যাতে অন্যান্য টুলগুলো তা ব্যবহার করতে পারে। -
write_reportResearchAgentকর্তৃক প্রাপ্ত তথ্য ব্যবহার করে প্রতিবেদনটি লেখে। -
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."
একটি মাল্টি-এজেন্ট অ্যাসিস্ট্যান্ট তৈরি করুন
একটি মাল্টি-এজেন্ট সিস্টেম তৈরি করতে, আপনাকে এজেন্টগুলো এবং তাদের পারস্পরিক ক্রিয়া-প্রতিক্রিয়া সংজ্ঞায়িত করতে হবে। আপনার সিস্টেমে তিনটি এজেন্ট থাকবে:
- একজন
ResearchAgentপ্রদত্ত বিষয়ে তথ্যের জন্য ওয়েবে অনুসন্ধান করে। -
ResearchAgentকর্তৃক প্রাপ্ত তথ্য ব্যবহার করে একজনWriteAgentপ্রতিবেদনটি লেখেন। - একজন
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 Documentation দেখুন।