For various reasons, we decided to use Azure OpenAI Service instead of OpenAIās API for Talk to the City.
Talk to the City Scatter hard-codes LangChainās ChatOpenAI extraction.py
llm = ChatOpenAI(model_name=model, temperature=0.0)
So to use it in Azure OpenAI, we need to rewrite here
The original code imports from langchain.chat_models import ChatOpenAI
.
I think it would work easily if you think about it from langchain.chat_models import AzureChatOpenAI
, donāt you?
- The expectation is that LangChain is tucked in as a middle layer to abstract the lower layers.
- But somehow it didnāt work.
- We havenāt identified the cause, so itās possible this isnāt the problem here.
I finally got it to work
from langchain_openai import AzureChatOpenAI
.
- We havenāt identified the cause, so itās possible this isnāt the problem here.
I finally got it to work
Hereās what the modifications look like py
# llm = ChatOpenAI(model_name=model, temperature=0.0)
llm = AzureChatOpenAI(
model_name=model,
temperature=0.0,
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
openai_api_key=os.getenv("AZURE_OPENAI_API_KEY"),
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION"))
Iām reading .env
in dotenv for more parameters.
py
import dotenv
import os
dotenv.load_dotenv()
Information such as AZURE_OPENAI_ENDPOINT
to be written in .env
can be found by creating a āresourceā and deploying a āmodelā.
I thought AZURE_OPENAI_API_VERSION was necessary, but it is loaded in the query parameter of AZURE_OPENAI_ENDPOINT, so it might be OK to omit it.
Also, I thought this would be enough, but Embedding is a separate model, so I need to work on that as well.
from langchain_openai import AzureOpenAIEmbeddings
py
embeds = AzureOpenAIEmbeddings(
model="text-embedding-3-large",
openai_api_key=os.getenv("AZURE_OPENAI_API_KEY"),
openai_api_version=os.getenv("AZURE_OPENAI_API_VERSION")
).embed_documents(args)
Why is there no AZURE_OPENAI_ENDPOINT over here?
- Oh, youāre reading
AZURE_EMBEDDING_ENDPOINT
directly from the environment variable.- In
.env
it says something likeAZURE_EMBEDDING_ENDPOINT=https://<resource>.openai.azure.com/openai/deployments/text-embedding-3-large/embeddings?api- version=2023-05-15
or something like that.
- In
This page is auto-translated from [/nishio/TTTCćAzure OpenAIć§ä½æć](https://scrapbox.io/nishio/TTTCćAzure OpenAIć§ä½æć) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iām very happy to spread my thought to non-Japanese readers.