共用方式為


探索語意核心 AzureAIAgent

這很重要

這項功能處於實驗階段。 在這個階段的功能正在開發中,在前進到預覽或發行候選階段之前,可能會變更。

小提示

如需此討論的詳細 API 檔,請參閱:

小提示

如需此討論的詳細 API 檔,請參閱:

Java 中目前無法使用的功能。

什麼是 AzureAIAgent

AzureAIAgent 是語意核心架構內的特製化代理程式,其設計目的是提供具有無縫工具整合的進階交談功能。 它能自動進行工具調用,省去手動解析和啟動的需求。 代理程式也會使用線程安全地管理交談歷程記錄,減少維護狀態的額外負荷。 此外,AzureAIAgent 支援各種不同的內建工具,包括透過 Bing、Azure AI 搜尋、Azure Functions 和 OpenAPI 進行檔案擷取、程式代碼執行和數據互動。

若要使用 AzureAIAgent,必須使用 Azure AI Foundry 專案。 下列文章提供 Azure AI Foundry 的概觀、如何建立和設定專案,以及代理程式服務:

準備您的開發環境

若要繼續開發 AzureAIAgent,請使用適當的套件來設定您的開發環境。

Microsoft.SemanticKernel.Agents.AzureAI 套件新增至您的專案:

dotnet add package Microsoft.SemanticKernel.Agents.AzureAI --prerelease

您可能也想要包含 Azure.Identity 套件:

dotnet add package Azure.Identity

安裝 semantic-kernel 套件:

pip install semantic-kernel

Java 中目前無法使用的功能。

設定 AI 專案用戶端

在存取 AzureAIAgent 之前必須先建立為特定 Foundry 專案設定的客戶端,通常是透過提供專案端點(Azure AI Foundry SDK:開始使用專案)。

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

修改您在根目錄中的 .env 檔案,以包含:

AZURE_AI_AGENT_ENDPOINT = "<example-endpoint>"
AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME = "<example-model-deployment-name>"

定義組態之後,就可以建立用戶端:

from semantic_kernel.agents import AzureAIAgent

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds) as client,
):
    # Your operational code here

如果已設定,Pydantic Settings 將會使用endpoint的相關設置。 否則,您可以將它明確地傳入create_client()方法:

from semantic_kernel.agents import AzureAIAgent

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds, endpoint="<your-endpoint>") as client,
):
    # Your operational code here

Java 中目前無法使用的功能。

建立新的AzureAIAgent

若要建立 AzureAIAgent,您可以從透過 Azure Agent 服務設定和初始化 Foundry 專案開始,然後將其與語意核心整合:

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

// 1. Define an agent on the Azure AI agent service
PersistentAgent definition = await agentsClient.Administration.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>");

// 2. Create a Semantic Kernel agent based on the agent definition
AzureAIAgent agent = new(definition, agentsClient);
from azure.identity.aio import DefaultAzureCredential
from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds) as client,
):
    # 1. Define an agent on the Azure AI agent service
    agent_definition = await client.agents.create_agent(
        model=AzureAIAgentSettings().model_deployment_name,
        name="<name>",
        instructions="<instructions>",
    )

    # 2. Create a Semantic Kernel agent based on the agent definition
    agent = AzureAIAgent(
        client=client,
        definition=agent_definition,
    )

Java 中目前無法使用的功能。

AzureAIAgent 進行互動

AzureAIAgent 的互動很簡單。 代理程式會使用線程自動維護交談歷程記錄。

Azure AI 代理程式線程的詳細數據會透過一個Microsoft.SemanticKernel.Agents.AzureAI.AzureAIAgentThread類別來抽象化,這是一個Microsoft.SemanticKernel.Agents.AgentThread的實作。

這很重要

請注意,Azure AI 代理程式 SDK 具有 類別 PersistentAgentThread 。 它不應該與 Microsoft.SemanticKernel.Agents.AgentThread混淆,這是所有線程類型的常見語意核心代理程式抽象概念。

AzureAIAgent目前只支援 類型AzureAIAgentThread為的線程。

