Azure OpenAI
本頁說明如何將 LangChain 與 Azure OpenAI 搭配使用。
Azure OpenAI API 與 OpenAI 的 API 相容。openai
Python 套件可讓您輕鬆使用 OpenAI 和 Azure OpenAI。您可以像呼叫 OpenAI 一樣呼叫 Azure OpenAI,但以下列例外情況除外。
API 配置
您可以設定 openai
套件,以使用環境變數來使用 Azure OpenAI。以下適用於 bash
# The API version you want to use: set this to `2023-12-01-preview` for the released version.
export OPENAI_API_VERSION=2023-12-01-preview
# The base URL for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource.
export AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com
# The API key for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource.
export AZURE_OPENAI_API_KEY=<your Azure OpenAI API key>
或者,您可以直接在執行中的 Python 環境中設定 API
import os
os.environ["OPENAI_API_VERSION"] = "2023-12-01-preview"
Azure Active Directory 驗證
您可以使用兩種方式來驗證 Azure OpenAI
- API 金鑰
- Azure Active Directory (AAD)
使用 API 金鑰是入門的最簡單方法。您可以在 Azure 入口網站的 Azure OpenAI 資源下找到您的 API 金鑰。
但是,如果您有複雜的安全性需求,您可能想要使用 Azure Active Directory。您可以在 此處 找到有關如何將 AAD 與 Azure OpenAI 搭配使用的更多資訊。
如果您是在本地端進行開發,則需要安裝 Azure CLI 並登入。您可以從 此處 安裝 Azure CLI。然後,執行 az login
進行登入。
新增 Azure 角色指派 Cognitive Services OpenAI User
,其範圍設定為您的 Azure OpenAI 資源。這可讓您從 AAD 取得權杖,以搭配 Azure OpenAI 使用。您可以將此角色指派授予使用者、群組、服務主體或受控識別。如需有關 Azure OpenAI RBAC 角色的更多資訊,請參閱 此處。
若要在 Python 中將 AAD 與 LangChain 搭配使用,請安裝 azure-identity
套件。然後,將 OPENAI_API_TYPE
設定為 azure_ad
。接著,使用 DefaultAzureCredential
類別,透過呼叫 get_token
從 AAD 取得權杖,如下所示。最後,將 OPENAI_API_KEY
環境變數設定為權杖值。
import os
from azure.identity import DefaultAzureCredential
# Get the Azure Credential
credential = DefaultAzureCredential()
# Set the API type to `azure_ad`
os.environ["OPENAI_API_TYPE"] = "azure_ad"
# Set the API_KEY to the token from the Azure credential
os.environ["OPENAI_API_KEY"] = credential.get_token("https://cognitiveservices.azure.com/.default").token
DefaultAzureCredential
類別是開始使用 AAD 驗證的簡單方法。您也可以在必要時自訂認證鏈。在以下範例中,我們首先嘗試受控識別,然後回復到 Azure CLI。如果您在 Azure 中執行程式碼,但想要在本地端進行開發,這會很有用。
from azure.identity import ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential
credential = ChainedTokenCredential(
ManagedIdentityCredential(),
AzureCliCredential()
)
部署
透過 Azure OpenAI,您可以設定自己的常見 GPT-3 和 Codex 模型部署。 在呼叫 API 時,您需要指定您想要使用的部署。
注意:這些文件適用於 Azure 文字完成模型。 像 GPT-4 這樣的模型是聊天模型。 它們具有稍微不同的介面,可以透過 AzureChatOpenAI
類別存取。 有關 Azure Chat 的文件,請參閱Azure Chat OpenAI 文件。
假設您的部署名稱為 gpt-35-turbo-instruct-prod
。 在 openai
Python API 中,您可以使用 engine
參數指定此部署。 例如:
import openai
client = openai.AzureOpenAI(
api_version="2023-12-01-preview",
)
response = client.completions.create(
model="gpt-35-turbo-instruct-prod",
prompt="Test prompt"
)
%pip install --upgrade --quiet langchain-openai
import os
os.environ["OPENAI_API_VERSION"] = "2023-12-01-preview"
os.environ["AZURE_OPENAI_ENDPOINT"] = "..."
os.environ["AZURE_OPENAI_API_KEY"] = "..."
# Import Azure OpenAI
from langchain_openai import AzureOpenAI
# Create an instance of Azure OpenAI
# Replace the deployment name with your own
llm = AzureOpenAI(
deployment_name="gpt-35-turbo-instruct-0914",
)
# Run the LLM
llm.invoke("Tell me a joke")
" Why couldn't the bicycle stand up by itself?\n\nBecause it was two-tired!"
我們也可以印出 LLM 並查看其自訂印出內容。
print(llm)
[1mAzureOpenAI[0m
Params: {'deployment_name': 'gpt-35-turbo-instruct-0914', 'model_name': 'gpt-3.5-turbo-instruct', 'temperature': 0.7, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'logit_bias': {}, 'max_tokens': 256}