2023-12-19 15:08:36 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Any,
|
|
|
|
AsyncGenerator,
|
|
|
|
AsyncIterator,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Generator,
|
|
|
|
Iterator,
|
|
|
|
List,
|
|
|
|
Mapping,
|
|
|
|
Optional,
|
|
|
|
Tuple,
|
2024-02-05 21:42:59 +00:00
|
|
|
Type,
|
2023-12-19 15:08:36 +00:00
|
|
|
Union,
|
|
|
|
)
|
|
|
|
|
|
|
|
from langchain_core.callbacks import (
|
|
|
|
AsyncCallbackManagerForLLMRun,
|
|
|
|
CallbackManagerForLLMRun,
|
|
|
|
)
|
|
|
|
from langchain_core.language_models.chat_models import (
|
|
|
|
BaseChatModel,
|
|
|
|
agenerate_from_stream,
|
|
|
|
generate_from_stream,
|
|
|
|
)
|
|
|
|
from langchain_core.language_models.llms import create_base_retry_decorator
|
2024-02-05 21:42:59 +00:00
|
|
|
from langchain_core.messages import AIMessageChunk, BaseMessage, BaseMessageChunk
|
2023-12-19 15:08:36 +00:00
|
|
|
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
2024-01-01 23:20:26 +00:00
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator
|
|
|
|
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
|
2023-12-19 15:08:36 +00:00
|
|
|
|
|
|
|
from langchain_community.adapters.openai import (
|
|
|
|
convert_dict_to_message,
|
|
|
|
convert_message_to_dict,
|
|
|
|
)
|
|
|
|
from langchain_community.chat_models.openai import _convert_delta_to_message_chunk
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from gpt_router.models import ChunkedGenerationResponse, GenerationResponse
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_API_BASE_URL = "https://gpt-router-preview.writesonic.com"
|
|
|
|
|
|
|
|
|
|
|
|
class GPTRouterException(Exception):
|
|
|
|
"""Error with the `GPTRouter APIs`"""
|
|
|
|
|
|
|
|
|
|
|
|
class GPTRouterModel(BaseModel):
|
2024-02-09 20:48:57 +00:00
|
|
|
"""GPTRouter model."""
|
|
|
|
|
2023-12-19 15:08:36 +00:00
|
|
|
name: str
|
|
|
|
provider_name: str
|
|
|
|
|
|
|
|
|
2024-02-05 21:42:59 +00:00
|
|
|
def get_ordered_generation_requests(
|
|
|
|
models_priority_list: List[GPTRouterModel], **kwargs: Any
|
|
|
|
) -> List:
|
2023-12-19 15:08:36 +00:00
|
|
|
"""
|
|
|
|
Return the body for the model router input.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from gpt_router.models import GenerationParams, ModelGenerationRequest
|
|
|
|
|
|
|
|
return [
|
|
|
|
ModelGenerationRequest(
|
|
|
|
model_name=model.name,
|
|
|
|
provider_name=model.provider_name,
|
|
|
|
order=index + 1,
|
|
|
|
prompt_params=GenerationParams(**kwargs),
|
|
|
|
)
|
|
|
|
for index, model in enumerate(models_priority_list)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def _create_retry_decorator(
|
2023-12-19 15:48:52 +00:00
|
|
|
llm: GPTRouter,
|
2023-12-19 15:08:36 +00:00
|
|
|
run_manager: Optional[
|
|
|
|
Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun]
|
|
|
|
] = None,
|
|
|
|
) -> Callable[[Any], Any]:
|
|
|
|
from gpt_router import exceptions
|
|
|
|
|
|
|
|
errors = [
|
|
|
|
exceptions.GPTRouterApiTimeoutError,
|
|
|
|
exceptions.GPTRouterInternalServerError,
|
|
|
|
exceptions.GPTRouterNotAvailableError,
|
|
|
|
exceptions.GPTRouterTooManyRequestsError,
|
|
|
|
]
|
|
|
|
return create_base_retry_decorator(
|
|
|
|
error_types=errors, max_retries=llm.max_retries, run_manager=run_manager
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def completion_with_retry(
|
2023-12-19 15:48:52 +00:00
|
|
|
llm: GPTRouter,
|
2023-12-19 15:08:36 +00:00
|
|
|
models_priority_list: List[GPTRouterModel],
|
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
**kwargs: Any,
|
2024-02-05 21:42:59 +00:00
|
|
|
) -> Union[GenerationResponse, Generator[ChunkedGenerationResponse, None, None]]:
|
2023-12-19 15:08:36 +00:00
|
|
|
"""Use tenacity to retry the completion call."""
|
|
|
|
retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)
|
|
|
|
|
|
|
|
@retry_decorator
|
|
|
|
def _completion_with_retry(**kwargs: Any) -> Any:
|
|
|
|
ordered_generation_requests = get_ordered_generation_requests(
|
|
|
|
models_priority_list, **kwargs
|
|
|
|
)
|
|
|
|
return llm.client.generate(
|
|
|
|
ordered_generation_requests=ordered_generation_requests,
|
|
|
|
is_stream=kwargs.get("stream", False),
|
|
|
|
)
|
|
|
|
|
|
|
|
return _completion_with_retry(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
async def acompletion_with_retry(
|
2023-12-19 15:48:52 +00:00
|
|
|
llm: GPTRouter,
|
2023-12-19 15:08:36 +00:00
|
|
|
models_priority_list: List[GPTRouterModel],
|
|
|
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
|
|
**kwargs: Any,
|
2024-02-05 21:42:59 +00:00
|
|
|
) -> Union[GenerationResponse, AsyncGenerator[ChunkedGenerationResponse, None]]:
|
2023-12-19 15:08:36 +00:00
|
|
|
"""Use tenacity to retry the async completion call."""
|
|
|
|
|
|
|
|
retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)
|
|
|
|
|
|
|
|
@retry_decorator
|
|
|
|
async def _completion_with_retry(**kwargs: Any) -> Any:
|
|
|
|
ordered_generation_requests = get_ordered_generation_requests(
|
|
|
|
models_priority_list, **kwargs
|
|
|
|
)
|
|
|
|
return await llm.client.agenerate(
|
|
|
|
ordered_generation_requests=ordered_generation_requests,
|
|
|
|
is_stream=kwargs.get("stream", False),
|
|
|
|
)
|
|
|
|
|
|
|
|
return await _completion_with_retry(**kwargs)
|
|
|
|
|
|
|
|
|
2023-12-19 15:48:52 +00:00
|
|
|
class GPTRouter(BaseChatModel):
|
2023-12-19 15:08:36 +00:00
|
|
|
"""GPTRouter by Writesonic Inc.
|
|
|
|
|
|
|
|
For more information, see https://gpt-router.writesonic.com/docs
|
|
|
|
"""
|
|
|
|
|
|
|
|
client: Any = Field(default=None, exclude=True) #: :meta private:
|
|
|
|
models_priority_list: List[GPTRouterModel] = Field(min_items=1)
|
|
|
|
gpt_router_api_base: str = Field(default=None)
|
|
|
|
"""WriteSonic GPTRouter custom endpoint"""
|
2024-01-01 23:20:26 +00:00
|
|
|
gpt_router_api_key: Optional[SecretStr] = None
|
2023-12-19 15:08:36 +00:00
|
|
|
"""WriteSonic GPTRouter API Key"""
|
|
|
|
temperature: float = 0.7
|
|
|
|
"""What sampling temperature to use."""
|
|
|
|
model_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
|
|
"""Holds any model parameters valid for `create` call not explicitly specified."""
|
|
|
|
max_retries: int = 4
|
|
|
|
"""Maximum number of retries to make when generating."""
|
|
|
|
streaming: bool = False
|
|
|
|
"""Whether to stream the results or not."""
|
|
|
|
n: int = 1
|
|
|
|
"""Number of chat completions to generate for each prompt."""
|
|
|
|
max_tokens: int = 256
|
|
|
|
|
|
|
|
@root_validator(allow_reuse=True)
|
|
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
|
|
values["gpt_router_api_base"] = get_from_dict_or_env(
|
|
|
|
values,
|
|
|
|
"gpt_router_api_base",
|
|
|
|
"GPT_ROUTER_API_BASE",
|
|
|
|
DEFAULT_API_BASE_URL,
|
|
|
|
)
|
|
|
|
|
2024-01-01 23:20:26 +00:00
|
|
|
values["gpt_router_api_key"] = convert_to_secret_str(
|
|
|
|
get_from_dict_or_env(
|
|
|
|
values,
|
|
|
|
"gpt_router_api_key",
|
|
|
|
"GPT_ROUTER_API_KEY",
|
|
|
|
)
|
2023-12-19 15:08:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
from gpt_router.client import GPTRouterClient
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
raise GPTRouterException(
|
|
|
|
"Could not import GPTRouter python package. "
|
|
|
|
"Please install it with `pip install GPTRouter`."
|
|
|
|
)
|
|
|
|
|
|
|
|
gpt_router_client = GPTRouterClient(
|
2024-01-01 23:20:26 +00:00
|
|
|
values["gpt_router_api_base"],
|
|
|
|
values["gpt_router_api_key"].get_secret_value(),
|
2023-12-19 15:08:36 +00:00
|
|
|
)
|
|
|
|
values["client"] = gpt_router_client
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
@property
|
|
|
|
def lc_secrets(self) -> Dict[str, str]:
|
2024-01-01 23:20:26 +00:00
|
|
|
return {"gpt_router_api_key": "GPT_ROUTER_API_KEY"}
|
2023-12-19 15:08:36 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def lc_serializable(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _llm_type(self) -> str:
|
|
|
|
"""Return type of chat model."""
|
|
|
|
return "gpt-router-chat"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _identifying_params(self) -> Dict[str, Any]:
|
|
|
|
"""Get the identifying parameters."""
|
|
|
|
return {
|
|
|
|
**{"models_priority_list": self.models_priority_list},
|
|
|
|
**self._default_params,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _default_params(self) -> Dict[str, Any]:
|
|
|
|
"""Get the default parameters for calling GPTRouter API."""
|
|
|
|
return {
|
|
|
|
"max_tokens": self.max_tokens,
|
|
|
|
"stream": self.streaming,
|
|
|
|
"n": self.n,
|
|
|
|
"temperature": self.temperature,
|
|
|
|
**self.model_kwargs,
|
|
|
|
}
|
|
|
|
|
|
|
|
def _generate(
|
|
|
|
self,
|
|
|
|
messages: List[BaseMessage],
|
|
|
|
stop: Optional[List[str]] = None,
|
2023-12-28 06:06:42 +00:00
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
stream: Optional[bool] = None,
|
2023-12-19 15:08:36 +00:00
|
|
|
**kwargs: Any,
|
|
|
|
) -> ChatResult:
|
|
|
|
should_stream = stream if stream is not None else self.streaming
|
|
|
|
if should_stream:
|
|
|
|
stream_iter = self._stream(
|
|
|
|
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
|
|
)
|
|
|
|
return generate_from_stream(stream_iter)
|
|
|
|
|
|
|
|
message_dicts, params = self._create_message_dicts(messages, stop)
|
|
|
|
params = {**params, **kwargs, "stream": False}
|
|
|
|
response = completion_with_retry(
|
|
|
|
self,
|
|
|
|
messages=message_dicts,
|
|
|
|
models_priority_list=self.models_priority_list,
|
|
|
|
run_manager=run_manager,
|
|
|
|
**params,
|
|
|
|
)
|
|
|
|
return self._create_chat_result(response)
|
|
|
|
|
|
|
|
async def _agenerate(
|
|
|
|
self,
|
|
|
|
messages: List[BaseMessage],
|
|
|
|
stop: Optional[List[str]] = None,
|
2023-12-28 06:06:42 +00:00
|
|
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
|
|
stream: Optional[bool] = None,
|
2023-12-19 15:08:36 +00:00
|
|
|
**kwargs: Any,
|
|
|
|
) -> ChatResult:
|
|
|
|
should_stream = stream if stream is not None else self.streaming
|
|
|
|
if should_stream:
|
|
|
|
stream_iter = self._astream(
|
|
|
|
messages, stop=stop, run_manager=run_manager, **kwargs
|
|
|
|
)
|
|
|
|
return await agenerate_from_stream(stream_iter)
|
|
|
|
|
|
|
|
message_dicts, params = self._create_message_dicts(messages, stop)
|
|
|
|
params = {**params, **kwargs, "stream": False}
|
|
|
|
response = await acompletion_with_retry(
|
|
|
|
self,
|
|
|
|
messages=message_dicts,
|
|
|
|
models_priority_list=self.models_priority_list,
|
|
|
|
run_manager=run_manager,
|
|
|
|
**params,
|
|
|
|
)
|
|
|
|
return self._create_chat_result(response)
|
|
|
|
|
2024-02-05 21:42:59 +00:00
|
|
|
def _create_chat_generation_chunk(
|
|
|
|
self, data: Mapping[str, Any], default_chunk_class: Type[BaseMessageChunk]
|
|
|
|
) -> Tuple[ChatGenerationChunk, Type[BaseMessageChunk]]:
|
2023-12-19 15:08:36 +00:00
|
|
|
chunk = _convert_delta_to_message_chunk(
|
|
|
|
{"content": data.get("text", "")}, default_chunk_class
|
|
|
|
)
|
|
|
|
finish_reason = data.get("finish_reason")
|
|
|
|
generation_info = (
|
|
|
|
dict(finish_reason=finish_reason) if finish_reason is not None else None
|
|
|
|
)
|
|
|
|
default_chunk_class = chunk.__class__
|
2024-02-05 21:42:59 +00:00
|
|
|
gen_chunk = ChatGenerationChunk(message=chunk, generation_info=generation_info)
|
|
|
|
return gen_chunk, default_chunk_class
|
2023-12-19 15:08:36 +00:00
|
|
|
|
|
|
|
def _stream(
|
|
|
|
self,
|
|
|
|
messages: List[BaseMessage],
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> Iterator[ChatGenerationChunk]:
|
|
|
|
message_dicts, params = self._create_message_dicts(messages, stop)
|
|
|
|
params = {**params, **kwargs, "stream": True}
|
|
|
|
|
2024-02-05 21:42:59 +00:00
|
|
|
default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
|
2023-12-19 15:08:36 +00:00
|
|
|
generator_response = completion_with_retry(
|
|
|
|
self,
|
|
|
|
messages=message_dicts,
|
|
|
|
models_priority_list=self.models_priority_list,
|
|
|
|
run_manager=run_manager,
|
|
|
|
**params,
|
|
|
|
)
|
|
|
|
for chunk in generator_response:
|
|
|
|
if chunk.event != "update":
|
|
|
|
continue
|
|
|
|
|
|
|
|
chunk, default_chunk_class = self._create_chat_generation_chunk(
|
|
|
|
chunk.data, default_chunk_class
|
|
|
|
)
|
|
|
|
|
|
|
|
if run_manager:
|
|
|
|
run_manager.on_llm_new_token(
|
|
|
|
token=chunk.message.content, chunk=chunk.message
|
|
|
|
)
|
|
|
|
|
2024-02-23 00:15:21 +00:00
|
|
|
yield chunk
|
|
|
|
|
2023-12-19 15:08:36 +00:00
|
|
|
async def _astream(
|
|
|
|
self,
|
|
|
|
messages: List[BaseMessage],
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
|
|
|
**kwargs: Any,
|
|
|
|
) -> AsyncIterator[ChatGenerationChunk]:
|
|
|
|
message_dicts, params = self._create_message_dicts(messages, stop)
|
|
|
|
params = {**params, **kwargs, "stream": True}
|
|
|
|
|
2024-02-05 21:42:59 +00:00
|
|
|
default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
|
2023-12-19 15:08:36 +00:00
|
|
|
generator_response = acompletion_with_retry(
|
|
|
|
self,
|
|
|
|
messages=message_dicts,
|
|
|
|
models_priority_list=self.models_priority_list,
|
|
|
|
run_manager=run_manager,
|
|
|
|
**params,
|
|
|
|
)
|
|
|
|
async for chunk in await generator_response:
|
|
|
|
if chunk.event != "update":
|
|
|
|
continue
|
|
|
|
|
|
|
|
chunk, default_chunk_class = self._create_chat_generation_chunk(
|
|
|
|
chunk.data, default_chunk_class
|
|
|
|
)
|
|
|
|
|
|
|
|
if run_manager:
|
|
|
|
await run_manager.on_llm_new_token(
|
|
|
|
token=chunk.message.content, chunk=chunk.message
|
|
|
|
)
|
|
|
|
|
2024-02-23 00:15:21 +00:00
|
|
|
yield chunk
|
|
|
|
|
2023-12-19 15:08:36 +00:00
|
|
|
def _create_message_dicts(
|
|
|
|
self, messages: List[BaseMessage], stop: Optional[List[str]]
|
|
|
|
) -> Tuple[List[Dict[str, Any]], Dict[str, Any]]:
|
|
|
|
params = self._default_params
|
|
|
|
if stop is not None:
|
|
|
|
if "stop" in params:
|
|
|
|
raise ValueError("`stop` found in both the input and default params.")
|
|
|
|
params["stop"] = stop
|
|
|
|
message_dicts = [convert_message_to_dict(m) for m in messages]
|
|
|
|
return message_dicts, params
|
|
|
|
|
|
|
|
def _create_chat_result(self, response: GenerationResponse) -> ChatResult:
|
|
|
|
generations = []
|
|
|
|
for res in response.choices:
|
|
|
|
message = convert_dict_to_message(
|
|
|
|
{
|
|
|
|
"role": "assistant",
|
|
|
|
"content": res.text,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
gen = ChatGeneration(
|
|
|
|
message=message,
|
|
|
|
generation_info=dict(finish_reason=res.finish_reason),
|
|
|
|
)
|
|
|
|
generations.append(gen)
|
|
|
|
llm_output = {"token_usage": response.meta, "model": response.model}
|
|
|
|
return ChatResult(generations=generations, llm_output=llm_output)
|