AzureAIAgentThread agentThread = new(agent.Client);
try
{
    ChatMessageContent message = new(AuthorRole.User, "<your user input>");
    await foreach (ChatMessageContent response in agent.InvokeAsync(message, agentThread))
    {
        Console.WriteLine(response.Content);
    }
}
finally
{
    await agentThread.DeleteAsync();
    await agent.Client.DeleteAgentAsync(agent.Id);
}

Azure AI 代理程式線程的詳細數據會透過一個AzureAIAgentThread類別來抽象化,這是一個AgentThread的實作。

USER_INPUTS = ["Hello", "What's your name?"]

thread: AzureAIAgentThread = AzureAIAgentThread()

try:
    for user_input in USER_INPUTS:
        response = await agent.get_response(messages=user_inputs, thread=thread)
        print(response)
        thread = response.thread
finally:
    await thread.delete() if thread else None

選擇性地,代理程式可以叫用為:

for user_input in USER_INPUTS:
    async for content in agent.invoke(messages=user_input, thread=thread):
        print(content.content)
        thread = response.thread

您也可以將訊息清單傳遞至 get_response(...)invoke(...)invoke_stream(...) 方法:

USER_INPUTS = ["Hello", "What's your name?"]

thread: AzureAIAgentThread = AzureAIAgentThread()

try:
    for user_input in USER_INPUTS:
        response = await agent.get_response(messages=USER_INPUTS, thread=thread)
        print(response)
        thread = response.thread
finally:
    await thread.delete() if thread else None

代理也可能產生串流回應:

ChatMessageContent message = new(AuthorRole.User, "<your user input>");
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(message, agentThread))
{
    Console.Write(response.Content);
}
for user_input in USER_INPUTS:
    await agent.add_chat_message(thread_id=thread.id, message=user_input)
    async for content in agent.invoke_stream(thread_id=thread.id):
        print(content.content, end="", flush=True)

Java 中目前無法使用的功能。

AzureAIAgent 上使用外掛程式

Semantic Kernel 支援使用自定義外掛程式擴充 AzureAIAgent,以提升功能:

KernelPlugin plugin = KernelPluginFactory.CreateFromType<YourPlugin>();
PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

PersistentAgent definition = await agentsClient.Administration.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>");

AzureAIAgent agent = new(definition, agentsClient, plugins: [plugin]);
from semantic_kernel.functions import kernel_function

class SamplePlugin:
    @kernel_function(description="Provides sample data.")
    def get_data(self) -> str:
        return "Sample data"

async with (
        DefaultAzureCredential() as creds,
        AzureAIAgent.create_client(credential=creds) as client,
    ):
        agent_definition = await client.agents.create_agent(
            model=AzureAIAgentSettings().model_deployment_name,
        )

        agent = AzureAIAgent(
            client=client,
            definition=agent_definition,
            plugins=[SamplePlugin()]
        )

Java 中目前無法使用的功能。

進階功能

AzureAIAgent 可以運用進階工具,例如:

程式碼解譯器

程式代碼解釋器可讓代理程式在沙盒化執行環境中撰寫和執行 Python 程式代碼(Azure AI 代理程式服務程式代碼解釋器)。

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

PersistentAgent definition = await agentsClient.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>",
    tools: [new CodeInterpreterToolDefinition()],
    toolResources:
        new()
        {
            CodeInterpreter = new()
            {
                FileIds = { ... },
            }
        }));

AzureAIAgent agent = new(definition, agentsClient);
from azure.ai.agents.models import CodeInterpreterTool

async with (
        DefaultAzureCredential() as creds,
        AzureAIAgent.create_client(credential=creds) as client,
    ):
        code_interpreter = CodeInterpreterTool()
        agent_definition = await client.agents.create_agent(
            model=ai_agent_settings.model_deployment_name,
            tools=code_interpreter.definitions,
            tool_resources=code_interpreter.resources,
        )

Java 中目前無法使用的功能。

檔案搜尋會增強代理程式,使他們能夠利用從其模型以外的知識(Azure AI 代理程式服務檔案搜尋工具)。

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

PersistentAgent definition = await agentsClient.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>",
    tools: [new FileSearchToolDefinition()],
    toolResources:
        new()
        {
            FileSearch = new()
            {
                VectorStoreIds = { ... },
            }
        });

