-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core: Azure OpenAI support for ChatGPT and GPT-4 chat models #195
core: Azure OpenAI support for ChatGPT and GPT-4 chat models #195
Conversation
a68dbd6
to
08a5233
Compare
@pieroit this PR LGTM and is ready to merge. It would be nice to have someone else testing. Thanks |
I have tested the PR with multiples prompts (simples and complex) and all works fine! @pieroit for me you can merge. |
core/cat/factory/llm.py
Outdated
openai_api_base: str | ||
api_type: str = "azure" | ||
# https://learn.microsoft.com/en-gb/azure/cognitive-services/openai/reference#chat-completions | ||
api_version: str = "2023-03-15-preview" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pieroit I found a bug. This is supposed to be:
openai_api_version: str = "2023-03-15-preview"
I think I did not catch it earlier because I had an env variable set.
However when I fix the name then I have the problem that the requests have api-version=2022-12-01
and they result in HTTP 404
cheshire_cat_core | [2023-05-15 11:58:45,916] DEBUG util.py 60 -=> message='Request to OpenAI API' method=post path=https://xxxxxxx.openai.azure.com//openai/deployments/gpt-35-turbo/chat/completions?api-version=2022-12-01
Do you have any idea why ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cristianorevil did you have environment variables set for api version when you tested ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. My .env always is:
Decide host and port for your Cat. Default will be localhost:1865
CORE_HOST=localhost
CORE_PORT=1865
Decide host and port for your Cat Admin. Default will be localhost:3000
ADMIN_HOST=localhost
ADMIN_PORT=3000
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not the environment variables. The cat is mixing configurations between the completion model and the chat model. In particular it seems to be using the api_version
from the completion model all the times.
For this reason if you never configured in the database the completion model, the chat model will not work.
I need @pieroit to help me to debug this because it seems that the function that loads the llm is involved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pieroit I need your help here:
for some reason it works only if in the DB there is both api_version
and openai_api_version`. Here is a dump of a working db:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE setting (
setting_id CHAR(32) NOT NULL,
value JSON,
"createdAt" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
"updatedAt" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
name VARCHAR NOT NULL,
category VARCHAR NOT NULL,
enabled BOOLEAN NOT NULL,
PRIMARY KEY (setting_id)
);
INSERT INTO setting VALUES('13c2c4bd72864e74bd13f9ee124ce3ef','{"model_name": "gpt-35-turbo", "api_type": "azure", "openai_api_version": "2023-03-15-preview", "api_version": "2023-03-15-preview", "openai_api_key": "<redacted>", "openai_api_base": "https://xxxxxxxxxxxxxx.openai.azure.com/", "deployment_name": "gpt-35-turbo"}','2023-05-15 14:14:27','2023-05-15 14:44:12','LLMAzureChatOpenAIConfig','llm_factory',1);
INSERT INTO setting VALUES('7a1dd487913b49d58c50b02fc71a0e53','{}','2023-05-15 14:16:14','2023-05-15 14:16:14','LLMDefaultConfig','llm_factory',1);
INSERT INTO setting VALUES('529e8710dc894ad78daecd874e7132a3','{"api_type": "azure", "api_version": "2023-03-15-preview", "deployment_name": "gpt-35-turbo", "model_name": "gpt-35-turbo", "openai_api_base": "https://xxxxxxxxxxxxxx.openai.azure.com/", "openai_api_key": "<redacted>"}','2023-05-15 14:16:55','2023-05-15 14:44:08','LLMAzureOpenAIConfig','llm_factory',1);
INSERT INTO setting VALUES('f1d72450772e4674bcaa4ebc453554d3','{"name": "LLMAzureChatOpenAIConfig"}','2023-05-15 14:44:12','2023-05-15 14:44:12','llm_selected','llm',1);
COMMIT;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I confirm that importing this DB works:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE setting (
setting_id CHAR(32) NOT NULL,
value JSON,
"createdAt" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
"updatedAt" TIMESTAMP DEFAULT (CURRENT_TIMESTAMP) NOT NULL,
name VARCHAR NOT NULL,
category VARCHAR NOT NULL,
enabled BOOLEAN NOT NULL,
PRIMARY KEY (setting_id)
);
INSERT INTO setting VALUES('13c2c4bd72864e74bd13f9ee124ce3ef','{"model_name": "gpt-35-turbo", "api_type": "azure", "openai_api_version": "2023-03-15-preview", "api_version": "2023-03-15-preview", "openai_api_key": "<redacted>", "openai_api_base": "https://xxxxxxxx.openai.azure.com/", "deployment_name": "gpt-35-turbo"}','2023-05-15 14:14:27','2023-05-15 14:44:12','LLMAzureChatOpenAIConfig','llm_factory',1);
INSERT INTO setting VALUES('f1d72450772e4674bcaa4ebc453554d3','{"name": "LLMAzureChatOpenAIConfig"}','2023-05-15 14:44:12','2023-05-15 14:44:12','llm_selected','llm',1);
COMMIT;
I create the file like this:
sqlite3 core/metadata-v3.db < db.sql
For some reasons when the db both values are present:
"openai_api_version": "2023-03-15-preview", "api_version": "2023-03-15-preview"
everything works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cristianorevil could you check in your sqlite db if you also have both "openai_api_version": "2023-03-15-preview", "api_version": "2023-03-15-preview"
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes i have both
@zioproto should I wait to merge? |
@pieroit wait. |
08a5233
to
2eb7829
Compare
@zioproto for Openai there are two different classes in langhcain, langchain.llms.OpenAI and langchain.llms.OpenAIChat (respectively for completion and chat). They are used respectively by two cat factory classes, It should be the same with Azure I guess |
It is not the same, there is |
Maybe we should wait for this LangChain PR before moving forward ? |
To use with an Azure OpenAI Service resource with either the gpt-35-turbo (preview), or the gpt-4 (preview)1 models deployed * https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=programming-language-python&tabs=command-line * https://learn.microsoft.com/en-gb/azure/cognitive-services/openai/reference#chat-completions
2eb7829
to
6f88f33
Compare
I need some help debugging this: This should be what happens in the cat: In the db of the cat we save a config like:
If I inspect the LLMChain object just before the call
But the call fails because the API version is not used, I have no idea where it reads 2022-12-01
I discovered by accident that if in the DB I adde the extra value
then this api_version is propagated to the mode_kwargs, that now are empty: model_kwargs{'api_version': '2023-03-15-preview'} then everything works and the value is used to build the URL. This makes no sense to me looking at: |
What is the difference between |
I fixed the problem with commit f74d7b3 it is the same problem described here: |
@cristianorevil could you test this latest version please ? But you need to delete your sqlite3 database before testing, thank you |
This is why it was hard to debug:
The output of this code is:
This means that I will open a bug to LangChain |
@zioproto i have tested (deleting first sqlite db) and NOT work. I have configured only 1 model (Azure Chat) and saved. After that an error occurs in log: "Did not find openai_api_version, please add an environment variable |
This is how I test:
to configure the cat, either from the admin UI, or to be reproducible:
Can you confirm these steps dont work for you ? Are you doing anything different from this ? |
i don't have cloned repo in new folder, i have changed the branch to PR, checkout, deleted database and after docker-compose build --no-cache. The cat was configured by ui admin. I will try in complete new folder |
Yes please try a new folder. The Docker containers mount the source code form your local folder, where you have probably a lot of cached
|
@zioproto ok in a new folder all works fine! |
@pieroit LGTM, please merge |
To use with an Azure OpenAI Service resource with either the gpt-35-turbo (preview), or the gpt-4 (preview)1 models deployed
Fixes #211