LoginSignup
2
1

AzureOpenAIのAPIを使ったEmbeddingモデルの取得方法

Last updated at Posted at 2024-03-31

はじめに

Pythonを使ってAzureOpenAIのエンドポイントを使う場合、openai1.x以降ではOpenAIのエンドポイントを使う場合と書き方が異なる模様。

参考:https://learn.microsoft.com/ja-jp/azure/ai-services/openai/how-to/switching-endpoints

Embeddingモデルを使う場合も書き方が異なってくるため、LangChainLlamaIndexのそれぞれでどのような方法になるかをまとめる。

いずれも関数として定義し、RAGなどで必要なEmbeddingモデルとして利用できるような形で示す。

環境

openai: 1.14.11.x以降

方法

以下、それぞれの定義はこちら。

LangChainの場合

from langchain_openai import AzureOpenAIEmbedding

def get_embedding():
    embedding = AzureOpenAIEmbedding(
        openai_api_type='azure',
        openai_api_version='{API_VERSION}',
        azure_endpoint='{API_ENDPOINT}',
        openai_api_key='{API_KEYS}',
        model='{EMBEDDING_MODEL}',
        azure_deployment='{MODEL_NAME}'
    )
    return embedding

LlamaIndexの場合

from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding

def get_embedding():
    embedding = AzureOpenAIEmbedding(
        model='{EMBEDDING_MODEL}',
        deployment_name='{EMBEDDING_DEPLOYMENT_MODEL_NAME}',
        api_key='{API_KEY}',
        azure_endpoint='{API_ENDPOINT}',
        api_version='{API_VERSION}'
    )
    return embedding

関連記事

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1