mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-25 17:08:46 +00:00
Add OpenAI API v1 support for ChatAnyscale and fixed a bug with openai_api_key (#13237)
1. Add OpenAI API v1 support 2. Fixed a bug to call `get_secret_value` on a str value (values["openai_api_key"])
This commit is contained in:
parent
545b76b0fd
commit
cc55d2fcee
@ -16,6 +16,7 @@ from langchain.chat_models.openai import (
|
||||
from langchain.pydantic_v1 import Field, SecretStr, root_validator
|
||||
from langchain.schema.messages import BaseMessage
|
||||
from langchain.utils import convert_to_secret_str, get_from_dict_or_env
|
||||
from langchain.utils.openai import is_openai_v1
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import tiktoken
|
||||
@ -133,7 +134,21 @@ class ChatAnyscale(ChatOpenAI):
|
||||
"Please install it with `pip install openai`.",
|
||||
) from e
|
||||
try:
|
||||
values["client"] = openai.ChatCompletion
|
||||
if is_openai_v1():
|
||||
client_params = {
|
||||
"api_key": values["openai_api_key"],
|
||||
"base_url": values["openai_api_base"],
|
||||
# To do: future support
|
||||
# "organization": values["openai_organization"],
|
||||
# "timeout": values["request_timeout"],
|
||||
# "max_retries": values["max_retries"],
|
||||
# "default_headers": values["default_headers"],
|
||||
# "default_query": values["default_query"],
|
||||
# "http_client": values["http_client"],
|
||||
}
|
||||
values["client"] = openai.OpenAI(**client_params).chat.completions
|
||||
else:
|
||||
values["client"] = openai.ChatCompletion
|
||||
except AttributeError as exc:
|
||||
raise ValueError(
|
||||
"`openai` has no `ChatCompletion` attribute, this is likely "
|
||||
@ -147,7 +162,7 @@ class ChatAnyscale(ChatOpenAI):
|
||||
model_name = values["model_name"]
|
||||
|
||||
available_models = cls.get_available_models(
|
||||
values["openai_api_key"].get_secret_value(),
|
||||
values["openai_api_key"],
|
||||
values["openai_api_base"],
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user