Skip to content

Commit e64d706

Browse files
Merge pull request #276050 from kristapratico/patch-1
Update assistants-python.md
2 parents 229d6dd + 3e304fb commit e64d706

File tree

1 file changed

+106
-56
lines changed

1 file changed

+106
-56
lines changed

articles/ai-services/openai/includes/assistants-python.md

Lines changed: 106 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
title: 'Quickstart: Use the OpenAI Service via the Python SDK'
33
titleSuffix: Azure OpenAI Service
4-
description: Walkthrough on how to get started with Azure OpenAI and make your first completions call with the Python SDK.
4+
description: Walkthrough on how to get started with Azure OpenAI and make your first Assistants call with the Python SDK.
55
manager: nitinme
66
author: mrbullwinkle
77
ms.author: mbullwin
88
ms.service: azure-ai-openai
99
ms.topic: include
10-
ms.date: 02/01/2024
10+
ms.date: 05/22/2024
1111
---
1212

13-
[Reference documentation](/java/api/overview/azure/ai-openai-assistants-readme?context=/azure/ai-services/openai/context/context) | <a href="https://github.com/openai/openai-python" target="_blank">Library source code</a> | <a href="https://pypi.org/project/openai/" target="_blank">Package (PyPi)</a> |
13+
[Reference documentation](https://platform.openai.com/docs/api-reference/assistants/createAssistant) | <a href="https://github.com/openai/openai-python" target="_blank">Library source code</a> | <a href="https://pypi.org/project/openai/" target="_blank">Package (PyPi)</a> |
1414

1515
## Prerequisites
1616

@@ -19,18 +19,32 @@ ms.date: 02/01/2024
1919

2020
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at <a href="https://aka.ms/oai/access" target="_blank">https://aka.ms/oai/access</a>. Open an issue on this repo to contact us if you have an issue.
2121
- <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>
22-
- The following Python libraries: os, json, openai (Version 1.x is required)
23-
- [Jupyter Notebooks](https://jupyter.org/)
22+
- The following Python libraries: os, openai (Version 1.x is required)
23+
- [Azure CLI](/cli/azure/install-azure-cli) used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
2424
- Azure OpenAI Assistants are currently available in Sweden Central, East US 2, and Australia East. For more information about model availability in those regions, see the [models guide](../concepts/models.md).
2525
- We recommend reviewing the [Responsible AI transparency note](/legal/cognitive-services/openai/transparency-note?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext&tabs=text) and other [Responsible AI resources](/legal/cognitive-services/openai/overview?context=%2Fazure%2Fai-services%2Fopenai%2Fcontext%2Fcontext) to familiarize yourself with the capabilities and limitations of the Azure OpenAI Service.
2626
- An Azure OpenAI resource with the `gpt-4 (1106-preview)` model deployed was used testing this example.
2727

28+
## Passwordless authentication is recommended
29+
30+
For passwordless authentication, you need to
31+
32+
1. Use the [azure-identity](https://pypi.org/project/azure-identity/) package.
33+
2. Assign the `Cognitive Services User` role to your user account. This can be done in the Azure portal under **Access control (IAM)** > **Add role assignment**.
34+
3. Sign in with the Azure CLI such as `az login`.
35+
2836
## Set up
2937

30-
Install the OpenAI Python client library with:
38+
1. Install the OpenAI Python client library with:
39+
40+
```console
41+
pip install openai
42+
```
43+
44+
2. For the **recommended** passwordless authentication:
3145

3246
```console
33-
pip install openai==v1.20.0
47+
pip install azure-identity
3448
```
3549

3650
[!INCLUDE [Assistants v2 note](./assistants-v2-note.md)]
@@ -71,26 +85,29 @@ In our code we are going to specify the following values:
7185

7286
An individual assistant can access up to 128 tools including `code interpreter`, as well as any custom tools you create via [functions](../how-to/assistant-functions.md).
7387

74-
Create and run an assistant with the following:
88+
### Create the Python app
89+
90+
Sign in to Azure with `az login` then create and run an assistant with the following **recommended** passwordless Python example:
7591

7692
```python
7793
import os
78-
import time
79-
import json
94+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
8095
from openai import AzureOpenAI
81-
96+
97+
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
98+
8299
client = AzureOpenAI(
83-
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
84-
api_version="2024-02-15-preview",
85-
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
86-
)
100+
azure_ad_token_provider=token_provider,
101+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
102+
api_version="2024-05-01-preview",
103+
)
87104

88105
# Create an assistant
89106
assistant = client.beta.assistants.create(
90107
name="Math Assist",
91108
instructions="You are an AI assistant that can write code to help answer math questions.",
92109
tools=[{"type": "code_interpreter"}],
93-
model="gpt-4-1106-preview" #You must replace this value with the deployment name for your model.
110+
model="gpt-4-1106-preview" # You must replace this value with the deployment name for your model.
94111
)
95112

96113
# Create a thread
@@ -103,41 +120,75 @@ message = client.beta.threads.messages.create(
103120
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
104121
)
105122

106-
# Run the thread
107-
run = client.beta.threads.runs.create(
108-
thread_id=thread.id,
109-
assistant_id=assistant.id,
123+
# Run the thread and poll for the result
124+
run = client.beta.threads.runs.create_and_poll(
125+
thread_id=thread.id,
126+
assistant_id=assistant.id,
127+
instructions="Please address the user as Jane Doe. The user has a premium account.",
110128
)
111129

112-
# Retrieve the status of the run
113-
run = client.beta.threads.runs.retrieve(
114-
thread_id=thread.id,
115-
run_id=run.id
130+
print("Run completed with status: " + run.status)
131+
132+
if run.status == "completed":
133+
messages = client.beta.threads.messages.list(thread_id=thread.id)
134+
print(messages.to_json(indent=2))
135+
```
136+
137+
To use the service API key for authentication, you can create and run an assistant with the following Python example:
138+
139+
```python
140+
import os
141+
from openai import AzureOpenAI
142+
143+
client = AzureOpenAI(
144+
api_key=os.environ["AZURE_OPENAI_API_KEY"],
145+
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
146+
api_version="2024-05-01-preview",
116147
)
117148

118-
status = run.status
149+
# Create an assistant
150+
assistant = client.beta.assistants.create(
151+
name="Math Assist",
152+
instructions="You are an AI assistant that can write code to help answer math questions.",
153+
tools=[{"type": "code_interpreter"}],
154+
model="gpt-4-1106-preview" # You must replace this value with the deployment name for your model.
155+
)
119156

120-
# Wait till the assistant has responded
121-
while status not in ["completed", "cancelled", "expired", "failed"]:
122-
time.sleep(5)
123-
run = client.beta.threads.runs.retrieve(thread_id=thread.id,run_id=run.id)
124-
status = run.status
157+
# Create a thread
158+
thread = client.beta.threads.create()
125159

126-
messages = client.beta.threads.messages.list(
127-
thread_id=thread.id
160+
# Add a user question to the thread
161+
message = client.beta.threads.messages.create(
162+
thread_id=thread.id,
163+
role="user",
164+
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
128165
)
129166

130-
print(messages.model_dump_json(indent=2))
167+
# Run the thread and poll for the result
168+
run = client.beta.threads.runs.create_and_poll(
169+
thread_id=thread.id,
170+
assistant_id=assistant.id,
171+
instructions="Please address the user as Jane Doe. The user has a premium account.",
172+
)
173+
174+
print("Run completed with status: " + run.status)
175+
176+
if run.status == "completed":
177+
messages = client.beta.threads.messages.list(thread_id=thread.id)
178+
print(messages.to_json(indent=2))
131179
```
132180

133181
## Output
134182

183+
Run completed with status: completed
184+
135185
```json
136186
{
137187
"data": [
138188
{
139-
"id": "msg_XOL8597uuV6zIEgaqZtI0KD3",
140-
"assistant_id": "asst_WKFOCDJ42Ld1bVUfS8w2pt6E",
189+
"id": "msg_4SuWxTubHsHpt5IlBTO5Hyw9",
190+
"assistant_id": "asst_cYqL1RuwLyFV3HU1gkaE2k0K",
191+
"attachments": [],
141192
"content": [
142193
{
143194
"text": {
@@ -147,17 +198,17 @@ print(messages.model_dump_json(indent=2))
147198
"type": "text"
148199
}
149200
],
150-
"created_at": 1705892759,
151-
"file_ids": [],
201+
"created_at": 1716397091,
152202
"metadata": {},
153203
"object": "thread.message",
154204
"role": "assistant",
155-
"run_id": "run_TSmF4LoU6bX4SD3xp5xDr1ey",
156-
"thread_id": "thread_hCOKdEZy1diZAAzwDudRqGRc"
205+
"run_id": "run_hFgBPbUtO8ZNTnNPC8PgpH1S",
206+
"thread_id": "thread_isb7spwRycI5ueT9E7357aOm"
157207
},
158208
{
159-
"id": "msg_F25tb90W5xTPqSn4KgU4aMsb",
209+
"id": "msg_Z32w2E7kY5wEWhZqQWxIbIUB",
160210
"assistant_id": null,
211+
"attachments": [],
161212
"content": [
162213
{
163214
"text": {
@@ -167,18 +218,17 @@ print(messages.model_dump_json(indent=2))
167218
"type": "text"
168219
}
169220
],
170-
"created_at": 1705892751,
171-
"file_ids": [],
221+
"created_at": 1716397025,
172222
"metadata": {},
173223
"object": "thread.message",
174224
"role": "user",
175225
"run_id": null,
176-
"thread_id": "thread_hCOKdEZy1diZAAzwDudRqGRc"
226+
"thread_id": "thread_isb7spwRycI5ueT9E7357aOm"
177227
}
178228
],
179229
"object": "list",
180-
"first_id": "msg_XOL8597uuV6zIEgaqZtI0KD3",
181-
"last_id": "msg_F25tb90W5xTPqSn4KgU4aMsb",
230+
"first_id": "msg_4SuWxTubHsHpt5IlBTO5Hyw9",
231+
"last_id": "msg_Z32w2E7kY5wEWhZqQWxIbIUB",
182232
"has_more": false
183233
}
184234
```
@@ -188,17 +238,17 @@ print(messages.model_dump_json(indent=2))
188238
In this example we create an assistant with code interpreter enabled. When we ask the assistant a math question it translates the question into python code and executes the code in sandboxed environment in order to determine the answer to the question. The code the model creates and tests to arrive at an answer is:
189239

190240
```python
191-
from sympy import symbols, Eq, solve
192-
193-
# Define the variable
194-
x = symbols('x')
195-
196-
# Define the equation
197-
equation = Eq(3*x + 11, 14)
198-
199-
# Solve the equation
200-
solution = solve(equation, x)
201-
solution
241+
from sympy import symbols, Eq, solve
242+
243+
# Define the variable
244+
x = symbols('x')
245+
246+
# Define the equation
247+
equation = Eq(3*x + 11, 14)
248+
249+
# Solve the equation
250+
solution = solve(equation, x)
251+
solution
202252
```
203253

204254
It is important to remember that while code interpreter gives the model the capability to respond to more complex queries by converting the questions into code and running that code iteratively in the Python sandbox until it reaches a solution, you still need to validate the response to confirm that the model correctly translated your question into a valid representation in code.

0 commit comments

Comments
 (0)