參考文檔:
Quickstart - Get started using ChatGPT and GPT-4 with Azure OpenAI Service - Azure OpenAI Service | Microsoft Learn
先決條件
- Azure 訂閲
- 在所需的 Azure 訂閲中授予 Azure OpenAI 的訪問權限。
- 具有 或
gpt-35-turbo已部署的模型。
轉到 Azure OpenAI Studio
導航到 Azure OpenAI Studio https://oai.azure.com/,並使用有權訪問 OpenAI 資源的憑據登錄。在登錄工作流期間或之後,選擇相應的目錄、Azure 訂閲和 Azure OpenAI 資源。
已經部署了一些模型
在“部署”中選擇模型:
啓動聊天會話
選擇“從助理設置”下選擇“查看代碼”,將重播到目前為止整個對話背後的代碼:
#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://openainewone.openai.azure.com/"
openai.api_version = "2023-03-15-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")//需要替換系統參數或直接修改
response = openai.ChatCompletion.create(
engine="openaig35turbo",
messages = [{"role":"system","content":"You are an AI assistant that helps people find information."},{"role":"user","content":"推薦端午節的華東好去處"},{"role":"assistant","content":"1. 蘇州園林:蘇州的園林建築是中國傳統文化的重要代表之一,端午節可以到蘇州遊覽古典園林,感受傳統文化的魅力。\n\n2. 杭州西湖:西湖是杭州的標誌性景點,端午節可以欣賞西湖美景,品嚐當地的龍井茶。\n\n3. 上海迪士尼樂園:迪士尼樂園是全家人的好去處,端午節可以帶着孩子一起來到這裏,享受歡樂時光。\n\n4. 黃山:黃山是中國著名的山水風景區,端午節可以在這裏爬山、觀賞雲海,感受大自然的魅力。\n\n5. 千島湖:千島湖是一個美麗的水鄉景區,端午節可以在這裏遊船、釣魚、享受湖光山色。\n\n6. 烏鎮:烏鎮是一個古色古香的小鎮,端午節可以在這裏散步、品嚐當地美食、感受古鎮文化。\n\n7. 南京夫子廟:夫子廟是南京的著名古蹟,端午節可以在這裏遊覽、購物、品嚐當地小吃。\n\n8. 嘉興南湖:南湖是嘉興的標誌性景點,端午節可以在這裏遊船、欣賞景色、品嚐當地美食。"}],
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)
其中:openai.api_key = os.getenv("OPENAI_API_KEY")//需要替換系統參數或直接修改
Client USE Python 代碼
- 安裝好Python 3
-
Set up
Install the OpenAI Python client library with:pip install openai pip3 install openai // MAC OS use this commond
官方示例:
#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
openai.api_version = "2023-05-15"
openai.api_key = os.getenv("AZURE_OPENAI_KEY")
response = openai.ChatCompletion.create(
engine="gpt-35-turbo", # engine = "deployment_name".
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Does Azure OpenAI support customer managed keys?"},
{"role": "assistant", "content": "Yes, customer managed keys are supported by Azure OpenAI."},
{"role": "user", "content": "Do other Azure Cognitive Services support this too?"}
]
)
print(response)
print(response['choices'][0]['message']['content'])
結合實驗:
#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://openainewone.openai.azure.com/"
openai.api_version = "2023-03-15-preview"
openai.api_key = "c9bb1dd8326b493195abc16e53cc"
response = openai.ChatCompletion.create(
engine="openaig35turbo",
messages = [{"role":"system","content":"You are an AI assistant that helps people find information."},{"role":"user","content":"What's the ways to learn Azure OpenAI"}],
temperature=0.7,
max_tokens=800,
top_p=0.95,
frequency_penalty=0,
presence_penalty=0,
stop=None)
print(response)
PS C:\Users\v-yizhao1> & C:/Users/v-yizhao1/AppData/Local/Microsoft/WindowsApps/python3.11.exe "c:/Users/v-yizhao1/OneDrive - Microsoft/Documents/Python/OpenAI-Untitled-1.py"
{
"id": "chatcmpl-7ThltTRf2ABTKJOjidNYUPHaYcXKq",
"object": "chat.completion",
"created": 1687314125,
"model": "gpt-35-turbo",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "There are several ways to learn Azure OpenAI. Here are a few suggestions:\n\n1. Microsoft Learn: Microsoft Learn is an online learning platform that provides free courses and tutorials on Azure and OpenAI. You can learn at your own pace and earn badges and certifications.\n\n2. Azure AI Fundamentals certification: Microsoft offers a certification exam that covers the fundamentals of Azure AI. This certification can help you validate your skills and knowledge in this area.\n\n3. Online courses: There are several online courses available that cover Azure and OpenAI. Sites like Coursera, Udemy, and Pluralsight offer courses on these topics.\n\n4. Community resources: There are several online communities where you can connect with other professionals and learn from their experiences. Some examples include the Microsoft Azure community, the Azure AI community, and the OpenAI community.\n\n5. Books: There are several books available on Azure and OpenAI. Some popular titles include \"Azure for Architects\" by Ritesh Modi and \"Hands-On Machine Learning with Azure\" by Nishith Pathak."
}
}
],
"usage": {
"completion_tokens": 213,
"prompt_tokens": 33,
"total_tokens": 246
}
}
AI Answer:
"content": "There are several ways to learn Azure OpenAI. Here are a few suggestions:\n\n1. Microsoft Learn: Microsoft Learn is an online learning platform that provides free courses and tutorials on Azure and OpenAI. You can learn at your own pace and earn badges and certifications.\n\n2. Azure AI Fundamentals certification: Microsoft offers a certification exam that covers the fundamentals of Azure AI. This certification can help you validate your skills and knowledge in this area.\n\n3. Online courses: There are several online courses available that cover Azure and OpenAI. Sites like Coursera, Udemy, and Pluralsight offer courses on these topics.\n\n4. Community resources: There are several online communities where you can connect with other professionals and learn from their experiences. Some examples include the Microsoft Azure community, the Azure AI community, and the OpenAI community.\n\n5. Books: There are several books available on Azure and OpenAI. Some popular titles include \"Azure for Architects\" by Ritesh Modi and \"Hands-On Machine Learning with Azure\" by Nishith Pathak."
Deply to WEB
這一步部署失敗,系統並沒給出原因!