Tony20100/langchain_full huggingface dataset:
Source code for langchain_community.chat_models.moonshot
"""Wrapper around Moonshot chat models."""
from typing import Dict
from langchain_core.pydantic_v1 import root_validator
from langchain_core.utils import get_from_dict_or_env
from langchain_community.chat_models import ChatOpenAI
from langchain_community.llms.moonshot import MOONSHOT_SERVICE_URL_BASE, MoonshotCommon
[docs]class MoonshotChat(MoonshotCommon, ChatOpenAI):  # type: ignore[misc]
    """Moonshot large language models.
    To use, you should have the ``openai`` python package installed, and the
    environment variable ``MOONSHOT_API_KEY`` set with your API key.
    (Moonshot's chat API is compatible with OpenAI's SDK.)
    Referenced from https://platform.moonshot.cn/docs
    Example:
        .. code-block:: python
            from langchain_community.chat_models.moonshot import MoonshotChat
            moonshot = MoonshotChat(model="moonshot-v1-8k")
    """
    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that the environment is set up correctly."""
        values["moonshot_api_key"] = get_from_dict_or_env(
            values, "moonshot_api_key", "MOONSHOT_API_KEY"
        )
        try:
            import openai
        except ImportError:
            raise ImportError(
                "Could not import openai python package. "
                "Please install it with `pip install openai`."
            )
        client_params = {
            "api_key": values["moonshot_api_key"].get_secret_value(),
            "base_url": values["base_url"]
            if "base_url" in values
            else MOONSHOT_SERVICE_URL_BASE,
        }
Source code for langchain_community.chat_models.gpt_router
from __future__ import annotations
import logging
from typing import (
    TYPE_CHECKING,
    Any,
    AsyncGenerator,
    AsyncIterator,
    Callable,
    Dict,
    Generator,
    Iterator,
    List,
    Mapping,
    Optional,
    Tuple,
    Type,
    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
from langchain_core.messages import AIMessageChunk, BaseMessage, BaseMessageChunk
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
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
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"
[docs]class GPTRouterException(Exception):
    """Error with the `GPTRouter APIs`"""
[docs]class GPTRouterModel(BaseModel):
    """GPTRouter model."""
    name: str
    provider_name: str
[docs]def get_ordered_generation_requests(
provider_name: str
[docs]def get_ordered_generation_requests(
    models_priority_list: List[GPTRouterModel], **kwargs: Any
) -> List:
    """
    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(
    llm: GPTRouter,
    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
    )
[docs]def completion_with_retry(
    llm: GPTRouter,
    models_priority_list: List[GPTRouterModel],
    run_manager: Optional[CallbackManagerForLLMRun] = None,
    **kwargs: Any,
) -> Union[GenerationResponse, Generator[ChunkedGenerationResponse, None, None]]:
    """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:
@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)
[docs]async def acompletion_with_retry(
    llm: GPTRouter,
    models_priority_list: List[GPTRouterModel],
    run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
    **kwargs: Any,
) -> Union[GenerationResponse, AsyncGenerator[ChunkedGenerationResponse, None]]:
    """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)
[docs]class GPTRouter(BaseChatModel):
    """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"""