AzureAIAgent agent = new(definition, agentsClient);
from azure.ai.agents.models import FileSearchTool

async with (
        DefaultAzureCredential() as creds,
        AzureAIAgent.create_client(credential=creds) as client,
    ):
        file_search = FileSearchTool(vector_store_ids=[vector_store.id])
        agent_definition = await client.agents.create_agent(
            model=ai_agent_settings.model_deployment_name,
            tools=file_search.definitions,
            tool_resources=file_search.resources,
        )

Java 中目前無法使用的功能。

OpenAPI 集成

將代理程式連線到外部 API(如何使用 Azure AI 代理程式服務搭配 OpenAPI 指定工具)。

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

string apiJsonSpecification = ...; // An Open API JSON specification

PersistentAgent definition = await agentsClient.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>",
    tools: [
        new OpenApiToolDefinition(
            "<api name>", 
            "<api description>", 
            BinaryData.FromString(apiJsonSpecification), 
            new OpenApiAnonymousAuthDetails())
    ]
);

AzureAIAgent agent = new(definition, agentsClient);
from azure.ai.agents.models import OpenApiTool, OpenApiAnonymousAuthDetails

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds) as client,
):
    openapi_spec_file_path = "sample/filepath/..."
    with open(os.path.join(openapi_spec_file_path, "spec_one.json")) as file_one:
        openapi_spec_one = json.loads(file_one.read())
    with open(os.path.join(openapi_spec_file_path, "spec_two.json")) as file_two:
        openapi_spec_two = json.loads(file_two.read())

    # Note that connection or managed identity auth setup requires additional setup in Azure
    auth = OpenApiAnonymousAuthDetails()
    openapi_tool_one = OpenApiTool(
        name="<name>",
        spec=openapi_spec_one,
        description="<description>",
        auth=auth,
    )
    openapi_tool_two = OpenApiTool(
        name="<name>",
        spec=openapi_spec_two,
        description="<description>",
        auth=auth,
    )

    agent_definition = await client.agents.create_agent(
        model=ai_agent_settings.model_deployment_name,
        tools=openapi_tool_one.definitions + openapi_tool_two.definitions,
    )

Java 中目前無法使用的功能。

AzureAI 搜尋整合

搭配您的代理程式使用現有的 Azure AI 搜尋索引 (使用現有的 AI 搜尋索引)。

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());

PersistentAgent definition = await agentsClient.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "<agent instructions>",
    tools: [new AzureAISearchToolDefinition()],
    toolResources: new()
    {
        AzureAISearch = new()
        {
            IndexList = { new AISearchIndexResource("<your connection id>", "<your index name>") }
        }
    });

AzureAIAgent agent = new(definition, agentsClient);
from azure.ai.agents.models import AzureAISearchTool, ConnectionType

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds) as client,
):
    conn_list = await client.connections.list()

    ai_search_conn_id = ""
    for conn in conn_list:
        if conn.connection_type == ConnectionType.AZURE_AI_SEARCH:
            ai_search_conn_id = conn.id
            break

    ai_search = AzureAISearchTool(
        index_connection_id=ai_search_conn_id, 
        index_name=AZURE_AI_SEARCH_INDEX_NAME,
    )

    agent_definition = await client.agents.create_agent(
        model=ai_agent_settings.model_deployment_name,
        instructions="Answer questions using your index.",
        tools=ai_search.definitions,
        tool_resources=ai_search.resources,
        headers={"x-ms-enable-preview": "true"},
    )

Java 中目前無法使用的功能。

Bing 綜合基礎

示例即將推出。

from azure.ai.agents.models import BingGroundingTool
from azure.identity.aio import DefaultAzureCredential

from semantic_kernel.agents import AzureAIAgent, AzureAIAgentSettings

