Azure OpenAI

Azure OpenAI

Colab에서 이 노트북을 여는 경우라면 LlamaIndex 🦙 설치가 필요할 거예요.

%pip install llama-index-llms-azure-openai
!pip install llama-index

출처: 문서

본문

사전 준비사항 (Prerequisites)

  1. Azure 구독을 설정하세요 - 여기에서 무료로 만들 수 있어요.
  2. Azure OpenAI 서비스 접근을 신청하세요 여기.
  3. Azure 포털에서 리소스를 만드세요 여기.
  4. Azure OpenAI Studio에서 모델을 배포하세요 여기.

자세한 내용은 이 가이드에서 찾을 수 있어요.

**"model name"**과 **"deployment name"**을 적어 두세요. LLM에 연결할 때 필요해요.

환경 설정 (Environment Setup)

설정 정보 찾기 - API base, API key, deployment name (즉 engine) 등

필요한 설정 정보를 찾으려면 다음을 수행하세요.

  1. Azure OpenAI Studio로 이동하세요 여기.
  2. chat 또는 completions playground로 이동하세요 (설정하는 LLM에 따라).
  3. "view code"를 클릭하세요 (아래 이미지 참고).
from IPython.display import Image


Image(filename="./azure_playground.png")

png

  1. api_type, api_base, api_version, engine(앞서 적어둔 "deployment name"과 같아야 해요), key를 적어 두세요.
from IPython.display import Image


Image(filename="./azure_env.png")

png

환경 변수 구성

Azure 배포의 OpenAI 모델 사용은 일반 OpenAI와 매우 유사해요. 환경 변수 몇 개만 더 구성하면 돼요.

  • OPENAI_API_VERSION: 2023-07-01-preview로 설정하세요. 향후 바뀔 수 있어요.
  • AZURE_OPENAI_ENDPOINT: 엔드포인트는 https://YOUR_RESOURCE_NAME.openai.azure.com/처럼 생겼어요.
  • AZURE_OPENAI_API_KEY: API 키예요.
import os


os.environ["AZURE_OPENAI_API_KEY"] = "<your-api-key>"
os.environ[
    "AZURE_OPENAI_ENDPOINT"
] = "https://<your-resource-name>.openai.azure.com/"
os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"

LLM 사용하기

from llama_index.llms.azure_openai import AzureOpenAI

일반 OpenAI와 달리, model 외에 engine 인자를 전달해야 해요. engine은 Azure OpenAI Studio에서 선택한 모델 배포의 이름이에요. 자세한 내용은 앞선 "설정 정보 찾기" 섹션을 참고하세요.

llm = AzureOpenAI(
    engine="simon-llm", model="gpt-35-turbo-16k", temperature=0.0
)

대안으로 환경 변수를 설정하지 않고 생성자에 파라미터를 직접 전달할 수도 있어요.

llm = AzureOpenAI(
    engine="my-custom-llm",
    model="gpt-35-turbo-16k",
    temperature=0.0,
    azure_endpoint="https://<your-resource-name>.openai.azure.com/",
    api_key="<your-api-key>",
    api_version="2023-07-01-preview",
)

텍스트 완성에는 complete 엔드포인트를 사용하세요.

response = llm.complete("The sky is a beautiful blue and")
print(response)
the sun is shining brightly. Fluffy white clouds float lazily across the sky, creating a picturesque scene. The vibrant blue color of the sky brings a sense of calm and tranquility. It is a perfect day to be outside, enjoying the warmth of the sun and the gentle breeze. The sky seems to stretch endlessly, reminding us of the vastness and beauty of the world around us. It is a reminder to appreciate the simple pleasures in life and to take a moment to admire the natural wonders that surround us.
response = llm.stream_complete("The sky is a beautiful blue and")
for r in response:
    print(r.delta, end="")
the sun is shining brightly. Fluffy white clouds float lazily across the sky, creating a picturesque scene. The vibrant blue color of the sky brings a sense of calm and tranquility. It is a perfect day to be outside, enjoying the warmth of the sun and the gentle breeze. The sky seems to stretch endlessly, reminding us of the vastness and beauty of the world around us. It is a reminder to appreciate the simple pleasures in life and to take a moment to pause and admire the natural wonders that surround us.

대화에는 chat 엔드포인트를 사용하세요.

from llama_index.core.llms import ChatMessage


messages = [
    ChatMessage(
        role="system", content="You are a pirate with colorful personality."
    ),
    ChatMessage(role="user", content="Hello"),
]


response = llm.chat(messages)
print(response)
assistant: Ahoy there, matey! How be ye on this fine day? I be Captain Jolly Roger, the most colorful pirate ye ever did lay eyes on! What brings ye to me ship?
response = llm.stream_chat(messages)
for r in response:
    print(r.delta, end="")
Ahoy there, matey! How be ye on this fine day? I be Captain Jolly Roger, the most colorful pirate ye ever did lay eyes on! What brings ye to me ship?

동일한 파라미터를 각 chat이나 completion 호출에 추가하는 대신 additional_kwargs로 인스턴스 단위에서 설정할 수 있어요.

llm = AzureOpenAI(
    engine="simon-llm",
    model="gpt-35-turbo-16k",
    temperature=0.0,
    additional_kwargs={"user": "your_user_id"},
)

더 알아보기 (Learn more)