Skip to content

Commit 3609f86

Browse files
committed
updating quickstart
1 parent cab4c62 commit 3609f86

File tree

2 files changed

+51
-81
lines changed

2 files changed

+51
-81
lines changed

articles/ai-services/agents/includes/quickstart-csharp.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ For example, your connection string may look something like:
6767

6868
`https://myresource.services.ai.azure.com/api/projects/myproject`
6969

70-
Set this connection string as an environment variable named `ENDPOINT_STRING`.
70+
Set this connection string as an environment variable named `PROJECT_ENDPOINT`.
7171

7272

7373
```csharp
@@ -84,7 +84,7 @@ public class Sample_Agent
8484
{
8585
static async Task Main()
8686
{
87-
var connectionString = Environment.GetEnvironmentVariable("ENDPOINT_STRING");
87+
var connectionString = Environment.GetEnvironmentVariable("PROJECT_ENDPOINT");
8888

8989
AgentsClient client = new AgentsClient(connectionString, new DefaultAzureCredential());
9090

articles/ai-services/agents/includes/quickstart-python.md

Lines changed: 49 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -48,104 +48,74 @@ Next, to authenticate your API requests and run the program, use the [az login](
4848
az login
4949
```
5050

51-
Use the following code to create and run an agent. To run this code, you will need to create a connection string using information from your project. This string is in the format:
51+
Use the following code to create and run an agent. To run this code, you will need to get the endpoint for your project. This string is in the format:
5252

53-
`<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>`
53+
`https://<AIFoundryResourceName>.services.ai.azure.com/api/projects/<ProjectName>`
5454

55-
[!INCLUDE [connection-string-portal](connection-string-portal.md)]
56-
57-
`HostName` can be found by navigating to your `discovery_url` and removing the leading `https://` and trailing `/discovery`. To find your `discovery_url`, run this CLI command:
58-
59-
```azurecli
60-
az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
61-
```
55+
[!INCLUDE [endpoint-string-portal](endpoint-string-portal.md)]
6256

6357
For example, your connection string may look something like:
6458

65-
`eastus.api.azureml.ms;12345678-abcd-1234-9fc6-62780b3d3e05;my-resource-group;my-project-name`
59+
`https://myresource.services.ai.azure.com/api/projects/myproject`
60+
61+
Set this connection string as an environment variable named `PROJECT_ENDPOINT`.
6662

67-
Set this connection string as an environment variable named `PROJECT_CONNECTION_STRING`.
6863

