feat(langchain): migrate ChatYandexGPT to Chat API#208
feat(langchain): migrate ChatYandexGPT to Chat API#208temrjan wants to merge 2 commits intoyandex-cloud:masterfrom
Conversation
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ainvoke(model, chat_history): |
There was a problem hiding this comment.
I mean, it is not going to work, because we have no network access at tests.
We usually using pytest-recording for tests like this, recording cassetetes with network responses/requests
| >>> result = model.invoke([HumanMessage("Hello!")]) | ||
| """ | ||
|
|
||
| class Config: |
There was a problem hiding this comment.
I remembered one more thing about our langchain support: we are restricting langchain-core dependency version, because new lanchain have new pydantic dependency, which is breaking backward compatibility.
Probably it have to be fixed if we want to bring this integration back to living world :D
But anyway, this is not about this PR.
| class ChatYandexGPT(BaseYandexLanguageModel[ChatAPIModel], LCBaseChatModel): | ||
| """LangChain chat model for Yandex GPT via Chat API. | ||
|
|
||
| Uses HTTP ``/_chat/completions/`` (not deprecated GRPC). |
There was a problem hiding this comment.
I'm kinda feel that claude didn't understand some contexts here.
I mean, HTTP /_chat/completions/ is means nothing for ordinary user who will read this doc
| #!/usr/bin/env python3 | ||
| """LangChain invoke example using the Chat API backend. | ||
|
|
||
| Uses sdk.chat.completions() instead of sdk.models.completions() |
There was a problem hiding this comment.
This is unnecessary info for example :)
|
Thanks for the review! Pushed fixes: Tests — switched to
Docstrings — removed implementation details ( Cassettes — I don't have API access to record them. Could you help record the VCR cassettes, or point me to a test folder_id/API key I could use? pytest tests/langchain_/test_chat_api.py --record-mode=once --env langchain_core -v |
I'm not sure, I will try to ask my collegues, if it is posibble to give you some kind of grant to use yandex cloud. |
|
Hey!
|
|
Hey @vhaldemar! Thanks for the guidance. Done — created a support ticket: KF218173 Let me know if anything else is needed from my side! |
|
Hey, collegues told me your ticket is done. Also, please? make a rebase or merge to master to fix docs build issue. |
Add LangChain integration using the new HTTP Chat API endpoint. Includes VCR-based tests and example script.
89b9103 to
7a6bd6e
Compare
Record HTTP cassettes for test_ainvoke and test_astream. Fix usage_metadata assertion to use dict access.
|
Hey @vhaldemar! Rebased on master, docs build should be fixed now. Cassettes are recorded with real API. Ready for another review when you have time! |
Summary
Migrates the LangChain integration from the deprecated GRPC Foundation Models API to the modern Chat API (
/_chat/completions/), enabling tool call support inChatYandexGPT.ChatYandexGPTin_chat/completions/langchain.pyusing HTTP RESTtool_callsfrom Chat API response intoAIMessage.tool_callstool_call_chunks.langchain()method toBaseChatModellangchainas optional dependency_models/completions/langchain.pyuntouchedChanges
_chat/completions/langchain.py_chat/completions/model.py.langchain()methodtests/langchain_/test_chat_api.pyexamples/langchain/chat/chat_api_invoke.pyDesign decisions
_transform_messages()converts LangChain messages → Chat API dicts, then existingmessages_to_json()handles serialization_parse_tool_calls()filters out malformed tool calls (tc.function is None)tool_call_chunkswithargsas JSON string (LangChain convention)run_sync_impl(same pattern as legacy adapter)HumanMessage.contentTest plan
test_ainvoke/test_invoke— basic completiontest_astream/test_stream— streamingtest_usage_metadata— token usage statstest_response_metadata— finish_reason, modelNext
This PR is the foundation for a follow-up PR adding
bind_tools()support (LangChain tools → Yandex FunctionTool conversion + full round-trip).Refs #142
🤖 Generated with Claude Code