LlamaIndex היא מסגרת ליצירת סוכני ידע באמצעות מודלים גדולים של שפה (LLM) שמחוברים לנתונים שלכם. בדוגמה הזו מוסבר איך ליצור תהליך עבודה עם כמה סוכנים לסוכן מחקר. ב-LlamaIndex, Workflows
הם אבני הבניין של מערכות עם סוכן אחד או כמה סוכנים.
אתם צריכים מפתח 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
הגדרת Gemini 2.5 Pro ב-LlamaIndex
המנוע של כל סוכן LlamaIndex הוא LLM שמטפל בהסקת מסקנות ובעיבוד טקסט. בדוגמה הזו נשתמש ב-Gemini 2.5 Pro. חשוב להגדיר את מפתח ה-API כמשתנה סביבה.
from llama_index.llms.google_genai import GoogleGenAI
llm = GoogleGenAI(model="gemini-2.5-pro")
כלי בנייה
סוכנים משתמשים בכלים כדי ליצור אינטראקציה עם העולם החיצוני, כמו חיפוש באינטרנט או אחסון מידע. כלים ב-LlamaIndex יכולים להיות פונקציות רגילות של Python, או מיובאים מ-ToolSpecs
קיימים. Gemini כולל כלי מובנה לשימוש בחיפוש Google, שמשמש כאן.
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])
)
עכשיו בודקים את מופע ה-LLM באמצעות שאילתה שדורשת חיפוש:
response = llm_with_search.complete("What's the weather like today in Biarritz?")
print(response)
הנציג למחקר ישתמש בפונקציות של Python ככלים. יש הרבה דרכים לבנות מערכת שתבצע את המשימה הזו. בדוגמה הזו, תשתמשו בנתונים הבאים:
-
search_web
משתמש ב-Gemini עם חיפוש Google כדי לחפש באינטרנט מידע על הנושא שצוין. -
record_notes
שומר את המחקר שנמצא באינטרנט במצב, כדי שהכלים האחרים יוכלו להשתמש בו. -
write_report
כותב את הדוח באמצעות המידע שנמצא על ידיResearchAgent
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
מחפש באינטרנט מידע על הנושא שצוין. WriteAgent
כותב את הדוח באמצעות המידע שנמצא על ידיResearchAgent
.-
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.