Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
52 lines (42 loc) · 1.9 KB
/
Copy pathclient.py
File metadata and controls
52 lines (42 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Copyright (c) Microsoft. All rights reserved.
"""Call a deployed Responses agent with stored, one-shot, and background requests."""
import os
from time import sleep
from azure.ai.projects import AIProjectClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
load_dotenv()
def main() -> None:
with (
AzureCliCredential() as credential,
AIProjectClient(
endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
credential=credential,
allow_preview=True,
) as project,
):
openai = project.get_openai_client( # ty: ignore[unresolved-attribute] # pyrefly: ignore
agent_name=os.environ["FOUNDRY_AGENT_NAME"]
)
conversation = openai.conversations.create()
first = openai.responses.create(input="Introduce yourself.", conversation=conversation.id, store=True)
second = openai.responses.create(
input="Answer briefly.",
conversation=conversation.id,
store=True,
max_output_tokens=300,
extra_body={"max_tokens": 150},
)
print(f"Stored response {first.id}; continued with {second.id}: {second.output_text}")
one_shot = openai.responses.create(input="Say hello.", store=False)
print(f"Unstored response (id cannot be retrieved): {one_shot.output_text}")
background = openai.responses.create(input="Write a report.", store=True, background=True)
response_id = background.id
while background.status in ("queued", "in_progress"):
sleep(2)
background = openai.responses.retrieve(response_id)
if background.status != "completed":
raise RuntimeError(f"Background response {response_id} ended with status {background.status}.")
print(f"Background response {response_id}: {background.output_text}")
if __name__ == "__main__":
main()