You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by xt...@apache.org on 2026/01/13 01:05:37 UTC
(flink-agents) 01/04: [runtime] Support using JavaChatModelConnection directly in Python
This is an automated email from the ASF dual-hosted git repository.
xtsong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-agents.git
commit 83ce09bd29a24d35965f049bfd9f895b4942c907
Author: youjin <me...@gmail.com>
AuthorDate: Mon Jan 12 13:48:38 2026 +0800
[runtime] Support using JavaChatModelConnection directly in Python
---
.../flink_agents/runtime/java/java_chat_model.py | 43 +++++++++++++++++-----
1 file changed, 33 insertions(+), 10 deletions(-)
diff --git a/python/flink_agents/runtime/java/java_chat_model.py b/python/flink_agents/runtime/java/java_chat_model.py
index a30e54c..f4501e8 100644
--- a/python/flink_agents/runtime/java/java_chat_model.py
+++ b/python/flink_agents/runtime/java/java_chat_model.py
@@ -24,6 +24,7 @@ from flink_agents.api.chat_models.java_chat_model import (
JavaChatModelConnection,
JavaChatModelSetup,
)
+from flink_agents.api.resource import ResourceType
from flink_agents.api.tools.tool import Tool
@@ -36,14 +37,27 @@ class JavaChatModelConnectionImpl(JavaChatModelConnection):
"""
- j_resource: Any
+ _j_resource: Any
+ _j_resource_adapter: Any
+
+ def __init__(self, j_resource: Any, j_resource_adapter: Any, **kwargs: Any) -> None:
+ """Creates a new JavaChatModelSetup.
+
+ Args:
+ j_resource: The Java resource object
+ j_resource_adapter: The Java resource adapter for method invocation
+ **kwargs: Additional keyword arguments
+ """
+ super().__init__(**kwargs)
+ self._j_resource=j_resource
+ self._j_resource_adapter=j_resource_adapter
@override
def chat(
- self,
- messages: Sequence[ChatMessage],
- tools: List[Tool] | None = None,
- **kwargs: Any,
+ self,
+ messages: Sequence[ChatMessage],
+ tools: List[Tool] | None = None,
+ **kwargs: Any,
) -> ChatMessage:
"""Chat method that throws UnsupportedOperationException.
@@ -51,12 +65,21 @@ class JavaChatModelConnectionImpl(JavaChatModelConnection):
Chat operations should be performed on the Java side using the underlying Java
chat model object.
"""
- err_msg = (
- "Chat method of JavaChatModelConnection cannot be called directly from Python runtime. "
- "This connection serves as a Java resource wrapper only. "
- "Chat operations should be performed on the Java side using the underlying Java chat model object."
+ java_messages = [
+ self._j_resource_adapter.fromPythonChatMessage(message)
+ for message in messages
+ ]
+ java_tools = [
+ self._j_resource_adapter.getResource(tool.name, ResourceType.TOOL.value) for tool in tools
+ ]
+ j_response_message = self._j_resource.chat(java_messages, java_tools, kwargs)
+
+ # Convert Java response back to Python format
+ from flink_agents.runtime.python_java_utils import (
+ from_java_chat_message,
)
- raise NotImplementedError(err_msg)
+
+ return from_java_chat_message(j_response_message)
class JavaChatModelSetupImpl(JavaChatModelSetup):