7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的に毎回悩むので、LangChainでOpenAI系モデルを使用する方法まとめ

Posted at

2024/4/2時点の情報

pip install langchain=="0.1.14" langchain-openai=="0.1.1"

ChatOpenAI

  • パラメーターで渡す場合

    from langchain_openai import ChatOpenAI
    
    openai_api_key = "sk-*****"
    
    chat = ChatOpenAI(openai_api_key=openai_api_key)
    
    chat.invoke("hello")
    
  • 環境変数を使用する場合

    import os
    os.environ["OPENAI_API_KEY"] = "sk-*****"
    
    from langchain_openai import ChatOpenAI
    
    chat = ChatOpenAI()
    
    chat.invoke("hello")
    

OpenAIEmbeddings

  • パラメーターで渡す場合

    from langchain_openai import OpenAIEmbeddings
    
    openai_api_key = "sk-*****"
    
    embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
    
    embeddings.embed_documents(["Hello"])
    embeddings.embed_query("Hello")
    
  • 環境変数を使用する場合

    import os
    os.environ["OPENAI_API_KEY"] = "sk-*****"
    
    from langchain_openai import OpenAIEmbeddings
    
    embeddings = OpenAIEmbeddings()
    
    embeddings.embed_documents(["Hello"])
    embeddings.embed_query("Hello")
    

AzureChatOpenAI

  • パラメーターで渡す場合

    from langchain_openai import AzureChatOpenAI
    
    api_key = "*****"
    api_version = "2023-05-15"
    azure_endpoint = "https://*****.openai.azure.com"
    azure_deployment = "*****"
    
    chat = AzureChatOpenAI(
        api_key=api_key,
        api_version=api_version,
        azure_endpoint=azure_endpoint,
        azure_deployment=azure_deployment,
    )
    
    chat.invoke("hello")
    
  • 環境変数を使用する場合(azure_deploymentは環境変数で指定できない?)

    import os
    os.environ["AZURE_OPENAI_API_KEY"] = "*****"
    os.environ["OPENAI_API_VERSION"] = "2023-05-15"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://*****.openai.azure.com"
    
    from langchain_openai import AzureChatOpenAI
    
    azure_deployment = "*****"
    
    chat = AzureChatOpenAI(azure_deployment=azure_deployment)
    
    chat.invoke("hello")
    

AzureOpenAIEmbeddings

  • パラメーターで渡す場合

    from langchain_openai import AzureOpenAIEmbeddings
    
    api_key = "*****"
    azure_endpoint = "https://*****.openai.azure.com"
    azure_deployment = "*****"
    
    embeddings = AzureOpenAIEmbeddings(
        api_key=api_key, azure_endpoint=azure_endpoint, azure_deployment=azure_deployment
    )
    
    embeddings.embed_documents(["Hello"])
    embeddings.embed_query("Hello")
    
  • 環境変数を使用する場合(azure_deploymentは環境変数で指定できない?)

    import os
    
    os.environ["AZURE_OPENAI_API_KEY"] = "*****"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://*****.openai.azure.com"
    
    from langchain_openai import AzureOpenAIEmbeddings
    
    azure_deployment = "*****"
    
    embeddings = AzureOpenAIEmbeddings(azure_deployment=azure_deployment)
    
    embeddings.embed_documents(["Hello"])
    embeddings.embed_query("Hello")
    

公式ドキュメント

7
3
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
7
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?