-
2025-08-12
在本快速入門中,我們會逐步引導您使用 Azure AI Foundry SDK 設定本機開發環境。 我們會撰寫提示、在您的應用程式程式碼中執行它、追蹤要進行的 LLM 呼叫,以及在 LLM 的輸出上執行基本評估。
Tip
本文的其餘部分說明如何使用 中樞型專案。 如果您想要改用 Foundry 專案,請選取本文頂端的 Foundry 專案。 我需要哪種類型的專案?
Prerequisites
- Azure 訂用帳戶。 如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶。
- 中樞型專案。 如果您不熟悉 Azure AI Foundry 且沒有中樞型專案,請選取本文頂端的 Foundry 專案 ,以改用 Foundry 專案。
設定開發環境
安裝這些套件。
pip install azure-ai-inference azure-identity azure-ai-projects==1.0.0b10
Note
不同的項目類型需要不同版本的
azure-ai-projects
套件。 若要避免衝突,請建立個別的 Python 環境:針對中樞型專案使用版本1.0.0b10
,以及 Foundry 專案的最新版本。
部署模型
Tip
因為您可以在 Azure AI Foundry 入口網站中 自訂左窗格,因此您看到的項目可能會與這些步驟中顯示的不同。 如果您沒有看到您要尋找的內容,請選取左窗格底部的 ... 更多。
登入 Azure AI Foundry。
選取中樞型專案。 如果您沒有中樞型專案,請選取本文頂端的 Foundry 專案 以改用 Foundry 專案。
從左窗格中選取 [模型目錄 ]。
從模型清單中選取 gpt-4o-mini 模型。 您可以使用搜尋列來尋找它。
在 [模型詳細數據] 頁面上,選取 [ 部署]。
保留預設 的部署名稱。 請選擇 部署。
部署模型之後,請選取 [在遊樂場 中開啟] 以測試您的模型。
組建您的聊天應用程式
建立名為 chat.py 檔案。 下列程式代碼複製並貼到其中。
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
project_connection_string = "<your-connection-string-goes-here>"
project = AIProjectClient.from_connection_string(
conn_str=project_connection_string, credential=DefaultAzureCredential()
)
chat = project.inference.get_chat_completions_client()
response = chat.complete(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": "You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig?",
},
{"role": "user", "content": "Hey, can you help me with my taxes? I'm a freelancer."},
],
)
print(response.choices[0].message.content)
插入您的連接字串
需要您的專案連接字串才能從您的程式碼中呼叫 Azure AI Foundry 模型中的 Azure OpenAI。
於您在 Azure AI Foundry 遊樂場快速入門中建立的 Azure AI Foundry 專案中尋找您的連接字串。 開啟專案,然後在 [概觀] 頁面上尋找連接字串。
複製連接字串,然後在 <your-connection-string-goes-here>
檔案中取代 。
執行您的聊天指令碼
執行指令碼以查看來自模型的回應。
python chat.py
從使用者輸入和提示範本來產生提示
指令碼會使用硬式編碼的輸入和輸出訊息。 在真實的應用程式中,您將從用戶端應用程式取得輸入,產生具有模型內部指令的系統訊息,然後使用所有訊息呼叫 LLM。
讓我們變更指令碼以從用戶端應用程式取得輸入並使用提示範本來產生系統訊息。
移除列印回應的指令碼的最後一行。
現在定義
get_chat_response
函數,該函數接受訊息和內容,且使用提示範本來產生系統訊息,並且呼叫模型。 將此程式代碼新增至您現有的 chat.py 檔案:from azure.ai.inference.prompts import PromptTemplate def get_chat_response(messages, context): # create a prompt template from an inline string (using mustache syntax) prompt_template = PromptTemplate.from_string( prompt_template=""" system: You are an AI assistant that speaks like a techno punk rocker from 2350. Be cool but not too cool. Ya dig? Refer to the user by their first name, try to work their last name into a pun. The user's first name is {{first_name}} and their last name is {{last_name}}. """ ) # generate system message from the template, passing in the context as variables system_message = prompt_template.create_messages(data=context) # add the prompt messages to the user messages return chat.complete( model="gpt-4o-mini", messages=system_message + messages, temperature=1, frequency_penalty=0.5, presence_penalty=0.5, )
Note
提示範本使用鬍子格式。
可以輕鬆地將 get_chat_response 函數新增為 FastAPI 或 Flask 應用程式的路由,以便從前端 Web 應用程式呼叫此函數。
現在模擬從前端應用程式傳遞訊息到此函數。 將列下程式碼加入 chat.py chat.py 檔案的結尾。 請隨意修改訊息並新增您自己的名稱。
if __name__ == "__main__": response = get_chat_response( messages=[{"role": "user", "content": "what city has the best food in the world?"}], context={"first_name": "Jessie", "last_name": "Irwin"}, ) print(response.choices[0].message.content)
執行修訂的指令碼來查看模型對此新輸入的回應。
python chat.py
清理資源
如果您不再需要您所建立的任何資源,請刪除與您的專案相關聯的資源群組。
在 Azure AI Foundry 入口網站中,選取右上角的項目名稱。 然後選取資源群組的連結,以在 Azure 入口網站中開啟它。 選取資源群組,然後選取 [ 刪除]。 確認您想要刪除資源群組。
後續步驟
在本快速入門中,您會使用 Azure AI Foundry 來:
- 建立專案
- 部署模型
- 執行聊天完成
- 建立和執行代理程式
- 將檔案上傳至代理程式
Azure AI Foundry SDK 提供多種語言,包括 Python、Java、JavaScript 和 C#。 此快速入門提供每個語言的指示。
Tip
本文的其餘部分說明如何建立和使用 Foundry 專案。 如果您想要改用中樞 型專案 ,請選取本文頂端的中樞型專案。 我需要哪種類型的專案?
Prerequisites
- Azure 訂用帳戶。 如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶。
- 您必須是訂用帳戶的 擁有者 ,才能接收使用專案所需的適當訪問控制。
Important
本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款。
從專案和模型開始
在入口網站中,您可以從 Microsoft、OpenAI、DeepSeek、Hugging Face、Meta 等等探索豐富的尖端模型目錄。 在本教學課程中,搜尋並選取 gpt-4o 模型。
在 [模型詳細數據] 頁面上,選取 [使用此模型]。
填入要用於項目的名稱,然後選取 [ 建立]。
檢閱部署資訊,然後選取 [ 部署]。
在您建立資源之後,您就會在聊天遊樂場中。
設定您的環境
不需要安裝 Azure AI Foundry 入口網站。
安裝下列套件:
pip install openai azure-ai-projects azure-identity
-
Azure AI Foundry 模型可讓客戶使用單一端點和認證,從旗艦模型提供者取用最強大的模型。 這表示您可以在模型之間切換,並從您的應用程式取用它們,而不需要變更單行程序代碼。
在專案的 [概觀] 區段中複製 Azure AI Foundry 專案端點。 您馬上就會用到它。
Tip
如果您沒有看到 Azure AI Foundry 專案端點,則會使用中樞型專案。 (請參閱 項目類型)。 切換至 Foundry 專案,或使用上述步驟建立一個專案。
在執行 Python 腳本之前,請務必先使用 CLI
az login
(或az login --use-device-code
) 命令登入以驗證。
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
-
Azure AI Foundry 模型可讓客戶使用單一端點和認證,從旗艦模型提供者取用最強大的模型。 這表示您可以在模型之間切換,並從您的應用程式取用它們,而不需要變更單行程序代碼。
在專案的 [概觀] 區段中複製 Azure AI Foundry 專案端點。 您馬上就會用到它。
Tip
如果您沒有看到 Azure AI Foundry 專案端點,則會使用中樞型專案。 (請參閱 項目類型)。 切換至 Foundry 專案,或使用上述步驟建立一個專案。
設定這些環境變數以在您的文稿中使用:
MODEL_DEPLOYMENT_NAME=gpt-4o PROJECT_ENDPOINT=https://<your-foundry-resource-name>.services.ai.azure.com/api/projects/<your-foundry-project-name>
在執行 Java 文稿之前,請務必先使用 CLI
az login
(或az login --use-device-code
) 命令登入以驗證。將 POM.XML 下載至您的 Java IDE。
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
在執行 JavaScript 腳本之前,請務必先使用 CLI
az login
(或az login --use-device-code
) 命令登入以驗證。下載 package.json。
使用
npm install
安裝套件-
Azure AI Foundry 模型可讓客戶使用單一端點和認證,從旗艦模型提供者取用最強大的模型。 這表示您可以在模型之間切換,並從您的應用程式取用它們,而不需要變更單行程序代碼。
在專案的 [概觀] 區段中複製 Azure AI Foundry 專案端點。 您馬上就會用到它。
Tip
如果您沒有看到 Azure AI Foundry 專案端點,則會使用中樞型專案。 (請參閱 項目類型)。 切換至 Foundry 專案,或使用上述步驟建立一個專案。
設定這些環境變數以在您的文稿中使用:
MODEL_DEPLOYMENT_NAME=gpt-4o PROJECT_ENDPOINT=https://<your-foundry-resource-name>.services.ai.azure.com/api/projects/<your-foundry-project-name>
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
在執行 TypeScript 腳本之前,請務必先使用 CLI
az login
(或az login --use-device-code
) 命令登入以驗證。下載 package.json。
使用
npm install
安裝套件-
Azure AI Foundry 模型可讓客戶使用單一端點和認證,從旗艦模型提供者取用最強大的模型。 這表示您可以在模型之間切換,並從您的應用程式取用它們,而不需要變更單行程序代碼。
在專案的 [概觀] 區段中複製 Azure AI Foundry 專案端點。 您馬上就會用到它。
Tip
如果您沒有看到 Azure AI Foundry 專案端點,則會使用中樞型專案。 (請參閱 項目類型)。 切換至 Foundry 專案,或使用上述步驟建立一個專案。
設定這些環境變數以在您的文稿中使用:
MODEL_DEPLOYMENT_NAME=gpt-4o PROJECT_ENDPOINT=https://<your-foundry-resource-name>.services.ai.azure.com/api/projects/<your-foundry-project-name>
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
安裝套件:
若要在 .NET 專案中使用 Azure AI 服務,您必須安裝數個 NuGet 套件。 在整合式終端機中使用 .NET CLI 新增 NuGet 套件:
# Add Azure AI SDK packages dotnet add package Azure.Identity dotnet add package Azure.AI.Projects dotnet add package Azure.AI.Agents.Persistent dotnet add package Azure.AI.Inference
-
Azure AI Foundry 模型可讓客戶使用單一端點和認證,從旗艦模型提供者取用最強大的模型。 這表示您可以在模型之間切換,並從您的應用程式取用它們,而不需要變更單行程序代碼。
在專案的 [概觀] 區段中複製 Azure AI Foundry 專案端點。 您馬上就會用到它。
Tip
如果您沒有看到 Azure AI Foundry 專案端點,則會使用中樞型專案。 (請參閱 項目類型)。 切換至 Foundry 專案,或使用上述步驟建立一個專案。
設定這些環境變數以在您的文稿中使用:
AZURE_AI_ENDPOINT=https://your.services.ai.azure.com/api/projects/project AZURE_AI_MODEL=your_model_name
在執行 C# 文稿之前,請務必先使用 CLI
az login
(或az login --use-device-code
) 命令登入以進行驗證。
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
在執行下一個命令之前,請務必使用 CLI
az login
(或az login --use-device-code
) 命令登入以驗證。取得暫時存取令牌。 它將在 60-90 分鐘後過期,之後您需要重新整理。
az account get-access-token --scope https://ai.azure.com/.default
將結果儲存為環境變數
AZURE_AI_AUTH_TOKEN
。
Note
本文中的所有程序代碼都在 GitHub 快速入門中。
執行聊天完成
聊天完成是 AI 應用程式的基本建置組塊。 使用聊天完成功能,您可以傳送一系列訊息,並從模型取得回應。
- 在聊天遊樂場中,填入提示,然後選取 [ 傳送 ] 按鈕。
- 模型會在 [ 回應 ] 窗格中傳回回應。
以此程式碼中的 endpoint
取代您的端點:
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
project = AIProjectClient(
endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
credential=DefaultAzureCredential(),
)
models = project.get_openai_client(api_version="2024-10-21")
response = models.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful writing assistant"},
{"role": "user", "content": "Write me a poem about flowers"},
],
)
print(response.choices[0].message.content)
package com.azure.ai.foundry.samples;
import com.azure.ai.inference.ChatCompletionsClient;
import com.azure.ai.inference.ChatCompletionsClientBuilder;
import com.azure.ai.inference.models.ChatCompletions;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.TokenCredential;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.DefaultAzureCredentialBuilder;
/**
* Sample demonstrating non-streaming chat completion functionality
* using the Azure AI Inference SDK, wired to your AOAI project endpoint.
*
* Environment variables:
* - PROJECT_ENDPOINT: Required. Your Azure AI project endpoint.
* - AZURE_AI_API_KEY: Optional. Your API key (falls back to DefaultAzureCredential).
* - AZURE_MODEL_DEPLOYMENT_NAME: Optional. Model deployment name (default: "phi-4").
* - AZURE_MODEL_API_PATH: Optional. API path segment (default: "deployments").
* - CHAT_PROMPT: Optional. The prompt to send (uses a default if not provided).
*
* SDK Features Demonstrated:
* - Using the Azure AI Inference SDK (com.azure:azure-ai-inference:1.0.0-beta.5)
* - Creating a ChatCompletionsClient with Azure or API key authentication
* - Configuring endpoint paths for different model deployments
* - Using the simplified complete() method for quick completions
* - Accessing response content through strongly-typed objects
* - Implementing proper error handling for service requests
* - Choosing between DefaultAzureCredential and AzureKeyCredential
*
*/
public class ChatCompletionSample {
private static final ClientLogger logger = new ClientLogger(ChatCompletionSample.class);
public static void main(String[] args) {
// 1) Read and validate the project endpoint
String projectEndpoint = System.getenv("PROJECT_ENDPOINT");
if (projectEndpoint == null || projectEndpoint.isBlank()) {
logger.error("PROJECT_ENDPOINT is required but not set");
return;
}
// 2) Optional auth + model settings
String apiKey = System.getenv("AZURE_AI_API_KEY");
String deploymentName = System.getenv("AZURE_MODEL_DEPLOYMENT_NAME");
String apiPath = System.getenv("AZURE_MODEL_API_PATH");
String prompt = System.getenv("CHAT_PROMPT");
if (deploymentName == null || deploymentName.isBlank()) {
deploymentName = "phi-4";
logger.info("No AZURE_MODEL_DEPLOYMENT_NAME provided, using default: {}", deploymentName);
}
if (apiPath == null || apiPath.isBlank()) {
apiPath = "deployments";
logger.info("No AZURE_MODEL_API_PATH provided, using default: {}", apiPath);
}
if (prompt == null || prompt.isBlank()) {
prompt = "What best practices should I follow when asking an AI model to review Java code?";
logger.info("No CHAT_PROMPT provided, using default prompt: {}", prompt);
}
try {
// 3) Build the full inference endpoint URL
String fullEndpoint = projectEndpoint.endsWith("/")
? projectEndpoint
: projectEndpoint + "/";
fullEndpoint += apiPath + "/" + deploymentName;
logger.info("Using inference endpoint: {}", fullEndpoint);
// 4) Create the client with key or token credential :contentReference[oaicite:0]{index=0}
ChatCompletionsClient client;
if (apiKey != null && !apiKey.isBlank()) {
logger.info("Authenticating using API key");
client = new ChatCompletionsClientBuilder()
.credential(new AzureKeyCredential(apiKey))
.endpoint(fullEndpoint)
.buildClient();
} else {
logger.info("Authenticating using DefaultAzureCredential");
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
client = new ChatCompletionsClientBuilder()
.credential(credential)
.endpoint(fullEndpoint)
.buildClient();
}
// 5) Send a simple chat completion request
logger.info("Sending chat completion request with prompt: {}", prompt);
ChatCompletions completions = client.complete(prompt);
// 6) Process the response
String content = completions.getChoice().getMessage().getContent();
logger.info("Received response from model");
System.out.println("\nResponse from AI assistant:\n" + content);
} catch (HttpResponseException e) {
// Handle API errors
int status = e.getResponse().getStatusCode();
logger.error("Service error {}: {}", status, e.getMessage());
if (status == 401 || status == 403) {
logger.error("Authentication failed. Check API key or Azure credentials.");
} else if (status == 404) {
logger.error("Deployment not found. Verify deployment name and endpoint.");
} else if (status == 429) {
logger.error("Rate limit exceeded. Please retry later.");
}
} catch (Exception e) {
// Handle all other exceptions
logger.error("Error in chat completion: {}", e.getMessage(), e);
}
}
}
// Get the Azure AI endpoint and deployment name from environment variables
const endpoint = process.env.PROJECT_ENDPOINT;
const deployment = process.env.MODEL_DEPLOYMENT_NAME || 'gpt-4o';
// Create an Azure OpenAI Client
const project = new AIProjectClient(endpoint, new DefaultAzureCredential());
const client = await project.inference.azureOpenAI({
// The API version should match the version of the Azure OpenAI resource
apiVersion: "2024-12-01-preview"
});
// Create a chat completion
const chatCompletion = await client.chat.completions.create({
model: deployment,
messages: [
{ role: "system", content: "You are a helpful writing assistant" },
{ role: "user", content: "Write me a poem about flowers" },
],
});
console.log(`\n==================== 🌷 COMPLETIONS POEM ====================\n`);
console.log(chatCompletion.choices[0].message.content);
// Get the Azure AI endpoint and deployment name from environment variables
const endpoint = process.env.PROJECT_ENDPOINT as string;
const deployment = process.env.MODEL_DEPLOYMENT_NAME || 'gpt-4o';
// Create an Azure OpenAI Client
const project = new AIProjectClient(endpoint, new DefaultAzureCredential());
const client = await project.inference.azureOpenAI({
// The API version should match the version of the Azure OpenAI resource
apiVersion: "2024-12-01-preview"
});
// Create a chat completion
const chatCompletion = await client.chat.completions.create({
model: deployment,
messages: [
{ role: "system", content: "You are a helpful writing assistant" },
{ role: "user", content: "Write me a poem about flowers" },
],
});
console.log(`\n==================== 🌷 COMPLETIONS POEM ====================\n`);
console.log(chatCompletion.choices[0].message.content);
using Azure;
using Azure.Identity;
using Azure.AI.Projects;
using Azure.AI.Inference;
var projectEndpoint = new Uri(System.Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT"));
var modelDeploymentName = System.Environment.GetEnvironmentVariable("AZURE_AI_MODEL");
var credential = new DefaultAzureCredential();
AIProjectClient client = new AIProjectClient(projectEndpoint, credential);
ChatCompletionsClient chatClient = client.GetChatCompletionsClient();
var requestOptions = new ChatCompletionsOptions()
{
Messages =
{
new ChatRequestSystemMessage("You are a helpful assistant."),
new ChatRequestUserMessage("How many feet are in a mile?"),
},
Model = modelDeploymentName
};
Response<ChatCompletions> response = chatClient.Complete(requestOptions);
Console.WriteLine(response.Value.Content);
將YOUR-FOUNDRY-RESOURCE-NAME
替換為您的值:
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-10-21' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
-d '{
"messages": [
{"role": "system",
"content": "You are a helpful writing assistant"},
{"role": "user",
"content": "Write me a poem about flowers"}
],
"model": "gpt-4o"
}'
與代理程式聊天
代理程式透過使用工具具有強大的功能。 首先,與客服人員聊天。
當您準備好嘗試代理程式時,會為您建立預設代理程式。 若要與此代理程式聊天:
- 在左窗格中,選取 [ 遊樂場]。
- 在 [代理程式遊樂場] 卡片中,選取 [雀躍期待]。
- 新增指示,例如「您是實用的寫作助理」。
- 例如,開始和你的經紀人聊天,例如,“給我寫一首關於鮮花的詩”。
以此程式碼中的 endpoint
取代您的端點:
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FilePurpose
project = AIProjectClient(
endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
credential=DefaultAzureCredential(),
)
agent = project.agents.create_agent(
model="gpt-4o",
name="my-agent",
instructions="You are a helpful writing assistant")
thread = project.agents.threads.create()
message = project.agents.messages.create(
thread_id=thread.id,
role="user",
content="Write me a poem about flowers")
run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
if run.status == "failed":
# Check if you got "Rate limit is exceeded.", then you want to get more quota
print(f"Run failed: {run.last_error}")
# Get messages from the thread
messages = project.agents.messages.list(thread_id=thread.id)
# Get the last message from the sender
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
if message.run_id == run.id and message.text_messages:
print(f"{message.role}: {message.text_messages[-1].text.value}")
# Delete the agent once done
project.agents.delete_agent(agent.id)
print("Deleted agent")
package com.azure.ai.foundry.samples;
import com.azure.ai.agents.persistent.PersistentAgentsClient;
import com.azure.ai.agents.persistent.PersistentAgentsClientBuilder;
import com.azure.ai.agents.persistent.PersistentAgentsAdministrationClient;
import com.azure.ai.agents.persistent.models.CreateAgentOptions;
import com.azure.ai.agents.persistent.models.CreateThreadAndRunOptions;
import com.azure.ai.agents.persistent.models.PersistentAgent;
import com.azure.ai.agents.persistent.models.ThreadRun;
import com.azure.core.credential.TokenCredential;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.DefaultAzureCredentialBuilder;
/**
* Sample demonstrating how to work with Azure AI Agents using the Azure AI Agents Persistent SDK.
*
* This sample shows how to:
* - Set up authentication with Azure credentials
* - Create a persistent agent with custom instructions
* - Start a thread and run with the agent
* - Access various properties of the agent and thread run
* - Work with the PersistentAgentsClient and PersistentAgentsAdministrationClient
*
* Environment variables:
* - AZURE_ENDPOINT: Optional fallback. The base endpoint for your Azure AI service if PROJECT_ENDPOINT is not provided.
* - PROJECT_ENDPOINT: Required. The endpoint for your Azure AI Project.
* - MODEL_DEPLOYMENT_NAME: Optional. The model deployment name (defaults to "gpt-4o").
* - AGENT_NAME: Optional. The name to give to the created agent (defaults to "java-quickstart-agent").
* - AGENT_INSTRUCTIONS: Optional. The instructions for the agent (defaults to a helpful assistant).
*
* Note: This sample requires proper Azure authentication. It uses DefaultAzureCredential which supports
* multiple authentication methods including environment variables, managed identities, and interactive login.
*
* SDK Features Demonstrated:
* - Using the Azure AI Agents Persistent SDK (com.azure:azure-ai-agents-persistent:1.0.0-beta.2)
* - Creating an authenticated client with DefaultAzureCredential
* - Using the PersistentAgentsClientBuilder pattern for client instantiation
* - Working with the PersistentAgentsAdministrationClient for agent management
* - Creating agents with specific configurations (name, model, instructions)
* - Starting threads and runs for agent conversations
* - Working with agent state and thread management
* - Accessing agent and thread run properties
* - Implementing proper error handling for Azure service interactions
*/
public class AgentSample {
private static final ClientLogger logger = new ClientLogger(AgentSample.class);
public static void main(String[] args) {
// Load environment variables with better error handling, supporting both .env and system environment variables
String endpoint = System.getenv("AZURE_ENDPOINT");
String projectEndpoint = System.getenv("PROJECT_ENDPOINT");
String modelName = System.getenv("MODEL_DEPLOYMENT_NAME");
String agentName = System.getenv("AGENT_NAME");
String instructions = System.getenv("AGENT_INSTRUCTIONS");
// Check for required endpoint configuration
if (projectEndpoint == null && endpoint == null) {
String errorMessage = "Environment variables not configured. Required: either PROJECT_ENDPOINT or AZURE_ENDPOINT must be set.";
logger.error("ERROR: {}", errorMessage);
logger.error("Please set your environment variables or create a .env file. See README.md for details.");
return;
}
// Use AZURE_ENDPOINT as fallback if PROJECT_ENDPOINT not set
if (projectEndpoint == null) {
projectEndpoint = endpoint;
logger.info("Using AZURE_ENDPOINT as PROJECT_ENDPOINT: {}", projectEndpoint);
}
// Set defaults for optional parameters with informative logging
if (modelName == null) {
modelName = "gpt-4o";
logger.info("No MODEL_DEPLOYMENT_NAME provided, using default: {}", modelName);
}
if (agentName == null) {
agentName = "java-quickstart-agent";
logger.info("No AGENT_NAME provided, using default: {}", agentName);
}
if (instructions == null) {
instructions = "You are a helpful assistant that provides clear and concise information.";
logger.info("No AGENT_INSTRUCTIONS provided, using default instructions");
}
// Create Azure credential with DefaultAzureCredentialBuilder
// This supports multiple authentication methods including environment variables,
// managed identities, and interactive browser login
logger.info("Building DefaultAzureCredential");
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
try {
// Build the general agents client
logger.info("Creating PersistentAgentsClient with endpoint: {}", projectEndpoint);
PersistentAgentsClient agentsClient = new PersistentAgentsClientBuilder()
.endpoint(projectEndpoint)
.credential(credential)
.buildClient();
// Derive the administration client
logger.info("Getting PersistentAgentsAdministrationClient");
PersistentAgentsAdministrationClient adminClient =
agentsClient.getPersistentAgentsAdministrationClient();
// Create an agent
logger.info("Creating agent with name: {}, model: {}", agentName, modelName);
PersistentAgent agent = adminClient.createAgent(
new CreateAgentOptions(modelName)
.setName(agentName)
.setInstructions(instructions)
);
logger.info("Agent created: ID={}, Name={}", agent.getId(), agent.getName());
logger.info("Agent model: {}", agent.getModel());
// Start a thread/run on the general client
logger.info("Creating thread and run with agent ID: {}", agent.getId());
ThreadRun runResult = agentsClient.createThreadAndRun(
new CreateThreadAndRunOptions(agent.getId())
);
logger.info("ThreadRun created: ThreadId={}", runResult.getThreadId());
// List available getters on ThreadRun for informational purposes
logger.info("\nAvailable getters on ThreadRun:");
for (var method : ThreadRun.class.getMethods()) {
if (method.getName().startsWith("get")) {
logger.info(" - {}", method.getName());
}
}
logger.info("\nDemo completed successfully!");
} catch (HttpResponseException e) {
// Handle service-specific errors with detailed information
int statusCode = e.getResponse().getStatusCode();
logger.error("Service error {}: {}", statusCode, e.getMessage());
logger.error("Refer to the Azure AI Agents documentation for troubleshooting information.");
} catch (Exception e) {
// Handle general exceptions
logger.error("Error in agent sample: {}", e.getMessage(), e);
}
}
}
const endpoint = process.env.PROJECT_ENDPOINT;
const deployment = process.env.MODEL_DEPLOYMENT_NAME || 'gpt-4o';
const client = new AIProjectClient(endpoint, new DefaultAzureCredential());
// Create an Agent
const agent = await client.agents.createAgent(deployment, {
name: 'my-agent',
instructions: 'You are a helpful agent'
});
console.log(`\n==================== 🕵️ POEM AGENT ====================`);
// Create a thread and message
const thread = await client.agents.threads.create();
const prompt = 'Write me a poem about flowers';
console.log(`\n---------------- 📝 User Prompt ---------------- \n${prompt}`);
await client.agents.messages.create(thread.id, 'user', prompt);
// Create run
let run = await client.agents.runs.create(thread.id, agent.id);
// Wait for run to complete
console.log(`\n---------------- 🚦 Run Status ----------------`);
while (['queued', 'in_progress', 'requires_action'].includes(run.status)) {
// Avoid adding a lot of messages to the console
await new Promise((resolve) => setTimeout(resolve, 1000));
run = await client.agents.runs.get(thread.id, run.id);
console.log(`Run status: ${run.status}`);
}
console.log('\n---------------- 📊 Token Usage ----------------');
console.table([run.usage]);
const messagesIterator = await client.agents.messages.list(thread.id);
const assistantMessage = await getAssistantMessage(messagesIterator);
console.log('\n---------------- 💬 Response ----------------');
printAssistantMessage(assistantMessage);
// Clean up
console.log(`\n---------------- 🧹 Clean Up Poem Agent ----------------`);
await client.agents.deleteAgent(agent.id);
console.log(`Deleted Agent, Agent ID: ${agent.id}`);
const endpoint = process.env.PROJECT_ENDPOINT as string;
const deployment = process.env.MODEL_DEPLOYMENT_NAME || 'gpt-4o';
const client = new AIProjectClient(endpoint, new DefaultAzureCredential());
// Create an Agent
const agent = await client.agents.createAgent(deployment, {
name: 'my-agent',
instructions: 'You are a helpful agent'
});
console.log(`\n==================== 🕵️ POEM AGENT ====================`);
// Create a thread and message
const thread = await client.agents.threads.create();
const prompt = 'Write me a poem about flowers';
console.log(`\n---------------- 📝 User Prompt ---------------- \n${prompt}`);
await client.agents.messages.create(thread.id, 'user', prompt);
// Create run
let run = await client.agents.runs.create(thread.id, agent.id);
// Wait for run to complete
console.log(`\n---------------- 🚦 Run Status ----------------`);
while (['queued', 'in_progress', 'requires_action'].includes(run.status)) {
// Avoid adding a lot of messages to the console
await new Promise((resolve) => setTimeout(resolve, 1000));
run = await client.agents.runs.get(thread.id, run.id);
console.log(`Run status: ${run.status}`);
}
console.log('\n---------------- 📊 Token Usage ----------------');
console.table([run.usage]);
const messagesIterator = await client.agents.messages.list(thread.id);
const assistantMessage = await getAssistantMessage(messagesIterator);
console.log('\n---------------- 💬 Response ----------------');
printAssistantMessage(assistantMessage);
// Clean up
console.log(`\n---------------- 🧹 Clean Up Poem Agent ----------------`);
await client.agents.deleteAgent(agent.id);
console.log(`Deleted Agent, Agent ID: ${agent.id}`);
using Azure;
using Azure.Identity;
using Azure.AI.Agents.Persistent;
// Creating the Client for agents
var projectEndpoint = System.Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("AZURE_AI_MODEL");
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
// Create an Agent with toolResources and process Agent run
PersistentAgent agent = client.Administration.CreateAgent(
model: modelDeploymentName,
name: "SDK Test Agent - Tutor",
instructions: "You are a personal electronics tutor. Write and run code to answer questions.",
tools: new List<ToolDefinition> { new CodeInterpreterToolDefinition() });
// Create thread for communication
PersistentAgentThread thread = client.Threads.CreateThread();
// Create message to thread
PersistentThreadMessage messageResponse = client.Messages.CreateMessage(
thread.Id,
MessageRole.User,
"I need to solve the equation `3x + 11 = 14`. Can you help me?");
// Run the Agent
ThreadRun run = client.Runs.CreateRun(thread, agent);
// Wait for the run to complete
do
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
run = client.Runs.GetRun(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress);
Pageable<PersistentThreadMessage> messages = client.Messages.GetMessages(
threadId: thread.Id,
order: ListSortOrder.Ascending
);
// Print the messages in the thread
WriteMessages(messages);
// Delete the thread and agent after use
client.Threads.DeleteThread(thread.Id);
client.Administration.DeleteAgent(agent.Id);
// Temporary function to use a list of messages in the thread and write them to the console.
static void WriteMessages(IEnumerable<PersistentThreadMessage> messages)
{
foreach (PersistentThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
Console.Write(textItem.Text);
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}
}
將 YOUR-FOUNDRY-RESOURCE-NAME
和 YOUR-PROJECT-NAME
替換為您的值。
# Create agent
curl --request POST --url "https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/assistants?api-version=v1" \
-h "authorization: Bearer $AZURE_AI_AUTH_TOKEN" \
-h "content-type: application/json" \
-d '{
"model": "gpt-4o",
"name": "my-agent",
"instructions": "You are a helpful writing assistant"
}'
#Lets say agent ID created is asst_123456789. Use this to run the agent
# Create thread
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
#Lets say thread ID created is thread_123456789. Use this in the next step
# Create message using thread ID
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/messages?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
-d '{
"role": "user",
"content": "Write me a poem about flowers"
}'
# Run thread with the agent - use both agent id and thread id
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/runs?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
--data '{
"assistant_id": "asst_123456789"
}'
# List the messages in the thread using thread ID
curl --request GET --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/messages?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
# Delete agent once done using agent id
curl --request DELETE --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/assistants/asst_123456789?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
將檔案新增至代理程式
現在讓我們新增檔案搜尋工具,讓我們能夠進行知識擷取。
- 下載 product_info_1.md ,以提供給您的代理程式。
- 在代理程式的 [ 設定 ] 窗格中,視需要向下捲動以尋找 知識。
- 選取 ,然後新增。
- 選取 [檔案] 以上傳 product_info_1.md 檔案。
- 選取 [選取本機檔案] 底下的 [新增檔案]。
- 選取 [上傳並儲存]。
- 變更您的代理程式指示,例如「您是一個有用的小幫手,而且可以從上傳的檔案搜尋資訊」。
- 您可以問個問題,例如:「您好,您知道哪些 Contoso 產品?」
- 若要新增更多檔案,請在 AgentVectorStore 上選取 ... ,然後選取 [ 管理]。
以此程式碼中的 endpoint
取代您的端點:
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.ai.agents.models import ListSortOrder, FileSearchTool
project = AIProjectClient(
endpoint="https://your-foundry-resource-name.ai.azure.com/api/projects/project-name",
credential=DefaultAzureCredential(),
)
# Upload file and create vector store
file = project.agents.files.upload(file_path="./product_info_1.md", purpose=FilePurpose.AGENTS)
vector_store = project.agents.vector_stores.create_and_poll(file_ids=[file.id], name="my_vectorstore")
# Create file search tool and agent
file_search = FileSearchTool(vector_store_ids=[vector_store.id])
agent = project.agents.create_agent(
model="gpt-4o",
name="my-assistant",
instructions="You are a helpful assistant and can search information from uploaded files",
tools=file_search.definitions,
tool_resources=file_search.resources,
)
# Create thread and process user message
thread = project.agents.threads.create()
project.agents.messages.create(thread_id=thread.id, role="user", content="Hello, what Contoso products do you know?")
run = project.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
# Handle run status
if run.status == "failed":
print(f"Run failed: {run.last_error}")
# Print thread messages
messages = project.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING)
for message in messages:
if message.run_id == run.id and message.text_messages:
print(f"{message.role}: {message.text_messages[-1].text.value}")
# Cleanup resources
project.agents.vector_stores.delete(vector_store.id)
project.agents.files.delete(file_id=file.id)
project.agents.delete_agent(agent.id)
package com.azure.ai.foundry.samples;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import com.azure.ai.agents.persistent.PersistentAgentsClient;
import com.azure.ai.agents.persistent.PersistentAgentsClientBuilder;
import com.azure.ai.agents.persistent.PersistentAgentsAdministrationClient;
import com.azure.ai.agents.persistent.models.CreateAgentOptions;
import com.azure.ai.agents.persistent.models.CreateThreadAndRunOptions;
import com.azure.ai.agents.persistent.models.PersistentAgent;
import com.azure.ai.agents.persistent.models.ThreadRun;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.DefaultAzureCredentialBuilder;
/**
* Sample demonstrating agent creation with document capabilities using Azure AI Agents Persistent SDK.
*
* This sample shows how to:
* - Set up authentication with Azure credentials
* - Create a temporary document file for demonstration purposes
* - Create a persistent agent with custom instructions for document search
* - Start a thread and run with the agent that can access document content
* - Work with file-based knowledge sources for agent interactions
*
* Environment variables:
* - AZURE_ENDPOINT: Optional fallback. The base endpoint for your Azure AI service if PROJECT_ENDPOINT is not provided.
* - PROJECT_ENDPOINT: Required. The endpoint for your Azure AI Project.
* - MODEL_DEPLOYMENT_NAME: Optional. The model deployment name (defaults to "gpt-4o").
* - AGENT_NAME: Optional. The name to give to the created agent (defaults to "java-file-search-agent").
* - AGENT_INSTRUCTIONS: Optional. The instructions for the agent (defaults to document-focused instructions).
*
* Note: This sample demonstrates the creation of an agent that can process document content.
* In a real-world scenario, you might want to integrate with Azure AI Search or similar services
* for more advanced document processing capabilities.
*
* SDK Features Demonstrated:
* - Using the Azure AI Agents Persistent SDK (com.azure:azure-ai-agents-persistent:1.0.0-beta.2)
* - Creating an authenticated client with DefaultAzureCredential
* - Using the PersistentAgentsClientBuilder for client instantiation
* - Working with the PersistentAgentsAdministrationClient for agent management
* - Creating temporary document files for agent access
* - Adding document knowledge sources to agents
* - Creating document-aware agents that can search and reference content
* - Starting threads and runs for document-based Q&A
* - Error handling for Azure service and file operations
*/
public class FileSearchAgentSample {
private static final ClientLogger logger = new ClientLogger(FileSearchAgentSample.class);
public static void main(String[] args) {
// Load environment variables with proper error handling
String endpoint = System.getenv("AZURE_ENDPOINT");
String projectEndpoint = System.getenv("PROJECT_ENDPOINT");
String modelName = System.getenv("MODEL_DEPLOYMENT_NAME");
String agentName = System.getenv("AGENT_NAME");
String instructions = System.getenv("AGENT_INSTRUCTIONS");
// Check for required endpoint configuration
if (projectEndpoint == null && endpoint == null) {
String errorMessage = "Environment variables not configured. Required: either PROJECT_ENDPOINT or AZURE_ENDPOINT must be set.";
logger.error("ERROR: {}", errorMessage);
logger.error("Please set your environment variables or create a .env file. See README.md for details.");
return;
}
// Set defaults for optional parameters
if (modelName == null) {
modelName = "gpt-4o";
logger.info("No MODEL_DEPLOYMENT_NAME provided, using default: {}", modelName);
}
if (agentName == null) {
agentName = "java-file-search-agent";
logger.info("No AGENT_NAME provided, using default: {}", agentName);
}
if (instructions == null) {
instructions = "You are a helpful assistant that can answer questions about documents.";
logger.info("No AGENT_INSTRUCTIONS provided, using default instructions: {}", instructions);
}
logger.info("Building DefaultAzureCredential");
var credential = new DefaultAzureCredentialBuilder().build();
// Use AZURE_ENDPOINT as fallback if PROJECT_ENDPOINT not set
String finalEndpoint = projectEndpoint != null ? projectEndpoint : endpoint;
logger.info("Using endpoint: {}", finalEndpoint);
try {
// Build the general agents client with proper error handling
logger.info("Creating PersistentAgentsClient with endpoint: {}", finalEndpoint);
PersistentAgentsClient agentsClient = new PersistentAgentsClientBuilder()
.endpoint(finalEndpoint)
.credential(credential)
.buildClient();
// Derive the administration client
logger.info("Getting PersistentAgentsAdministrationClient");
PersistentAgentsAdministrationClient adminClient =
agentsClient.getPersistentAgentsAdministrationClient();
// Create sample document for demonstration
Path tmpFile = createSampleDocument();
logger.info("Created sample document at: {}", tmpFile);
String filePreview = Files.readString(tmpFile).substring(0, 200) + "...";
logger.info("{}", filePreview);
// Create the agent with proper configuration
logger.info("Creating agent with name: {}, model: {}", agentName, modelName);
PersistentAgent agent = adminClient.createAgent(
new CreateAgentOptions(modelName)
.setName(agentName)
.setInstructions(instructions)
);
logger.info("Agent ID: {}", agent.getId());
logger.info("Agent model: {}", agent.getModel());
// Start a thread and run on the general client
logger.info("Creating thread and run with agent ID: {}", agent.getId());
ThreadRun threadRun = agentsClient.createThreadAndRun(
new CreateThreadAndRunOptions(agent.getId())
);
logger.info("ThreadRun ID: {}", threadRun.getThreadId());
// Display success message
logger.info("\nDemo completed successfully!");
} catch (HttpResponseException e) {
// Handle service-specific errors with detailed information
int statusCode = e.getResponse().getStatusCode();
logger.error("Service error {}: {}", statusCode, e.getMessage());
logger.error("Refer to the Azure AI Agents documentation for troubleshooting information.");
} catch (IOException e) {
// Handle IO exceptions specifically for file operations
logger.error("I/O error while creating sample document: {}", e.getMessage(), e);
} catch (Exception e) {
// Handle general exceptions
logger.error("Error in file search agent sample: {}", e.getMessage(), e);
}
}
/**
* Creates a sample markdown document with cloud computing information.
*
* This method demonstrates:
* - Creating a temporary file that will be automatically deleted when the JVM exits
* - Writing structured markdown content to the file
* - Logging file creation and preview of content
*
* In a real application, you might read existing files or create more complex documents.
* You could also upload them to a document storage service for persistent access.
*
* @return Path to the created temporary file
* @throws IOException if an I/O error occurs during file creation or writing
*/
private static Path createSampleDocument() throws IOException {
logger.info("Creating sample document");
String content = """
# Cloud Computing Overview
Cloud computing is the delivery of computing services over the internet, including servers, storage,
databases, networking, software, analytics, and intelligence. Cloud services offer faster innovation,
flexible resources, and economies of scale.
## Key Cloud Service Models
1. **Infrastructure as a Service (IaaS)** - Provides virtualized computing resources
2. **Platform as a Service (PaaS)** - Provides hardware and software tools over the internet
3. **Software as a Service (SaaS)** - Delivers software applications over the internet
## Major Cloud Providers
- Microsoft Azure
- Amazon Web Services (AWS)
- Google Cloud Platform (GCP)
- IBM Cloud
## Benefits of Cloud Computing
- Cost efficiency
- Scalability
- Reliability
- Performance
- Security
""";
Path tempFile = Files.createTempFile("cloud-doc", ".md");
Files.writeString(tempFile, content);
logger.info("Sample document created at: {}", tempFile);
return tempFile;
}
}
// Upload a file named product_info_1.md
console.log(`\n==================== 🕵️ FILE AGENT ====================`);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, '../data/product_info_1.md');
const fileStream = fs.createReadStream(filePath);
fileStream.on('data', (chunk) => {
console.log(`Read ${chunk.length} bytes of data.`);
});
const file = await client.agents.files.upload(fileStream, 'assistants', {
fileName: 'product_info_1.md'
});
console.log(`Uploaded file, ID: ${file.id}`);
const vectorStore = await client.agents.vectorStores.create({
fileIds: [file.id],
name: 'my_vectorstore'
});
console.log('\n---------------- 🗃️ Vector Store Info ----------------');
console.table([
{
'Vector Store ID': vectorStore.id,
'Usage (bytes)': vectorStore.usageBytes,
'File Count': vectorStore.fileCounts?.total ?? 'N/A'
}
]);
// Create an Agent and a FileSearch tool
const fileSearchTool = ToolUtility.createFileSearchTool([vectorStore.id]);
const fileAgent = await client.agents.createAgent(deployment, {
name: 'my-file-agent',
instructions: 'You are a helpful assistant and can search information from uploaded files',
tools: [fileSearchTool.definition],
toolResources: fileSearchTool.resources,
});
// Create a thread and message
const fileSearchThread = await client.agents.threads.create({ toolResources: fileSearchTool.resources });
const filePrompt = 'What are the steps to setup the TrailMaster X4 Tent?';
console.log(`\n---------------- 📝 User Prompt ---------------- \n${filePrompt}`);
await client.agents.messages.create(fileSearchThread.id, 'user', filePrompt);
// Create run
let fileSearchRun = await client.agents.runs.create(fileSearchThread.id, fileAgent.id).stream();
for await (const eventMessage of fileSearchRun) {
if (eventMessage.event === DoneEvent.Done) {
console.log(`Run completed: ${eventMessage.data}`);
}
if (eventMessage.event === ErrorEvent.Error) {
console.log(`An error occurred. ${eventMessage.data}`);
}
}
const fileSearchMessagesIterator = await client.agents.messages.list(fileSearchThread.id);
const fileAssistantMessage = await getAssistantMessage(fileSearchMessagesIterator);
console.log(`\n---------------- 💬 Response ---------------- \n`);
printAssistantMessage(fileAssistantMessage);
// Clean up
console.log(`\n---------------- 🧹 Clean Up File Agent ----------------`);
client.agents.vectorStores.delete(vectorStore.id);
client.agents.files.delete(file.id);
client.agents.deleteAgent(fileAgent.id);
console.log(`Deleted VectorStore, File, and FileAgent. FileAgent ID: ${fileAgent.id}`);
// Upload a file named product_info_1.md
console.log(`\n==================== 🕵️ FILE AGENT ====================`);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.join(__dirname, '../data/product_info_1.md');
const fileStream = fs.createReadStream(filePath);
fileStream.on('data', (chunk: string | Buffer) => {
console.log(`Read ${chunk.length} bytes of data.`);
});
const file = await client.agents.files.upload(fileStream, 'assistants', {
fileName: 'product_info_1.md'
});
console.log(`Uploaded file, ID: ${file.id}`);
const vectorStore = await client.agents.vectorStores.create({
fileIds: [file.id],
name: 'my_vectorstore'
});
console.log('\n---------------- 🗃️ Vector Store Info ----------------');
console.table([
{
'Vector Store ID': vectorStore.id,
'Usage (bytes)': vectorStore.usageBytes,
'File Count': vectorStore.fileCounts?.total ?? 'N/A'
}
]);
// Create an Agent and a FileSearch tool
const fileSearchTool = ToolUtility.createFileSearchTool([vectorStore.id]);
const fileAgent = await client.agents.createAgent(deployment, {
name: 'my-file-agent',
instructions: 'You are a helpful assistant and can search information from uploaded files',
tools: [fileSearchTool.definition],
toolResources: fileSearchTool.resources
});
// Create a thread and message
const fileSearchThread = await client.agents.threads.create({ toolResources: fileSearchTool.resources });
const filePrompt = 'What are the steps to setup the TrailMaster X4 Tent?';
console.log(`\n---------------- 📝 User Prompt ---------------- \n${filePrompt}`);
await client.agents.messages.create(fileSearchThread.id, 'user', filePrompt);
// Create run
let fileSearchRun = await client.agents.runs.create(fileSearchThread.id, fileAgent.id).stream();
for await (const eventMessage of fileSearchRun) {
if (eventMessage.event === DoneEvent.Done) {
console.log(`Run completed: ${eventMessage.data}`);
}
if (eventMessage.event === ErrorEvent.Error) {
console.log(`An error occurred. ${eventMessage.data}`);
}
}
const fileSearchMessagesIterator = await client.agents.messages.list(fileSearchThread.id);
const fileAssistantMessage = await getAssistantMessage(fileSearchMessagesIterator);
console.log(`\n---------------- 💬 Response ---------------- \n`);
printAssistantMessage(fileAssistantMessage);
// Clean up
console.log(`\n---------------- 🧹 Clean Up File Agent ----------------`);
client.agents.vectorStores.delete(vectorStore.id);
client.agents.files.delete(file.id);
client.agents.deleteAgent(fileAgent.id);
console.log(`Deleted VectorStore, File, and FileAgent. FileAgent ID: ${fileAgent.id}`);
using Azure;
using Azure.Identity;
using Azure.AI.Agents.Persistent;
// Creating the Client for agents and vector stores
var projectEndpoint = System.Environment.GetEnvironmentVariable("AZURE_AI_ENDPOINT");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("AZURE_AI_MODEL");
PersistentAgentsClient client = new(projectEndpoint, new DefaultAzureCredential());
PersistentAgentFileInfo uploadedAgentFile = client.Files.UploadFile(
filePath: "product_info_1.md",
purpose: PersistentAgentFilePurpose.Agents);
// Create a vector store with the file and wait for it to be processed.
// If you do not specify a vector store, create_message will create a vector store with a default expiration policy of seven days after they were last active
Dictionary<string, string> fileIds = new()
{
{ uploadedAgentFile.Id, uploadedAgentFile.Filename }
};
PersistentAgentsVectorStore vectorStore = client.VectorStores.CreateVectorStore(
name: "my_vector_store");
// Add file ID to vector store.
VectorStoreFile vctFile = client.VectorStores.CreateVectorStoreFile(
vectorStoreId: vectorStore.Id,
fileId: uploadedAgentFile.Id
);
Console.WriteLine($"Added file to vector store. The id file in the vector store is {vctFile.Id}.");
FileSearchToolResource fileSearchToolResource = new FileSearchToolResource();
fileSearchToolResource.VectorStoreIds.Add(vectorStore.Id);
// Create an Agent with toolResources and process Agent run
PersistentAgent agent = client.Administration.CreateAgent(
model: modelDeploymentName,
name: "SDK Test Agent - Retrieval",
instructions: "You are a helpful agent that can help fetch data from files you know about.",
tools: new List<ToolDefinition> { new FileSearchToolDefinition() },
toolResources: new ToolResources() { FileSearch = fileSearchToolResource });
// Create thread for communication
PersistentAgentThread thread = client.Threads.CreateThread();
// Create message to thread
PersistentThreadMessage messageResponse = client.Messages.CreateMessage(
thread.Id,
MessageRole.User,
"Can you give me information on how to mount the product?");
// Run the Agent
ThreadRun run = client.Runs.CreateRun(thread, agent);
// Wait for the run to complete
// This is a blocking call, so it will wait until the run is completed
do
{
Thread.Sleep(TimeSpan.FromMilliseconds(500));
run = client.Runs.GetRun(thread.Id, run.Id);
}
while (run.Status == RunStatus.Queued
|| run.Status == RunStatus.InProgress);
// Create a list of messages in the thread and write them to the console.
Pageable<PersistentThreadMessage> messages = client.Messages.GetMessages(
threadId: thread.Id,
order: ListSortOrder.Ascending
);
WriteMessages(messages, fileIds);
// Delete the thread and agent after use
client.VectorStores.DeleteVectorStore(vectorStore.Id);
client.Files.DeleteFile(uploadedAgentFile.Id);
client.Threads.DeleteThread(thread.Id);
client.Administration.DeleteAgent(agent.Id);
// Helper method to write messages to the console
static void WriteMessages(IEnumerable<PersistentThreadMessage> messages, Dictionary<string, string> fileIds)
{
foreach (PersistentThreadMessage threadMessage in messages)
{
Console.Write($"{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss} - {threadMessage.Role,10}: ");
foreach (MessageContent contentItem in threadMessage.ContentItems)
{
if (contentItem is MessageTextContent textItem)
{
if (threadMessage.Role == MessageRole.Agent && textItem.Annotations.Count > 0)
{
string strMessage = textItem.Text;
foreach (MessageTextAnnotation annotation in textItem.Annotations)
{
if (annotation is MessageTextFilePathAnnotation pathAnnotation)
{
strMessage = replaceReferences(fileIds, pathAnnotation.FileId, pathAnnotation.Text, strMessage);
}
else if (annotation is MessageTextFileCitationAnnotation citationAnnotation)
{
strMessage = replaceReferences(fileIds, citationAnnotation.FileId, citationAnnotation.Text, strMessage);
}
}
Console.Write(strMessage);
}
else
{
Console.Write(textItem.Text);
}
}
else if (contentItem is MessageImageFileContent imageFileItem)
{
Console.Write($"<image from ID: {imageFileItem.FileId}");
}
Console.WriteLine();
}
}
}
// Helper method to replace file references in the text
static string replaceReferences(Dictionary<string, string> fileIds, string fileID, string placeholder, string text)
{
if (fileIds.TryGetValue(fileID, out string replacement))
return text.Replace(placeholder, $" [{replacement}]");
else
return text.Replace(placeholder, $" [{fileID}]");
}
將 YOUR-FOUNDRY-RESOURCE-NAME
和 YOUR-PROJECT-NAME
替換為您的值。
#Upload the file
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/files?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-f purpose="assistant" \
-f file="@product_info_1.md" #File object (not file name) to be uploaded.
#Lets say file ID created is assistant-123456789. Use this in the next step
# create vector store
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/vector_stores?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
-d '{
"name": "my_vectorstore",
"file_ids": ["assistant-123456789"]
}'
#Lets say Vector Store ID created is vs_123456789. Use this in the next step
# Create Agent for File Search
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/assistants?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
-d '{
"model": "gpt-4o",
"name": "my-assistant",
"instructions": "You are a helpful assistant and can search information from uploaded files",
"tools": [{"type": "file_search"}],
"tool_resources": {"file_search": {"vector_store_ids": ["vs_123456789"]}}
}'
#Lets say agent ID created is asst_123456789. Use this to run the agent
# Create thread
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
#Lets say thread ID created is thread_123456789. Use this in the next step
# Create message using thread ID
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/messages?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
-d '{
"role": "user",
"content": "Hello, what Contoso products do you know?"
}'
# Run thread with the agent - use both agent id and thread id
curl --request POST --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/runs?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json' \
--data '{
"assistant_id": "asst_123456789"
}'
# List the messages in the thread using thread ID
curl --request GET --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/threads/thread_123456789/messages?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
# Delete agent once done using agent id
curl --request DELETE --url 'https://YOUR-FOUNDRY-RESOURCE-NAME.services.ai.azure.com/api/projects/YOUR-PROJECT-NAME/assistants/asst_123456789?api-version=v1' \
-h 'authorization: Bearer $AZURE_AI_AUTH_TOKEN' \
-h 'content-type: application/json'
清理資源
如果您不再需要您所建立的任何資源,請刪除與您的專案相關聯的資源群組。
在 Azure AI Foundry 入口網站中,選取右上角的項目名稱。 然後選取資源群組的連結,以在 Azure 入口網站中開啟它。 選取資源群組,然後選取 [ 刪除]。 確認您想要刪除資源群組。
後續步驟
意見反應
此頁面對您有幫助嗎?