探索語意核心 OpenAIAssistantAgent
重要
這項功能位於候選版階段。 此階段的功能幾乎完整且一般穩定,不過在達到完整正式運作之前,它們可能會經歷輕微的精簡或優化。
如需此討論的詳細 API 檔,請參閱:
代理程式目前無法在Java中使用。
什麼是助理?
OpenAI Assistant API 是專為更進階和互動式 AI 功能而設計的特製化介面,可讓開發人員建立個人化和多步驟工作導向的代理程式。 與專注於簡單交談交換的聊天完成 API 不同,小幫手 API 允許動態、目標導向的互動與程式代碼解釋器和檔案搜尋等其他功能互動。
準備開發環境
若要繼續開發 OpenAIAIAssistantAgent
,請使用適當的套件來設定您的開發環境。
將 Microsoft.SemanticKernel.Agents.OpenAI
套件新增至您的專案:
dotnet add package Microsoft.SemanticKernel.Agents.AzureAI --prerelease
您也可以包含 Azure.Identity
套件:
dotnet add package Azure.Identity
請安裝 semantic-kernel
套件,並選擇性地包含 Azure 相依性:
pip install semantic-kernel[azure]
代理程式目前無法在Java中使用。
建立 OpenAIAssistantAgent
建立 OpenAIAssistant
需要叫用異步處理的遠端服務。 為了管理此情況,OpenAIAssistantAgent
會透過靜態處理站方法具現化,以確保進程以非封鎖方式發生。 這個方法會抽象化異步呼叫的複雜度,一旦助理完全初始化並可供使用,就會傳回承諾或未來。
AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
Assistant assistant =
await this.AssistantClient.CreateAssistantAsync(
"<model name>",
"<agent name>",
instructions: "<agent instructions>");
OpenAIAssistantAgent agent = new(assistant, client);
from semantic_kernel.agents.open_ai import AzureAssistantAgent, OpenAIAssistantAgent
# Set up the client and model using Azure OpenAI Resources
client, model = AzureAssistantAgent.setup_resources()
# Define the assistant definition
definition = await client.beta.assistants.create(
model=model,
instructions="<instructions>",
name="<agent name>",
)
# Create the AzureAssistantAgent instance using the client and the assistant definition
agent = AzureAssistantAgent(
client=client,
definition=definition,
)
# or
# Set up the client and model using OpenAI Resources
client, model = OpenAIAssistantAgent.setup_resources()
# Define the assistant definition
definition = await client.beta.assistants.create(
model=model,
instructions="<instructions>",
name="<agent name>",
)
# Create the OpenAIAssistantAgent instance using the client and the assistant definition
agent = OpenAIAssistantAgent(
client=client,
definition=definition,
)
代理程式目前無法在Java中使用。
擷取一個 OpenAIAssistantAgent
建立之後,可以透過其識別碼來存取助理的識別碼。 此標識碼可用來從現有的助理定義生成 OpenAIAssistantAgent
。
針對 .NET,代理程式識別碼會透過任何代理程式所定義的 屬性公開為 string
。
AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
Assistant assistant = await this.AssistantClient.GetAssistantAsync("<assistant id>");
OpenAIAssistantAgent agent = new(assistant, client);
# Using Azure OpenAI Resources
# Create the client using Azure OpenAI resources and configuration
client, model = AzureAssistantAgent.setup_resources()
# Create the assistant definition
definition = await client.beta.assistants.create(
model=model,
name="<agent name>",
instructions="<instructions>",
)
# Store the assistant ID
assistant_id = definition.id
# Retrieve the assistant definition from the server based on the assistant ID
new_asst_definition = await client.beta.assistants.retrieve(assistant_id)
# Create the AzureAssistantAgent instance using the client and the assistant definition
agent = AzureAssistantAgent(
client=client,
definition=new_asst_definition,
)
代理程式目前無法在Java中使用。
使用 OpenAIAssistantAgent
如同所有 小幫手 API 的層面,交談內容會儲存在遠端。 每個對話稱為討論串,並由唯一string
識別。 與 OpenAIAssistantAgent
的互動會系結至呼叫代理程式時必須指定的這個特定線程標識碼/
// Define agent
OpenAIAssistantAgent agent = ...;
// Create a thread for the agent conversation.
string threadId = await agent.CreateThreadAsync();
// Add a user message to the conversation
chat.Add(threadId, new ChatMessageContent(AuthorRole.User, "<user input>"));
// Generate the agent response(s)
await foreach (ChatMessageContent response in agent.InvokeAsync(threadId))
{
// Process agent response(s)...
}
// Delete the thread when it is no longer needed
await agent.DeleteThreadAsync(threadId);
# Define agent
openai_agent = await ...
# Create a thread for the agent conversation
thread_id = await agent.create_thread()
# Add a user message to the conversation
await agent.add_chat_message(
thread_id=thread_id,
message=ChatMessageContent(role=AuthorRole.USER, content="<user input>"),
)
# Generate the agent response(s)
async for response in agent.invoke(thread_id=thread_id):
# process agent response(s)...
# Delete the thread when it is no longer needed
await agent.delete_thread(thread_id)
代理程式目前無法在Java中使用。
刪除 OpenAIAssistantAgent
由於助理的定義會從遠端儲存,因此若未刪除,則會保存該定義。
刪除小幫手定義可以直接使用 AssistantClient
來執行。
注意:嘗試在刪除後使用代理程序實例會導致服務例外狀況。
針對 .NET,代理識別碼會透過任何代理程式所定義的string
屬性公開為Agent.Id
。
AssistantClient client = OpenAIAssistantAgent.CreateAzureOpenAIClient(...).GetAssistantClient();
Assistant assistant = await this.AssistantClient.DeleteAssistantAsync("<assistant id>");
await agent.delete()
is_deleted = agent._is_deleted
代理程式目前無法在Java中使用。
操作指南
如需 OpenAIAssistantAgent
的端對端範例,請參閱: