Open newday1 opened 1 month 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:
MoonshotChat(MoonshotCommon, ChatOpenAI)
in langchain_community.chat_models.moonshot
;
validate_environment()
assigns client as openai.OpenAI().chat.completions
ChatOpenAI
;MoonshotCommon
in langchain_community.llms.moonshot
;
validate_environment()
assigns _client as _MoonshotClient
;MoonshotCommon
itself;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
.
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
Package Information
Optional packages not installed
Other Dependencies