- Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathsample_chat_completions.py
52 lines (41 loc) · 1.74 KB
/
sample_chat_completions.py
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 Corporation.
# Licensed under the MIT License.
# ------------------------------------
"""
DESCRIPTION:
This sample demonstrates how to get a chat completions response from
the service using a synchronous client.
USAGE:
python sample_chat_completions.py
Set these two environment variables before running the sample:
1) AZURE_AI_CHAT_ENDPOINT - Your endpoint URL, in the form
https://<your-deployment-name>.<your-azure-region>.inference.ai.azure.com
where `your-deployment-name` is your unique AI Model deployment name, and
`your-azure-region` is the Azure region where your model is deployed.
2) AZURE_AI_CHAT_KEY - Your model key (a 32-character string). Keep it secret.
"""
defsample_chat_completions():
importos
try:
endpoint=os.environ["AZURE_AI_CHAT_ENDPOINT"]
key=os.environ["AZURE_AI_CHAT_KEY"]
exceptKeyError:
print("Missing environment variable 'AZURE_AI_CHAT_ENDPOINT' or 'AZURE_AI_CHAT_KEY'")
print("Set them before running this sample.")
exit()
# [START chat_completions]
fromazure.ai.inferenceimportChatCompletionsClient
fromazure.ai.inference.modelsimportSystemMessage, UserMessage
fromazure.core.credentialsimportAzureKeyCredential
client=ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(key))
response=client.complete(
messages=[
SystemMessage(content="You are a helpful assistant."),
UserMessage(content="How many feet are in a mile?"),
]
)
print(response.choices[0].message.content)
# [END chat_completions]
if__name__=="__main__":
sample_chat_completions()