Fix MoonshotChat instantiate with alias (#25755)

- **Description:**
   -  Fix `MoonshotChat` instantiate with alias
   - Add `MoonshotChat` to `__init__.py`

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
maang-h 2024-08-27 01:33:22 +08:00 committed by GitHub
parent ec99f0d193
commit a566a15930
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 2 deletions

View File

@ -122,6 +122,9 @@ if TYPE_CHECKING:
from langchain_community.chat_models.mlx import (
ChatMLX,
)
from langchain_community.chat_models.moonshot import (
MoonshotChat,
)
from langchain_community.chat_models.oci_generative_ai import (
ChatOCIGenAI, # noqa: F401
)
@ -224,6 +227,7 @@ __all__ = [
"JinaChat",
"LlamaEdgeChatService",
"MiniMaxChat",
"MoonshotChat",
"PaiEasChatEndpoint",
"PromptLayerChatOpenAI",
"QianfanChatEndpoint",
@ -280,6 +284,7 @@ _module_lookup = {
"JinaChat": "langchain_community.chat_models.jinachat",
"LlamaEdgeChatService": "langchain_community.chat_models.llama_edge",
"MiniMaxChat": "langchain_community.chat_models.minimax",
"MoonshotChat": "langchain_community.chat_models.moonshot",
"PaiEasChatEndpoint": "langchain_community.chat_models.pai_eas_endpoint",
"PromptLayerChatOpenAI": "langchain_community.chat_models.promptlayer_openai",
"SolarChat": "langchain_community.chat_models.solar",

View File

@ -33,7 +33,11 @@ class MoonshotChat(MoonshotCommon, ChatOpenAI): # type: ignore[misc]
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the environment is set up correctly."""
values["moonshot_api_key"] = convert_to_secret_str(
get_from_dict_or_env(values, "moonshot_api_key", "MOONSHOT_API_KEY")
get_from_dict_or_env(
values,
["moonshot_api_key", "api_key", "openai_api_key"],
"MOONSHOT_API_KEY",
)
)
try:

View File

@ -1,9 +1,10 @@
"""Test Moonshot Chat Model."""
from typing import Type
from typing import Type, cast
import pytest
from langchain_core.language_models import BaseChatModel
from langchain_core.pydantic_v1 import SecretStr
from langchain_standard_tests.integration_tests import ChatModelIntegrationTests
from langchain_community.chat_models.moonshot import MoonshotChat
@ -21,3 +22,10 @@ class TestMoonshotChat(ChatModelIntegrationTests):
@pytest.mark.xfail(reason="Not yet implemented.")
def test_usage_metadata(self, model: BaseChatModel) -> None:
super().test_usage_metadata(model)
def test_chat_moonshot_instantiate_with_alias() -> None:
"""Test MoonshotChat instantiate when using alias."""
api_key = "your-api-key"
chat = MoonshotChat(api_key=api_key) # type: ignore[call-arg]
assert cast(SecretStr, chat.moonshot_api_key).get_secret_value() == api_key

View File

@ -48,6 +48,7 @@ EXPECTED_ALL = [
"JinaChat",
"LlamaEdgeChatService",
"MiniMaxChat",
"MoonshotChat",
"PaiEasChatEndpoint",
"PromptLayerChatOpenAI",
"SolarChat",