6964
```python
7065
import os
71-
from azure.ai.projects import AIProjectClient
72-
from azure.ai.projects.models import CodeInterpreterTool
66+
import os, time
7367
from azure.identity import DefaultAzureCredential
74-
from typing import Any
68+
from azure.ai.projects import AIProjectClient
7569
from pathlib import Path
7670

77-
# Create an Azure AI Client from a connection string, copied from your Azure AI Foundry project.
78-
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>"
79-
# HostName can be found by navigating to your discovery_url and removing the leading "https://" and trailing "/discovery"
80-
# To find your discovery_url, run the CLI command: az ml workspace show -n {project_name} --resource-group {resource_group_name} --query discovery_url
81-
# Project Connection example: eastus.api.azureml.ms;12345678-abcd-1234-9fc6-62780b3d3e05;my-resource-group;my-project-name
82-
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
71+
# Retrieve the project endpoint from environment variables
72+
project_endpoint = os.environ["PROJECT_ENDPOINT"]
8373

84-
project_client = AIProjectClient.from_connection_string(
85-
credential=DefaultAzureCredential(), conn_str=os.environ["PROJECT_CONNECTION_STRING"]
74+
# Initialize the AIProjectClient
75+
project_client = AIProjectClient(
76+
endpoint=project_endpoint,
77+
credential=DefaultAzureCredential(),
78+
api_version="latest",
8679
)
8780

88-
with project_client:
89-
# Create an instance of the CodeInterpreterTool
90-
code_interpreter = CodeInterpreterTool()
81+
# Initialize the FunctionTool with user-defined functions
82+
functions = FunctionTool(functions=user_functions)
9183

92-
# The CodeInterpreterTool needs to be included in creation of the agent
84+
with project_client:
85+
# Create an agent with custom functions
9386
agent = project_client.agents.create_agent(
94-
model="gpt-4o-mini",
87+
model=os.environ["MODEL_DEPLOYMENT_NAME"],
9588
name="my-agent",
96-
instructions="You are helpful agent",
97-
tools=code_interpreter.definitions,
98-
tool_resources=code_interpreter.resources,
89+
instructions="You are a helpful agent"
9990
)
100-
print(f"Created agent, agent ID: {agent.id}")
91+
print(f"Created agent, ID: {agent.id}")
10192

102-
# Create a thread
103-
thread = project_client.agents.create_thread()
104-
print(f"Created thread, thread ID: {thread.id}")
93+
# Create a thread for communication
94+
thread = project_client.agents.threads.create()
95+
print(f"Created thread, ID: {thread.id}")
10596

106-
# Create a message
107-
message = project_client.agents.create_message(
108-
thread_id=thread.id,
109-
role="user",
110-
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",
111-
)
112-
print(f"Created message, message ID: {message.id}")
113-
114-
# Run the agent
115-
run = project_client.agents.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
116-
print(f"Run finished with status: {run.status}")
117-
118-
if run.status == "failed":
119-
# Check if you got "Rate limit is exceeded.", then you want to get more quota
120-
print(f"Run failed: {run.last_error}")
121-
122-
# Get messages from the thread
123-
messages = project_client.agents.list_messages(thread_id=thread.id)
124-
print(f"Messages: {messages}")
125-
126-
# Get the last message from the sender
127-
last_msg = messages.get_last_text_message_by_role("assistant")
128-
if last_msg:
129-
print(f"Last Message: {last_msg.text.value}")
130-
131-
# Generate an image file for the bar chart
132-
for image_content in messages.image_contents:
133-
print(f"Image File ID: {image_content.image_file.file_id}")
134-
file_name = f"{image_content.image_file.file_id}_image_file.png"
135-
project_client.agents.save_file(file_id=image_content.image_file.file_id, file_name=file_name)
136-
print(f"Saved image file to: {Path.cwd() / file_name}")
137-
138-
# Print the file path(s) from the messages
139-
for file_path_annotation in messages.file_path_annotations:
140-
print(f"File Paths:")
141-
print(f"Type: {file_path_annotation.type}")
142-
print(f"Text: {file_path_annotation.text}")
143-
print(f"File ID: {file_path_annotation.file_path.file_id}")
144-
print(f"Start Index: {file_path_annotation.start_index}")
145-
print(f"End Index: {file_path_annotation.end_index}")
146-
project_client.agents.save_file(file_id=file_path_annotation.file_path.file_id, file_name=Path(file_path_annotation.text).name)
147-
148-
# Delete the agent once done
149-
project_client.agents.delete_agent(agent.id)
150-
print("Deleted agent")
97+
# Create a message in the thread
98+
message = project_client.agents.messages.create(
99+
thread_id=thread.id,
100+
role="user", # Role of the message sender
101+
content="When was Microsoft founded?", # Message content
102+
)
103+
print(f"Created message, ID: {message['id']}")
104+
105+
# Create and process an agent run in the thread
106+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
107+
print(f"Run finished with status: {run.status}")
108+
109+
# Check if the run failed
110+
if run.status == "failed":
111+
print(f"Run failed: {run.last_error}")
112+
113+
# Fetch and log all messages from the thread
114+
messages = project_client.agents.messages.list(thread_id=thread.id)
115+
for message in messages.data:
116+
print(f"Role: {message.role}, Content: {message.content}")
117+
118+
# Delete the agent after use
119+
project_client.agents.delete_agent(agent.id)
120+
print("Deleted agent")
151121
```

0 commit comments

Comments
 (0)