async with (
    DefaultAzureCredential() as creds,
    AzureAIAgent.create_client(credential=creds) as client,
):
    # 1. Enter your Bing Grounding Connection Name
    bing_connection = await client.connections.get(connection_name="<your-bing-grounding-connection-name>")
    conn_id = bing_connection.id

    # 2. Initialize agent bing tool and add the connection id
    bing_grounding = BingGroundingTool(connection_id=conn_id)

    # 3. Create an agent with Bing grounding on the Azure AI agent service
    agent_definition = await client.agents.create_agent(
        name="BingGroundingAgent",
        instructions="Use the Bing grounding tool to answer the user's question.",
        model=AzureAIAgentSettings().model_deployment_name,
        tools=bing_grounding.definitions,
    )

    # 4. Create a Semantic Kernel agent for the Azure AI agent
    agent = AzureAIAgent(
        client=client,
        definition=agent_definition,
    )

使用 Bing Grounding 工具時, FunctionCallContent 被傳遞至回調函式的 on_intermediate_message 會將其函式名稱設定為 "bing_grounding"。 執行完成之後, ChatMessageContent.items 清單將會包含 AnnotationContentStreamingAnnotationContent,視調用為標準或串流而定。 這些標註項目包含代理在回應過程中所訪問的連結資訊,類似於FunctionCallContent中出現的資訊。

如需詳細資訊,請參閱下列概念範例:

Java 中目前無法使用的功能。

擷取現有的 AzureAIAgent

您可以藉由指定其助理識別碼來擷取及重複使用現有的代理程式:

PersistentAgent definition = await agentsClient.Administration.GetAgentAsync("<your agent id>");
AzureAIAgent agent = new(definition, agentsClient);
agent_definition = await client.agents.get_agent(assistant_id="your-agent-id")
agent = AzureAIAgent(client=client, definition=agent_definition)

Java 中目前無法使用的功能。

刪除 AzureAIAgent

不再需要代理程式及其相關聯的線程時,可以刪除:

await agentThread.DeleteAsync();
await agentsClient.Administration.DeleteAgentAsync(agent.Id);
await client.agents.delete_thread(thread.id)
await client.agents.delete_agent(agent.id)

如果使用向量存放區或檔案,它們可能也會被刪除:

await agentsClient.VectorStores.DeleteVectorStoreAsync("<your store id>");
await agentsClient.Files.DeleteFileAsync("<your file id>");
await client.agents.files.delete(file_id=file.id)
await client.agents.vector_stores.delete(vector_store_id=vector_store.id)

Java 中目前無法使用的功能。

如需 檔案搜尋 工具的詳細資訊,請參閱 azure AI 代理程式服務檔案搜尋工具 一文。

操作指南

如需使用 AzureAIAgent的實際範例,請參閱 GitHub 上的程式碼範例:

Java 中目前無法使用的功能。

處理包含AzureAIAgent的中繼訊息

Semantic Kernel AzureAIAgent 的設計目的是叫用可滿足使用者查詢或問題的代理程式。 在叫用期間,代理程式可能會執行工具來衍生最終答案。 若要存取此過程中產生的中繼訊息,呼叫者可以提供一個處理FunctionCallContentFunctionResultContent實例的回呼函式。

AzureAIAgent 的回呼文件即將推出。

on_intermediate_messageagent.invoke(...) 內設定 agent.invoke_stream(...) 回呼,可以讓呼叫方在制定代理人的最終回應過程中接收到產生的中間訊息。

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 FunctionCallContent, FunctionResultContent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
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"


# This callback function will be called for each intermediate message,
# which will allow one to handle FunctionCallContent and FunctionResultContent.
# If the callback is not provided, the agent will return the final response
# with no intermediate tool call steps.
async def handle_intermediate_steps(message: ChatMessageContent) -> None:
    for item in message.items or []:
        if isinstance(item, FunctionResultContent):
            print(f"Function Result:> {item.result} for function: {item.name}")
        elif isinstance(item, FunctionCallContent):
            print(f"Function Call:> {item.name} with arguments: {item.arguments}")
        else:
            print(f"{item}")


