(คลิกที่ภาพด้านบนเพื่อดูวิดีโอของบทเรียนนี้)
กรอบการทำงานของ AI Agent เป็นแพลตฟอร์มซอฟต์แวร์ที่ออกแบบมาเพื่อช่วยให้ง่ายต่อการสร้าง การปรับใช้ และการจัดการ AI Agent กรอบการทำงานเหล่านี้มอบส่วนประกอบสำเร็จรูป การย่อส่วน และเครื่องมือที่ช่วยให้การพัฒนาระบบ AI ที่ซับซ้อนเป็นไปอย่างราบรื่น
กรอบการทำงานเหล่านี้ช่วยให้นักพัฒนามุ่งเน้นไปที่แง่มุมเฉพาะของแอปพลิเคชันของพวกเขา โดยการนำเสนอวิธีการมาตรฐานในการแก้ปัญหาทั่วไปในกระบวนการพัฒนา AI Agent ซึ่งช่วยเพิ่มความสามารถในการขยายตัว การเข้าถึง และประสิทธิภาพในการสร้างระบบ AI
บทเรียนนี้จะครอบคลุม:
เป้าหมายของบทเรียนนี้คือช่วยให้คุณเข้าใจ:
กรอบการทำงาน AI แบบดั้งเดิมสามารถช่วยให้คุณผสานรวม AI เข้ากับแอปของคุณและทำให้แอปเหล่านี้ดีขึ้นในลักษณะดังนี้:
กรอบการทำงานของ AI Agent เป็นมากกว่ากรอบการทำงาน AI ทั่วไป พวกมันถูกออกแบบมาเพื่อสร้าง Agent ที่ชาญฉลาดที่สามารถโต้ตอบกับผู้ใช้ Agent อื่นๆ และสภาพแวดล้อมเพื่อบรรลุเป้าหมายเฉพาะ Agent เหล่านี้สามารถแสดงพฤติกรรมอัตโนมัติ ตัดสินใจ และปรับตัวให้เข้ากับสภาพแวดล้อมที่เปลี่ยนแปลงได้ มาดูความสามารถสำคัญที่กรอบการทำงานของ AI Agent มอบให้:
สรุปแล้ว Agent ช่วยให้คุณทำได้มากขึ้น ยกระดับการทำงานอัตโนมัติไปอีกขั้น และสร้างระบบที่ชาญฉลาดยิ่งขึ้นที่สามารถปรับตัวและเรียนรู้จากสภาพแวดล้อมของพวกเขา
นี่เป็นพื้นที่ที่เปลี่ยนแปลงอย่างรวดเร็ว แต่มีบางสิ่งที่เป็นเรื่องปกติในกรอบการทำงานของ AI Agent ส่วนใหญ่ที่สามารถช่วยให้คุณสร้างต้นแบบและปรับปรุงได้อย่างรวดเร็ว ได้แก่ ส่วนประกอบแบบโมดูล เครื่องมือการทำงานร่วมกัน และการเรียนรู้แบบเรียลไทม์ มาดูรายละเอียดกัน:
SDKs อย่าง Microsoft Semantic Kernel และ LangChain มีส่วนประกอบสำเร็จรูป เช่น ตัวเชื่อมต่อ AI แม่แบบคำสั่ง และการจัดการหน่วยความจำ
ทีมงานสามารถใช้สิ่งเหล่านี้ได้อย่างไร: ทีมงานสามารถประกอบส่วนประกอบเหล่านี้อย่างรวดเร็วเพื่อสร้างต้นแบบที่ใช้งานได้โดยไม่ต้องเริ่มจากศูนย์ ทำให้สามารถทดลองและปรับปรุงได้อย่างรวดเร็ว
การทำงานในทางปฏิบัติ: คุณสามารถใช้ตัวแยกวิเคราะห์สำเร็จรูปเพื่อดึงข้อมูลจากการป้อนข้อมูลของผู้ใช้ โมดูลหน่วยความจำเพื่อจัดเก็บและเรียกคืนข้อมูล และตัวสร้างคำสั่งเพื่อโต้ตอบกับผู้ใช้ โดยไม่ต้องสร้างส่วนประกอบเหล่านี้ตั้งแต่ต้น
ตัวอย่างโค้ด. มาดูตัวอย่างวิธีใช้ตัวเชื่อมต่อ AI สำเร็จรูปกับ Semantic Kernel Python และ .Net ที่ใช้การเรียกฟังก์ชันอัตโนมัติเพื่อให้โมเดลตอบสนองต่อการป้อนข้อมูลของผู้ใช้:
# Semantic Kernel Python Example
import asyncio
from typing import Annotated
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory
from semantic_kernel.functions import kernel_function
from semantic_kernel.kernel import Kernel
# Define a ChatHistory object to hold the conversation's context
chat_history = ChatHistory()
chat_history.add_user_message("I'd like to go to New York on January 1, 2025")
# Define a sample plugin that contains the function to book travel
class BookTravelPlugin:
"""A Sample Book Travel Plugin"""
@kernel_function(name="book_flight", description="Book travel given location and date")
async def book_flight(
self, date: Annotated[str, "The date of travel"], location: Annotated[str, "The location to travel to"]
) -> str:
return f"Travel was booked to {location} on {date}"
# Create the Kernel
kernel = Kernel()
# Add the sample plugin to the Kernel object
kernel.add_plugin(BookTravelPlugin(), plugin_name="book_travel")
# Define the Azure OpenAI AI Connector
chat_service = AzureChatCompletion(
deployment_name="YOUR_DEPLOYMENT_NAME",
api_key="YOUR_API_KEY",
endpoint="https://<your-resource>.azure.openai.com/",
)
# Define the request settings to configure the model with auto-function calling
request_settings = AzureChatPromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto())
async def main():
# Make the request to the model for the given chat history and request settings
# The Kernel contains the sample that the model will request to invoke
response = await chat_service.get_chat_message_content(
chat_history=chat_history, settings=request_settings, kernel=kernel
)
assert response is not None
"""
Note: In the auto function calling process, the model determines it can invoke the
`BookTravelPlugin` using the `book_flight` function, supplying the necessary arguments.
For example:
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "BookTravelPlugin-book_flight",
"arguments": "{'location': 'New York', 'date': '2025-01-01'}"
}
}
]
Since the location and date arguments are required (as defined by the kernel function), if the
model lacks either, it will prompt the user to provide them. For instance:
User: Book me a flight to New York.
Model: Sure, I'd love to help you book a flight. Could you please specify the date?
User: I want to travel on January 1, 2025.
Model: Your flight to New York on January 1, 2025, has been successfully booked. Safe travels!
"""
print(f"`{response}`")
# Example AI Model Response: `Your flight to New York on January 1, 2025, has been successfully booked. Safe travels! ✈️🗽`
# Add the model's response to our chat history context
chat_history.add_assistant_message(response.content)
if __name__ == "__main__":
asyncio.run(main())
// Semantic Kernel C# example
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using System.ComponentModel;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("I'd like to go to New York on January 1, 2025");
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddAzureOpenAIChatCompletion(
deploymentName: "NAME_OF_YOUR_DEPLOYMENT",
apiKey: "YOUR_API_KEY",
endpoint: "YOUR_AZURE_ENDPOINT"
);
kernelBuilder.Plugins.AddFromType<BookTravelPlugin>("BookTravel");
var kernel = kernelBuilder.Build();
var settings = new AzureOpenAIPromptExecutionSettings()
{
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
var chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
var response = await chatCompletion.GetChatMessageContentAsync(chatHistory, settings, kernel);
/*
Behind the scenes, the model recognizes the tool to call, what arguments it already has (location) and (date)
{
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "BookTravelPlugin-book_flight",
"arguments": "{'location': 'New York', 'date': '2025-01-01'}"
}
}
]
*/
Console.WriteLine(response.Content);
chatHistory.AddMessage(response!.Role, response!.Content!);
// Example AI Model Response: Your flight to New York on January 1, 2025, has been successfully booked. Safe travels! ✈️🗽
// Define a plugin that contains the function to book travel
public class BookTravelPlugin
{
[KernelFunction("book_flight")]
[Description("Book travel given location and date")]
public async Task<string> BookFlight(DateTime date, string location)
{
return await Task.FromResult( $"Travel was booked to {location} on {date}");
}
}
จากตัวอย่างนี้ คุณจะเห็นว่าคุณสามารถใช้ตัวแยกวิเคราะห์สำเร็จรูปเพื่อดึงข้อมูลสำคัญจากการป้อนข้อมูลของผู้ใช้ เช่น ต้นทาง ปลายทาง และวันที่ของคำขอจองเที่ยวบิน วิธีการแบบโมดูลนี้ช่วยให้คุณมุ่งเน้นไปที่ตรรกะระดับสูงได้
กรอบการทำงานอย่าง CrewAI, Microsoft AutoGen และ Semantic Kernel ช่วยให้สามารถสร้าง Agent หลายตัวที่สามารถทำงานร่วมกันได้
ทีมงานสามารถใช้สิ่งเหล่านี้ได้อย่างไร: ทีมงานสามารถออกแบบ Agent ที่มีบทบาทและงานเฉพาะ เพื่อให้สามารถทดสอบและปรับปรุงกระบวนการทำงานร่วมกัน และเพิ่มประสิทธิภาพของระบบโดยรวม
การทำงานในทางปฏิบัติ: คุณสามารถสร้างทีมของ Agent ที่แต่ละตัวมีฟังก์ชันเฉพาะ เช่น การดึงข้อมูล การวิเคราะห์ หรือการตัดสินใจ Agent เหล่านี้สามารถสื่อสารและแบ่งปันข้อมูลเพื่อบรรลุเป้าหมายร่วมกัน เช่น การตอบคำถามของผู้ใช้หรือการทำงานให้เสร็จสมบูรณ์
ตัวอย่างโค้ด (AutoGen):
# creating agents, then create a round robin schedule where they can work together, in this case in order
# Data Retrieval Agent
# Data Analysis Agent
# Decision Making Agent
agent_retrieve = AssistantAgent(
name="dataretrieval",
model_client=model_client,
tools=[retrieve_tool],
system_message="Use tools to solve tasks."
)
agent_analyze = AssistantAgent(
name="dataanalysis",
model_client=model_client,
tools=[analyze_tool],
system_message="Use tools to solve tasks."
)
# conversation ends when user says "APPROVE"
termination = TextMentionTermination("APPROVE")
user_proxy = UserProxyAgent("user_proxy", input_func=input)
team = RoundRobinGroupChat([agent_retrieve, agent_analyze, user_proxy], termination_condition=termination)
stream = team.run_stream(task="Analyze data", max_turns=10)
# Use asyncio.run(...) when running in a script.
await Console(stream)
จากโค้ดก่อนหน้านี้ คุณจะเห็นว่าคุณสามารถสร้างงานที่เกี่ยวข้องกับ Agent หลายตัวที่ทำงานร่วมกันเพื่อวิเคราะห์ข้อมูลได้อย่างไร แต่ละ Agent มีฟังก์ชันเฉพาะ และงานจะดำเนินการโดยการประสานงานของ Agent เพื่อให้ได้ผลลัพธ์ที่ต้องการ การสร้าง Agent ที่มีบทบาทเฉพาะช่วยเพิ่มประสิทธิภาพและประสิทธิผลของงาน
กรอบการทำงานขั้นสูงมอบความสามารถในการทำความเข้าใจบริบทและการปรับตัวแบบเรียลไทม์
ทีมงานสามารถใช้สิ่งเหล่านี้ได้อย่างไร: ทีมงานสามารถใช้ฟีดแบ็กลูปที่ Agent เรียนรู้จากการโต้ตอบและปรับพฤติกรรมของพวกเขาแบบไดนามิก ซึ่งนำไปสู่การปรับปรุงและพัฒนาความสามารถอย่างต่อเนื่อง
การทำงานในทางปฏิบัติ: Agent สามารถวิเคราะห์ฟีดแบ็กของผู้ใช้ ข้อมูลสภาพแวดล้อม และผลลัพธ์ของงานเพื่ออัปเดตฐานความรู้ของพวกเขา ปรับอัลกอริธึมการตัดสินใจ และปรับปรุงประสิทธิภาพเมื่อเวลาผ่านไป กระบวนการเรียนรู้แบบวนซ้ำนี้ช่วยให้ Agent ปรับตัวให้เข้ากับสภาพแวดล้อมที่เปลี่ยนแปลงและความชอบของผู้ใช้ได้ เพิ่มประสิทธิภาพของระบบโดยรวม
มีหลายวิธีในการเปรียบเทียบกรอบการทำงานเหล่านี้ แต่เรามาดูความแตกต่างสำคัญในแง่ของการออกแบบ ความสามารถ และกรณีการใช้งานเป้าหมาย:
AutoGen เป็นกรอบการทำงานโอเพ่นซอร์สที่พัฒนาโดย Microsoft Research’s AI Frontiers Lab มุ่งเน้นไปที่แอปพลิเคชันแบบกระจายที่ขับเคลื่อนด้วยเหตุการณ์ agentic ซึ่งช่วยให้สามารถใช้งาน LLMs และ SLMs หลายตัว เครื่องมือ และรูปแบบการออกแบบ Agent หลายตัวขั้นสูง
AutoGen ถูกสร้างขึ้นรอบแนวคิดหลักของ Agent ซึ่งเป็นเอนทิตีอัตโนมัติที่สามารถรับรู้สภาพแวดล้อมของพวกเขา ตัดสินใจ และดำเนินการเพื่อบรรลุเป้าหมายเฉพาะ Agent สื่อสารผ่านข้อความแบบอะซิงโครนัส ทำให้พวกเขาสามารถทำงานได้อย่างอิสระและขนานกัน เพิ่มความสามารถในการขยายตัวและการตอบสนองของระบบ
Agent อิงตามโมเดล Actor ตามที่ Wikipedia กล่าวไว้ Actor คือ หน่วยพื้นฐานของการคำนวณแบบขนาน ในการตอบสนองต่อข้อความที่ได้รับ Actor สามารถ: ตัดสินใจในท้องถิ่น สร้าง Actor เพิ่มเติม ส่งข้อความเพิ่มเติม และกำหนดวิธีการตอบสนองต่อข้อความถัดไปที่ได้รับ
กรณีการใช้งาน: การสร้างโค้ดอัตโนมัติ งานวิเคราะห์ข้อมูล และการสร้าง Agent แบบกำหนดเองสำหรับฟังก์ชันการวางแผนและการวิจัย
นี่คือตัวอย่างแนวคิดหลักของ AutoGen:
นี่คือตัวอย่างโค้ดสั้นๆ ที่คุณสร้าง Agent ของคุณเองที่มีความสามารถในการแชท:
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.messages import TextMessage
from autogen_ext.models.openai import OpenAIChatCompletionClient
class MyAgent(RoutedAgent):
def __init__(self, name: str) -> None:
super().__init__(name)
model_client = OpenAIChatCompletionClient(model="gpt-4o")
self._delegate = AssistantAgent(name, model_client=model_client)
@message_handler
async def handle_my_message_type(self, message: MyMessageType, ctx: MessageContext) -> None:
print(f"{self.id.type} received message: {message.content}")
response = await self._delegate.on_messages(
[TextMessage(content=message.content, source="user")], ctx.cancellation_token
)
print(f"{self.id.type} responded: {response.chat_message.content}")
ในโค้ดก่อนหน้านี้ MyAgent ถูกสร้างขึ้นและสืบทอดจาก RoutedAgent มันมีตัวจัดการข้อความที่พิมพ์เนื้อหาของข้อความและส่งการตอบกลับโดยใช้ตัวแทน AssistantAgent โดยเฉพาะอย่างยิ่งสังเกตว่าเรากำหนดให้ self._delegate เป็นอินสแตนซ์ของ AssistantAgent ซึ่งเป็น Agent สำเร็จรูปที่สามารถจัดการการแชทได้
มาทำให้ AutoGen รู้จักประเภท Agent นี้และเริ่มโปรแกรมกัน:
# main.py
runtime = SingleThreadedAgentRuntime()
await MyAgent.register(runtime, "my_agent", lambda: MyAgent())
runtime.start() # Start processing messages in the background.
await runtime.send_message(MyMessageType("Hello, World!"), AgentId("my_agent", "default"))
ในโค้ดก่อนหน้านี้ Agent ถูกลงทะเบียนกับ runtime และจากนั้นข้อความจะถูกส่งไปยัง Agent ส่งผลให้ได้ผลลัพธ์ดังนี้:
# Output from the console:
my_agent received message: Hello, World!
my_assistant received message: Hello, World!
my_assistant responded: Hello! How can I assist you today?
Multi agents. AutoGen รองรับการสร้าง Agent หลายตัวที่สามารถทำงานร่วมกันเพื่อบรรลุงานที่ซับซ้อน Agent สามารถสื่อสาร แบ่งปันข้อมูล และประสานการกระทำของพวกเขาเพื่อแก้ปัญหาได้อย่างมีประสิทธิภาพมากขึ้น ในการสร้างระบบหลาย Agent คุณสามารถกำหนดประเภท Agent ที่แตกต่างกันที่มีฟังก์ชันและบทบาทเฉพาะ เช่น การดึงข้อมูล การวิเคราะห์ การตัดสินใจ และการโต้ตอบกับผู้ใช้ มาดูตัวอย่างการสร้างดังกล่าวเพื่อให้เราเข้าใจ:
editor_description = "Editor for planning and reviewing the content."
# Example of declaring an Agent
editor_agent_type = await EditorAgent.register(
runtime,
editor_topic_type, # Using topic type as the agent type.
lambda: EditorAgent(
description=editor_description,
group_chat_topic_type=group_chat_topic_type,
model_client=OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06",
# api_key="YOUR_API_KEY",
),
),
)
# remaining declarations shortened for brevity
# Group chat
group_chat_manager_type = await GroupChatManager.register(
runtime,
"group_chat_manager",
lambda: GroupChatManager(
participant_topic_types=[writer_topic_type, illustrator_topic_type, editor_topic_type, user_topic_type],
model_client=OpenAIChatCompletionClient(
model="gpt-4o-2024-08-06",
# api_key="YOUR_API_KEY",
),
participant_descriptions=[
writer_description,
illustrator_description,
editor_description,
user_description
],
),
)
ในโค้ดก่อนหน้านี้ เรามี GroupChatManager ที่ลงทะเบียนกับ runtime ผู้จัดการนี้รับผิดชอบในการประสานการโต้ตอบระหว่างประเภท Agent ที่แตกต่างกัน เช่น นักเขียน นักวาดภาพ บรรณาธิการ และผู้ใช้
Stand-alone runtime. นี่เป็นตัวเลือกที่ดีสำหรับแอปพลิเคชันที่ทำงานในกระบวนการเดียวที่ Agent ทั้งหมดถูกนำมาใช้ในภาษาโปรแกรมเดียวกันและทำงานในกระบวนการเดียวกัน นี่คือตัวอย่างการทำงาน:
Stand-alone runtime
Application stack
Agent สื่อสารผ่านข้อความผ่าน runtime และ runtime จัดการวงจรชีวิตของ Agent
Distributed agent runtime, เหมาะสำหรับแอปพลิเคชันหลายกระบวนการที่ Agent อาจถูกนำมาใช้ในภาษาการเขียนโปรแกรมที่แตกต่างกันและทำงานบนเครื่องที่แตกต่างกัน นี่คือตัวอย่างการทำงาน:
Semantic Kernel เป็น AI Orchestration SDK ที่พร้อมสำหรับองค์กร ประกอบด้วยตัวเชื่อมต่อ AI และ Memory พร้อมกับ Agent Framework
มาดูส่วนประกอบหลักกันก่อน:
AI Connectors: นี่คือตัวเชื่อมต่อกับบริการ AI ภายนอกและแหล่งข้อมูลสำหรับใช้งานใน Python และ C#
# Semantic Kernel Python
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.kernel import Kernel
kernel = Kernel()
kernel.add_service(
AzureChatCompletion(
deployment_name="your-deployment-name",
api_key="your-api-key",
endpoint="your-endpoint",
)
)
// Semantic Kernel C#
using Microsoft.SemanticKernel;
// Create kernel
var builder = Kernel.CreateBuilder();
// Add a chat completion service:
builder.Services.AddAzureOpenAIChatCompletion(
"your-resource-name",
"your-endpoint",
"your-resource-key",
"deployment-model");
var kernel = builder.Build();
นี่คือตัวอย่างง่ายๆ ของวิธีที่คุณสามารถสร้าง Kernel และ
ข้อเท็จจริงเหล่านี้จะถูกจัดเก็บไว้ในคอลเลกชันหน่วยความจำ SummarizedAzureDocs นี่เป็นตัวอย่างที่ง่ายมาก แต่คุณสามารถเห็นได้ว่าคุณสามารถจัดเก็บข้อมูลในหน่วยความจำเพื่อให้ LLM ใช้งานได้อย่างไร
นี่คือพื้นฐานของเฟรมเวิร์ก Semantic Kernel แล้ว Agent Framework ล่ะ?
Azure AI Agent Service เป็นส่วนเสริมใหม่ที่เปิดตัวในงาน Microsoft Ignite 2024 ซึ่งช่วยให้สามารถพัฒนาและปรับใช้ AI agents ด้วยโมเดลที่ยืดหยุ่นมากขึ้น เช่น การเรียกใช้งาน LLMs แบบโอเพ่นซอร์สโดยตรง เช่น Llama 3, Mistral และ Cohere
Azure AI Agent Service มีระบบรักษาความปลอดภัยระดับองค์กรที่แข็งแกร่งและวิธีการจัดเก็บข้อมูลที่เหมาะสำหรับการใช้งานในองค์กร
มันทำงานได้ทันทีร่วมกับเฟรมเวิร์กการจัดการหลายเอเจนต์ เช่น AutoGen และ Semantic Kernel
บริการนี้อยู่ในช่วง Public Preview และรองรับ Python และ C# สำหรับการสร้างเอเจนต์
โดยใช้ Semantic Kernel Python เราสามารถสร้าง Azure AI Agent พร้อมปลั๊กอินที่ผู้ใช้กำหนดเองได้:
import asyncio
from typing import Annotated
from azure.identity.aio import DefaultAzureCredential
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings, AzureAIAgentThread
from semantic_kernel.contents import ChatMessageContent
from semantic_kernel.contents import AuthorRole
from semantic_kernel.functions import kernel_function
# Define a sample plugin for the sample
class MenuPlugin:
"""A sample Menu Plugin used for the concept sample."""
@kernel_function(description="Provides a list of specials from the menu.")
def get_specials(self) -> Annotated[str, "Returns the specials from the menu."]:
return """
Special Soup: Clam Chowder
Special Salad: Cobb Salad
Special Drink: Chai Tea
"""
@kernel_function(description="Provides the price of the requested menu item.")
def get_item_price(
self, menu_item: Annotated[str, "The name of the menu item."]
) -> Annotated[str, "Returns the price of the menu item."]:
return "$9.99"
async def main() -> None:
ai_agent_settings = AzureAIAgentSettings.create()
async with (
DefaultAzureCredential() as creds,
AzureAIAgent.create_client(
credential=creds,
conn_str=ai_agent_settings.project_connection_string.get_secret_value(),
) as client,
):
# Create agent definition
agent_definition = await client.agents.create_agent(
model=ai_agent_settings.model_deployment_name,
name="Host",
instructions="Answer questions about the menu.",
)
# Create the AzureAI Agent using the defined client and agent definition
agent = AzureAIAgent(
client=client,
definition=agent_definition,
plugins=[MenuPlugin()],
)
# Create a thread to hold the conversation
# If no thread is provided, a new thread will be
# created and returned with the initial response
thread: AzureAIAgentThread | None = None
user_inputs = [
"Hello",
"What is the special soup?",
"How much does that cost?",
"Thank you",
]
try:
for user_input in user_inputs:
print(f"# User: '{user_input}'")
# Invoke the agent for the specified thread
response = await agent.get_response(
messages=user_input,
thread_id=thread,
)
print(f"# {response.name}: {response.content}")
thread = response.thread
finally:
await thread.delete() if thread else None
await client.agents.delete_agent(agent.id)
if __name__ == "__main__":
asyncio.run(main())
Azure AI Agent Service มีแนวคิดหลักดังนี้:
Agent. Azure AI Agent Service ผสานรวมกับ Azure AI Foundry ภายใน AI Foundry, AI Agent ทำหน้าที่เป็นไมโครเซอร์วิส “อัจฉริยะ” ที่สามารถใช้ตอบคำถาม (RAG), ดำเนินการ หรือทำงานอัตโนมัติได้อย่างสมบูรณ์ โดยใช้พลังของโมเดล AI เชิงสร้างสรรค์ร่วมกับเครื่องมือที่ช่วยให้สามารถเข้าถึงและโต้ตอบกับแหล่งข้อมูลในโลกจริงได้ ตัวอย่างของเอเจนต์มีดังนี้:
agent = project_client.agents.create_agent(
model="gpt-4o-mini",
name="my-agent",
instructions="You are helpful agent",
tools=code_interpreter.definitions,
tool_resources=code_interpreter.resources,
)
ในตัวอย่างนี้ เอเจนต์ถูกสร้างขึ้นด้วยโมเดล gpt-4o-mini, ชื่อ my-agent, และคำแนะนำ You are helpful agent เอเจนต์นี้มีเครื่องมือและทรัพยากรสำหรับการทำงานตีความโค้ด
Thread และข้อความ. Thread เป็นอีกหนึ่งแนวคิดสำคัญ มันแสดงถึงการสนทนาหรือการโต้ตอบระหว่างเอเจนต์และผู้ใช้ Threads สามารถใช้ติดตามความคืบหน้าของการสนทนา เก็บข้อมูลบริบท และจัดการสถานะของการโต้ตอบ ตัวอย่างของ Thread มีดังนี้:
thread = project_client.agents.create_thread()
message = project_client.agents.create_message(
thread_id=thread.id,
role="user",
content="Could you please create a bar chart for the operating profit using the following data and provide the file to me? Company A: $1.2 million, Company B: $2.5 million, Company C: $3.0 million, Company D: $1.8 million",
)
# Ask the agent to perform work on the thread
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
# Fetch and log all messages to see the agent's response
messages = project_client.agents.list_messages(thread_id=thread.id)
print(f"Messages: {messages}")
ในโค้ดก่อนหน้านี้ Thread ถูกสร้างขึ้น หลังจากนั้นข้อความถูกส่งไปยัง Thread โดยการเรียก create_and_process_run เอเจนต์ถูกขอให้ทำงานใน Thread สุดท้ายข้อความถูกดึงและบันทึกเพื่อดูการตอบสนองของเอเจนต์ ข้อความแสดงถึงความคืบหน้าของการสนทนาระหว่างผู้ใช้และเอเจนต์ นอกจากนี้ยังสำคัญที่ต้องเข้าใจว่าข้อความสามารถมีหลายประเภท เช่น ข้อความ, รูปภาพ หรือไฟล์ ซึ่งเป็นผลลัพธ์ของงานที่เอเจนต์ทำ เช่น การตอบกลับเป็นข้อความหรือรูปภาพ ตัวอย่างเช่น ในฐานะนักพัฒนา คุณสามารถใช้ข้อมูลนี้เพื่อประมวลผลเพิ่มเติมหรือแสดงผลให้ผู้ใช้
ผสานรวมกับเฟรมเวิร์ก AI อื่นๆ. Azure AI Agent Service สามารถโต้ตอบกับเฟรมเวิร์กอื่นๆ เช่น AutoGen และ Semantic Kernel ซึ่งหมายความว่าคุณสามารถสร้างส่วนหนึ่งของแอปในหนึ่งในเฟรมเวิร์กเหล่านี้ และใช้ Agent Service เป็นตัวจัดการ หรือคุณสามารถสร้างทุกอย่างใน Agent Service ได้
กรณีการใช้งาน: Azure AI Agent Service ถูกออกแบบมาสำหรับการใช้งานในองค์กรที่ต้องการการปรับใช้ AI agent ที่ปลอดภัย, ขยายได้ และยืดหยุ่น
ดูเหมือนว่าเฟรมเวิร์กเหล่านี้จะมีความคล้ายคลึงกันมาก แต่มีความแตกต่างสำคัญในแง่ของการออกแบบ, ความสามารถ และกรณีการใช้งานเป้าหมาย:
ยังไม่แน่ใจว่าจะเลือกอะไร?
ลองดูว่ากรณีการใช้งานทั่วไปสามารถช่วยคุณได้หรือไม่:
Q: ฉันกำลังทดลอง, เรียนรู้ และสร้างแอปพลิเคชันเอเจนต์ต้นแบบ และฉันต้องการสร้างและทดลองได้อย่างรวดเร็ว
A: AutoGen จะเป็นตัวเลือกที่ดีสำหรับสถานการณ์นี้ เนื่องจากมุ่งเน้นไปที่แอปพลิเคชันเอเจนต์แบบกระจายที่ขับเคลื่อนด้วยเหตุการณ์ และรองรับรูปแบบการออกแบบเอเจนต์หลายตัวขั้นสูง
Q: อะไรทำให้ AutoGen เป็นตัวเลือกที่ดีกว่า Semantic Kernel และ Azure AI Agent Service สำหรับกรณีการใช้งานนี้?
A: AutoGen ถูกออกแบบมาโดยเฉพาะสำหรับแอปพลิเคชันเอเจนต์แบบกระจายที่ขับเคลื่อนด้วยเหตุการณ์ ทำให้เหมาะสำหรับการทำงานอัตโนมัติในงานสร้างโค้ดและวิเคราะห์ข้อมูล มันมีเครื่องมือและความสามารถที่จำเป็นในการสร้างระบบหลายเอเจนต์ที่ซับซ้อนอย่างมีประสิทธิภาพ
Q: ฟังดูเหมือน Azure AI Agent Service ก็สามารถใช้งานได้ที่นี่เช่นกัน มันมีเครื่องมือสำหรับการสร้างโค้ดและอื่นๆ ใช่ไหม?
A: ใช่ Azure AI Agent Service เป็นบริการแพลตฟอร์มสำหรับเอเจนต์และมีความสามารถในตัวสำหรับโมเดลหลายตัว, Azure AI Search, Bing Search และ Azure Functions มันทำให้การสร้างเอเจนต์ใน Foundry Portal และการปรับใช้ในระดับใหญ่เป็นเรื่องง่าย
Q: ฉันยังสับสนอยู่ แค่บอกตัวเลือกเดียวให้ฉัน
A: ตัวเลือกที่ดีคือการสร้างแอปพลิเคชันของคุณใน Semantic Kernel ก่อน แล้วใช้ Azure AI Agent Service เพื่อปรับใช้เอเจนต์ของคุณ วิธีนี้ช่วยให้คุณสามารถจัดเก็บเอเจนต์ของคุณได้ง่ายในขณะที่ใช้พลังของการสร้างระบบหลายเอเจนต์ใน Semantic Kernel นอกจากนี้ Semantic Kernel ยังมีตัวเชื่อมต่อใน AutoGen ทำให้การใช้เฟรมเวิร์กทั้งสองร่วมกันเป็นเรื่องง่าย
สรุปความแตกต่างสำคัญในตาราง:
| เฟรมเวิร์ก | จุดเน้น | แนวคิดหลัก | กรณีการใช้งาน |
|---|---|---|---|
| AutoGen | แอปพลิเคชันเอเจนต์แบบกระจายที่ขับเคลื่อนด้วยเหตุการณ์ | Agents, Personas, Functions, Data | งานสร้างโค้ด, การวิเคราะห์ข้อมูล |
| Semantic Kernel | การทำความเข้าใจและสร้างเนื้อหาที่เหมือนมนุษย์ | Agents, Modular Components, Collaboration | การทำความเข้าใจภาษา, การสร้างเนื้อหา |
| Azure AI Agent Service | โมเดลที่ยืดหยุ่น, ความปลอดภัยระดับองค์กร, การสร้างโค้ด, การเรียกใช้เครื่องมือ | Modularity, Collaboration, Process Orchestration | การปรับใช้ AI agent ที่ปลอดภัย, ขยายได้ และยืดหยุ่น |
กรณีการใช้งานที่เหมาะสมที่สุดสำหรับแต่ละเฟรมเวิร์กคืออะไร?
คำตอบคือใช่ คุณสามารถผสานรวมเครื่องมือในระบบ Azure ที่มีอยู่ของคุณโดยตรงกับ Azure AI Agent Service โดยเฉพาะ เนื่องจากมันถูกสร้างมาให้ทำงานร่วมกับบริการ Azure อื่นๆ ได้อย่างราบรื่น ตัวอย่างเช่น คุณสามารถผสานรวม Bing, Azure AI Search และ Azure Functions นอกจากนี้ยังมีการผสานรวมลึกกับ Azure AI Foundry
สำหรับ AutoGen และ Semantic Kernel คุณก็สามารถผสานรวมกับบริการ Azure ได้เช่นกัน แต่คุณอาจต้องเรียกใช้บริการ Azure จากโค้ดของคุณ อีกวิธีหนึ่งในการผสานรวมคือการใช้ Azure SDKs เพื่อโต้ตอบกับบริการ Azure จากเอเจนต์ของคุณ นอกจากนี้ อย่างที่กล่าวไว้ คุณสามารถใช้ Azure AI Agent Service เป็นตัวจัดการสำหรับเอเจนต์ที่สร้างใน AutoGen หรือ Semantic Kernel ซึ่งจะช่วยให้เข้าถึงระบบ Azure ได้ง่าย
เข้าร่วม Azure AI Foundry Discord เพื่อพบปะกับผู้เรียนคนอื่นๆ เข้าร่วมช่วง Office Hours และรับคำตอบสำหรับคำถามเกี่ยวกับ AI Agents ของคุณ
Introduction to AI Agents and Agent Use Cases
Understanding Agentic Design Patterns
ข้อจำกัดความรับผิดชอบ:
เอกสารนี้ได้รับการแปลโดยใช้บริการแปลภาษา AI Co-op Translator แม้ว่าเราจะพยายามให้การแปลมีความถูกต้อง แต่โปรดทราบว่าการแปลอัตโนมัติอาจมีข้อผิดพลาดหรือความไม่ถูกต้อง เอกสารต้นฉบับในภาษาต้นทางควรถือเป็นแหล่งข้อมูลที่เชื่อถือได้ สำหรับข้อมูลที่สำคัญ แนะนำให้ใช้บริการแปลภาษามนุษย์ที่เป็นมืออาชีพ เราไม่รับผิดชอบต่อความเข้าใจผิดหรือการตีความผิดที่เกิดจากการใช้การแปลนี้