langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.42k stars 15.26k forks source link

langchain moonshotChat exmaple didn't work #27058

Open newday1 opened 1 month ago

newday1 commented 1 month ago

Checked other resources

Example Code

from langchain_community.chat_models.moonshot import MoonshotChat from langchain_core.messages import HumanMessage, SystemMessage import os

Generate your api key from: https://platform.moonshot.cn/console/api-keys

os.environ["MOONSHOT_API_KEY"] = "xxxxx"

chat = MoonshotChat()

messages = [ SystemMessage( content="You are a helpful assistant that translates English to French." ), HumanMessage( content="Translate this sentence from English to French. I love programming." ), ]

chat.invoke(messages)

Error Message and Stack Trace (if applicable)


ValidationError Traceback (most recent call last) Cell In[5], line 6 2 from langchain_community.chat_models.moonshot import MoonshotChat 3 from langchain_core.messages import HumanMessage, SystemMessage ----> 6 chat = MoonshotChat() 8 messages = [ 9 SystemMessage( 10 content="You are a helpful assistant that translates English to French." (...) 14 ), 15 ] 17 response = chat.invoke(messages)

File c:\Users\eric\miniconda3\envs\myai\Lib\site-packages\langchain_core_api\deprecation.py:213, in deprecated..deprecate..finalize..warn_if_direct_instance(self, *args, *kwargs) 211 warned = True 212 emit_warning() --> 213 return wrapped(self, args, **kwargs)

File c:\Users\eric\miniconda3\envs\myai\Lib\site-packages\langchain_core\load\serializable.py:111, in Serializable.init(self, *args, kwargs) 109 def init(self, *args: Any, *kwargs: Any) -> None: 110 """""" --> 111 super().init(args, kwargs)

File c:\Users\eric\miniconda3\envs\myai\Lib\site-packages\pydantic\main.py:212, in BaseModel.init(self, **data) ...

ValidationError: 1 validation error for MoonshotChat client Input should be a valid dictionary or instance of _MoonshotClient [type=model_type, input_value=<openai.resources.chat.co...t at 0x000001B3AB6F9220>, input_type=Completions] For further information visit https://errors.pydantic.dev/2.9/v/model_type Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Description

I executed simple use cases from the official documentation, but they didn't work.

System Info

System Information

OS: Windows OS Version: 10.0.22631 Python Version: 3.12.6 | packaged by conda-forge | (main, Sep 30 2024, 17:48:58) [MSC v.1941 64 bit (AMD64)]

Package Information

langchain_core: 0.3.8 langchain: 0.3.1 langchain_community: 0.3.1 langsmith: 0.1.130 langchain_text_splitters: 0.3.0

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.10.8 async-timeout: Installed. No version info available. dataclasses-json: 0.6.7 httpx: 0.27.2 jsonpatch: 1.33 numpy: 1.26.4 orjson: 3.10.7 packaging: 24.1 pydantic: 2.9.2 pydantic-settings: 2.5.2 PyYAML: 6.0.2 requests: 2.32.3 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.35 tenacity: 8.5.0 typing-extensions: 4.12.2

twang2218 commented 5 days ago

I encountered the same issue, with the error message:

pydantic_core._pydantic_core.ValidationError: 1 validation error for MoonshotChat
client
  Input should be a valid dictionary or instance of _MoonshotClient [type=model_type, input_value=<openai.resources.chat.co...s object at 0x116c45a50>, input_type=Completions]
    For further information visit https://errors.pydantic.dev/2.9/v/model_type 

I have researched this issue a little bit.

Initially, in PR #17100, the implementation for Moonshot was added, which defined two classes:

At this time, there was no conflict between the two, one being client and the other _client.

However, in PR #25878 which fixed #24390, _client in MoonshotCommon was changed to client. Since then, a conflict in the definition of client has arisen between MoonshotCommon and MoonshotChat, which caused pydantic validation error.

To fix this issue, I think the type of client in MoonshotCommon should be changed to Any.