async def main() -> None:
    ai_agent_settings = AzureAIAgentSettings()

    async with (
        DefaultAzureCredential() as creds,
        AzureAIAgent.create_client(credential=creds, endpoint=ai_agent_settings.endpoint) as client,
    ):
        AGENT_NAME = "Host"
        AGENT_INSTRUCTIONS = "Answer questions about the menu."

        # Create agent definition
        agent_definition = await client.agents.create_agent(
            model=ai_agent_settings.deployment_name,
            name=AGENT_NAME,
            instructions=AGENT_INSTRUCTIONS,
        )

        # Create the AzureAI Agent
        agent = AzureAIAgent(
            client=client,
            definition=agent_definition,
            plugins=[MenuPlugin()],  # add the sample plugin to the agent
        )

        # Create a thread for the agent
        # If no thread is provided, a new thread will be
        # created and returned with the initial response
        thread: AzureAIAgentThread = 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}'")
                async for response in agent.invoke(
                    messages=user_input,
                    thread=thread,
                    on_intermediate_message=handle_intermediate_steps,
                ):
                    print(f"# Agent: {response}")
                    thread = response.thread
        finally:
            # Cleanup: Delete the thread and agent
            await thread.delete() if thread else None
            await client.agents.delete_agent(agent.id)


if __name__ == "__main__":
    asyncio.run(main())

下列示範來自代理程式調用程式的範例輸出:

User: 'Hello'
Agent: Hi there! How can I assist you today?
User: 'What is the special soup?'
Function Call:> MenuPlugin-get_specials with arguments: {}
Function Result:> 
        Special Soup: Clam Chowder
        Special Salad: Cobb Salad
        Special Drink: Chai Tea
        for function: MenuPlugin-get_specials
Agent: The special soup is Clam Chowder. Would you like to know anything else about the menu?
User: 'How much does that cost?'
Function Call:> MenuPlugin-get_item_price with arguments: {"menu_item":"Clam Chowder"}
Function Result:> $9.99 for function: MenuPlugin-get_item_price
Agent: The Clam Chowder costs $9.99. Let me know if you would like assistance with anything else!
User: 'Thank you'
Agent: You're welcome! Enjoy your meal! 😊

Java 中目前無法使用的功能。

宣告式規格

即將推出的使用宣告式規格的文件。

這很重要

這項功能處於實驗階段。 在這個階段的功能正在開發中,在前進到預覽或發行候選階段之前,可能會變更。

AzureAIAgent支援從 YAML 宣告式規範進行實例化。 宣告式方法可讓您在單一可稽核的文件中定義代理程序的屬性、指示、模型組態、工具和其他選項。 這可讓代理程式組合可攜式且輕鬆地跨環境進行管理。

備註

宣告式 YAML 中列出的任何工具、函式或外掛程式,都必須在建構階段提供給代理程式使用。 對於以核心為基礎的外掛程式,這表示必須在核心中註冊它們。 針對 Bing Grounding、檔案搜尋或 OpenAPI 工具等內建工具,必須提供正確的設定和憑證。 代理程式載入器不會從頭開始建立函式。 如果缺少必要的元件,代理程式建立將會失敗。

如何使用宣告式規格

本節不列舉每個可能的 YAML 組態,而是概述主要原則,並提供概念範例的連結,這些範例會顯示每個工具類型的完整程序代碼。 請參閱這些示範範例,來了解具有宣告式規格的AzureAIAgent端對端實作。

範例:從 YAML 建立 AzureAIAgent

最小 YAML 宣告式規格看起來可能如下所示:

type: foundry_agent
name: MyAgent
instructions: Respond politely to the user's questions.
model:
  id: ${AzureAI:ChatModelId}
tools:
  - id: MenuPlugin.get_specials
    type: function
  - id: MenuPlugin.get_item_price
    type: function

如需如何連接代理程式的詳細資訊,請參閱上述的完整程式碼範例。

重點

  • 宣告式規格允許在 YAML 中定義代理程式結構、工具和行為。
  • 所有參考的工具和外掛程式都必須在運行時間註冊或存取。
  • Bing、檔案搜尋和程式代碼解釋器等內建工具需要適當的組態和認證(通常是透過環境變數或明確自變數)。
  • 如需完整的範例,請參閱提供的範例連結,其中示範實際案例,包括外掛程式註冊、Azure 身分識別組態和進階工具使用。

此功能無法使用。

後續步驟