현재 Azure OpenAI text completion 모델 사용에 관한 페이지를 보고 계십니다. 최신이자 가장 인기 있는 Azure OpenAI 모델은 chat completion 모델입니다.gpt-3.5-turbo-instruct를 특별히 사용하는 경우가 아니라면, 이 페이지를 찾고 계실 것입니다.
이 페이지는 Azure OpenAI와 함께 LangChain을 사용하는 방법을 다룹니다. Azure OpenAI API는 OpenAI의 API와 호환됩니다. openai Python 패키지를 사용하면 OpenAI와 Azure OpenAI를 모두 쉽게 사용할 수 있습니다. 아래에 명시된 예외 사항을 제외하고는 OpenAI를 호출하는 것과 동일한 방식으로 Azure OpenAI를 호출할 수 있습니다.

API 구성

환경 변수를 사용하여 Azure OpenAI를 사용하도록 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 Key
  • Azure Active Directory (AAD)
API key를 사용하는 것이 시작하기에 가장 쉬운 방법입니다. Azure portal의 Azure OpenAI 리소스에서 API key를 찾을 수 있습니다. 그러나 복잡한 보안 요구 사항이 있는 경우 Azure Active Directory를 사용할 수 있습니다. Azure OpenAI에서 AAD를 사용하는 방법에 대한 자세한 내용은 여기에서 확인할 수 있습니다. 로컬에서 개발하는 경우 Azure CLI를 설치하고 로그인해야 합니다. Azure CLI는 여기에서 설치할 수 있습니다. 그런 다음 az login을 실행하여 로그인합니다. Azure OpenAI 리소스로 범위가 지정된 Azure 역할 할당 Cognitive Services OpenAI User를 추가합니다. 이를 통해 Azure OpenAI와 함께 사용할 AAD 토큰을 얻을 수 있습니다. 이 역할 할당은 사용자, 그룹, 서비스 주체 또는 관리 ID에 부여할 수 있습니다. Azure OpenAI RBAC 역할에 대한 자세한 내용은 여기를 참조하세요. LangChain과 함께 Python에서 AAD를 사용하려면 azure-identity 패키지를 설치합니다. 그런 다음 OPENAI_API_TYPEazure_ad로 설정합니다. 다음으로 아래와 같이 get_token을 호출하여 DefaultAzureCredential 클래스를 사용해 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 인증을 시작하기 쉬운 방법입니다. 필요한 경우 credential chain을 사용자 정의할 수도 있습니다. 아래 예제에서는 먼저 Managed Identity를 시도한 다음 Azure CLI로 대체합니다. 이는 Azure에서 코드를 실행하지만 로컬에서 개발하려는 경우에 유용합니다.
from azure.identity import ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential

credential = ChainedTokenCredential(
    ManagedIdentityCredential(),
    AzureCliCredential()
)

Deployments

Azure OpenAI를 사용하면 일반적인 GPT-3 및 Codex 모델의 자체 deployment를 설정합니다. API를 호출할 때 사용하려는 deployment를 지정해야 합니다. 참고: 이 문서는 Azure text completion 모델에 대한 것입니다. GPT-4와 같은 모델은 chat 모델입니다. 약간 다른 인터페이스를 가지고 있으며 AzureChatOpenAI 클래스를 통해 액세스할 수 있습니다. Azure chat에 대한 문서는 Azure Chat OpenAI 문서를 참조하세요. deployment 이름이 gpt-35-turbo-instruct-prod라고 가정해 봅시다. openai Python API에서 engine 매개변수를 사용하여 이 deployment를 지정할 수 있습니다. 예를 들어:
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 -qU  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)
AzureOpenAI
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}

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I