"""WriteSonic GPTRouter custom endpoint"""
    gpt_router_api_key: Optional[SecretStr] = None
    """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,
        )
        values["gpt_router_api_key"] = convert_to_secret_str(
            get_from_dict_or_env(
                values,
                "gpt_router_api_key",
                "GPT_ROUTER_API_KEY",
            )
        )
        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(
            values["gpt_router_api_base"],
            values["gpt_router_api_key"].get_secret_value(),
        )
values["gpt_router_api_key"].get_secret_value(),
        )
        values["client"] = gpt_router_client
        return values
    @property
    def lc_secrets(self) -> Dict[str, str]:
        return {"gpt_router_api_key": "GPT_ROUTER_API_KEY"}
    @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,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        stream: Optional[bool] = None,
        **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)
)
            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,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        stream: Optional[bool] = None,
        **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)
    def _create_chat_generation_chunk(
        self, data: Mapping[str, Any], default_chunk_class: Type[BaseMessageChunk]
    ) -> Tuple[ChatGenerationChunk, Type[BaseMessageChunk]]:
        chunk = _convert_delta_to_message_chunk(
            {"content": data.get("text", "")}, default_chunk_class
        )
{"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__
        gen_chunk = ChatGenerationChunk(message=chunk, generation_info=generation_info)
        return gen_chunk, default_chunk_class
    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}
        default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
        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
                )
            yield chunk
    async def _astream(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> AsyncIterator[ChatGenerationChunk]:
**kwargs: Any,
    ) -> AsyncIterator[ChatGenerationChunk]:
        message_dicts, params = self._create_message_dicts(messages, stop)
        params = {**params, **kwargs, "stream": True}
        default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
        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
                )
            yield chunk
    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,
Source code for langchain_community.chat_models.baichuan
import json
import logging
from typing import Any, Dict, Iterator, List, Mapping, Optional, Type
import requests
from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_core.language_models.chat_models import (
    BaseChatModel,
    generate_from_stream,
)
from langchain_core.messages import (
    AIMessage,
    AIMessageChunk,
    BaseMessage,
    BaseMessageChunk,
    ChatMessage,
    ChatMessageChunk,
    HumanMessage,
    HumanMessageChunk,
)
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
from langchain_core.pydantic_v1 import Field, SecretStr, root_validator
from langchain_core.utils import (
    convert_to_secret_str,
    get_from_dict_or_env,
    get_pydantic_field_names,
)
logger = logging.getLogger(__name__)
DEFAULT_API_BASE = "https://api.baichuan-ai.com/v1/chat/completions"
def _convert_message_to_dict(message: BaseMessage) -> dict:
    message_dict: Dict[str, Any]
    if isinstance(message, ChatMessage):
        message_dict = {"role": message.role, "content": message.content}
    elif isinstance(message, HumanMessage):
        message_dict = {"role": "user", "content": message.content}
    elif isinstance(message, AIMessage):
        message_dict = {"role": "assistant", "content": message.content}
    else:
        raise TypeError(f"Got unknown type {message}")
    return message_dict
def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
    role = _dict["role"]
    if role == "user":
        return HumanMessage(content=_dict["content"])
if role == "user":
        return HumanMessage(content=_dict["content"])
    elif role == "assistant":
        return AIMessage(content=_dict.get("content", "") or "")
    else:
        return ChatMessage(content=_dict["content"], role=role)
def _convert_delta_to_message_chunk(
    _dict: Mapping[str, Any], default_class: Type[BaseMessageChunk]
) -> BaseMessageChunk:
    role = _dict.get("role")
    content = _dict.get("content") or ""
    if role == "user" or default_class == HumanMessageChunk:
        return HumanMessageChunk(content=content)
    elif role == "assistant" or default_class == AIMessageChunk:
        return AIMessageChunk(content=content)
    elif role or default_class == ChatMessageChunk:
        return ChatMessageChunk(content=content, role=role)  # type: ignore[arg-type]
    else:
        return default_class(content=content)  # type: ignore[call-arg]
[docs]class ChatBaichuan(BaseChatModel):
    """Baichuan chat models API by Baichuan Intelligent Technology.
    For more information, see https://platform.baichuan-ai.com/docs/api
    """
    @property
    def lc_secrets(self) -> Dict[str, str]:
        return {
            "baichuan_api_key": "BAICHUAN_API_KEY",
        }
    @property
    def lc_serializable(self) -> bool:
        return True
    baichuan_api_base: str = Field(default=DEFAULT_API_BASE)
    """Baichuan custom endpoints"""
    baichuan_api_key: Optional[SecretStr] = Field(default=None, alias="api_key")
    """Baichuan API Key"""
"""Baichuan API Key"""
    baichuan_secret_key: Optional[SecretStr] = None
    """[DEPRECATED, keeping it for for backward compatibility] Baichuan Secret Key"""
    streaming: bool = False
    """Whether to stream the results or not."""
    request_timeout: int = Field(default=60, alias="timeout")
    """request timeout for chat http requests"""
    model = "Baichuan2-Turbo-192K"
    """model name of Baichuan, default is `Baichuan2-Turbo-192K`,
    other options include `Baichuan2-Turbo`"""
    temperature: Optional[float] = Field(default=0.3)
    """What sampling temperature to use."""
    top_k: int = 5
    """What search sampling control to use."""
    top_p: float = 0.85
    """What probability mass to use."""
    with_search_enhance: bool = False
    """Whether to use search enhance, default is False."""
    model_kwargs: Dict[str, Any] = Field(default_factory=dict)
    """Holds any model parameters valid for API call not explicitly specified."""
    class Config:
        """Configuration for this pydantic object."""
        allow_population_by_field_name = True
    @root_validator(pre=True)
    def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        """Build extra kwargs from additional params that were passed in."""
        all_required_field_names = get_pydantic_field_names(cls)
        extra = values.get("model_kwargs", {})
        for field_name in list(values):
            if field_name in extra:
                raise ValueError(f"Found {field_name} supplied twice.")
            if field_name not in all_required_field_names:
if field_name not in all_required_field_names:
                logger.warning(
                    f"""WARNING! {field_name} is not default parameter.
                    {field_name} was transferred to model_kwargs.
                    Please confirm that {field_name} is what you intended."""
                )
                extra[field_name] = values.pop(field_name)
        invalid_model_kwargs = all_required_field_names.intersection(extra.keys())
        if invalid_model_kwargs:
            raise ValueError(
                f"Parameters {invalid_model_kwargs} should be specified explicitly. "
                f"Instead they were passed in as part of `model_kwargs` parameter."
            )
        values["model_kwargs"] = extra
        return values
    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        values["baichuan_api_base"] = get_from_dict_or_env(
            values,
            "baichuan_api_base",
            "BAICHUAN_API_BASE",
            DEFAULT_API_BASE,
        )
        values["baichuan_api_key"] = convert_to_secret_str(
            get_from_dict_or_env(
                values,
                "baichuan_api_key",
                "BAICHUAN_API_KEY",
            )
        )
        return values
    @property
    def _default_params(self) -> Dict[str, Any]:
        """Get the default parameters for calling Baichuan API."""
        normal_params = {
            "model": self.model,
            "temperature": self.temperature,
            "top_p": self.top_p,
            "top_k": self.top_k,
            "with_search_enhance": self.with_search_enhance,
            "stream": self.streaming,
        }
        return {**normal_params, **self.model_kwargs}
    def _generate(
        self,
def _generate(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> ChatResult:
        if self.streaming:
            stream_iter = self._stream(
                messages=messages, stop=stop, run_manager=run_manager, **kwargs
            )
            return generate_from_stream(stream_iter)
        res = self._chat(messages, **kwargs)
        if res.status_code != 200:
            raise ValueError(f"Error from Baichuan api response: {res}")
        response = res.json()
        return self._create_chat_result(response)
    def _stream(
        self,
        messages: List[BaseMessage],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> Iterator[ChatGenerationChunk]:
        res = self._chat(messages, **kwargs)
        if res.status_code != 200:
            raise ValueError(f"Error from Baichuan api response: {res}")
        default_chunk_class = AIMessageChunk
        for chunk in res.iter_lines():
            chunk = chunk.decode("utf-8").strip("\r\n")
            parts = chunk.split("data: ", 1)
            chunk = parts[1] if len(parts) > 1 else None
            if chunk is None:
                continue
            if chunk == "[DONE]":
                break
            response = json.loads(chunk)
            for m in response.get("choices"):
                chunk = _convert_delta_to_message_chunk(
                    m.get("delta"), default_chunk_class
                )