yandex-cloud / yandex-cloud-ml-sdk

Other
25 stars 0 forks source link

Langchain Integration doesn't work with langchain_core>=0.3 #6

Open isbalashov opened 3 days ago

isbalashov commented 3 days ago

Description:

Langchain integration example doesn't work - AttributeError: Config in yandex_cloud_ml_sdk

Steps to Reproduce:

  1. Run example:

from yandex_cloud_ml_sdk import YCloudML
from langchain_core.messages import AIMessage, HumanMessage

sdk = YCloudML(folder_id="...", auth="<APIKey/IAMToken/SomethingElse>")

model = sdk.models.completions('yandexgpt').langchain()

langchain_result = model.invoke([
    HumanMessage(content="hello!"),
    AIMessage(content="Hi there human!"),
    HumanMessage(content="Meow!"),
])

Error Traceback:

AttributeError
Traceback (most recent call last)
Cell In[6], line 6
      2 from langchain_core.messages import AIMessage, HumanMessage
      4 sdk = YCloudML(folder_id="xxxxxx", auth="xxxxxxxxxxxxxx")
----> 6 model = sdk.models.completions('yandexgpt').langchain()
      8 langchain_result = model.invoke([
      9     HumanMessage(content="hello!"),
     10     AIMessage(content="Hi there human!"),
     11     HumanMessage(content="Meow!"),
     12 ])

File xxxxxxxxxx/.venv/lib/python3.12/site-packages/yandex_cloud_ml_sdk/_models/completions/model.py:39, in BaseGPTModel.langchain(self, model_type, timeout)
     38 def langchain(self, model_type: Literal["chat"] = "chat", timeout: int = 60) -> BaseYandexLanguageModel:
---> 39     from .langchain import ChatYandexGPT  # pylint: disable=import-outside-toplevel
     41     if model_type == "chat":
     42     # idk why but pylint thinks this class still abstract
     43         return ChatYandexGPT(ycmlsdk_model=self, timeout=timeout)  # pylint: disable=abstract-class-instantiated

File xxxxxxxxxx/.venv/lib/python3.12/site-packages/yandex_cloud_ml_sdk/_models/completions/langchain.py:12
      9 from langchain_core.messages.ai import UsageMetadata
     10 from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
---> 12 from yandex_cloud_ml_sdk._types.langchain import BaseYandexLanguageModel
     13 from yandex_cloud_ml_sdk._utils.langchain import make_async_run_manager
     14 from yandex_cloud_ml_sdk._utils.sync import run_sync_generator_impl, run_sync_impl

File xxxxxxxxxx/.venv/lib/python3.12/site-packages/yandex_cloud_ml_sdk/_types/langchain.py:15
     10 from yandex_cloud_ml_sdk._types.model import BaseModel
     12 ModelTypeT = TypeVar('ModelTypeT', bound=BaseModel)
---> 15 class BaseYandexModel(LangchainModel, Generic[ModelTypeT]):
     16     ycmlsdk_model: ModelTypeT
     17     timeout: int = 60

File xxxxxxxxxx/.venv/lib/python3.12/site-packages/yandex_cloud_ml_sdk/_types/langchain.py:19, in BaseYandexModel()
     16 ycmlsdk_model: ModelTypeT
     17 timeout: int = 60
---> 19 class Config(Serializable.Config):
     20     arbitrary_types_allowed = True

File xxxxxxxxxx/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py:262, in ModelMetaclass.__getattr__(self, item)
    260 if private_attributes and item in private_attributes:
    261     return private_attributes[item]
--> 262 raise AttributeError(item)

AttributeError: Config

Environment:

vhaldemar commented 3 days ago

Hey! Langchain just updated its internals and we need to adapt our integration with it. Meanwhile you could downgrade langchain-core>=0.2.29,<=0.2.41

isbalashov commented 3 days ago

Yes, downgrading to langchain-core>=0.2.29,<=0.2.41 did the trick

vhaldemar commented 3 days ago

I will leave this ticket open until I make the correct fix.