-
Notifications
You must be signed in to change notification settings - Fork 139
[api][runtime][python] Propagate metric groups to cross-language reso… #860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,14 @@ | |
| from flink_agents.api.tools.tool import Tool, ToolMetadata, ToolType | ||
|
|
||
|
|
||
| def set_java_resource_metric_group(j_resource: Any, metric_group: Any) -> None: | ||
| """Bind the underlying Java metric group to a wrapped Java resource.""" | ||
| if j_resource is None: | ||
| return | ||
| j_metric_group = getattr(metric_group, "_j_metric_group", metric_group) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the real flow this is correct —
|
||
| j_resource.setMetricGroup(j_metric_group) | ||
|
|
||
|
|
||
| class JavaTool(Tool): | ||
| """Java Tool that carries tool metadata and can be recognized by PythonChatModel.""" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| ################################################################################ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ################################################################################# | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from flink_agents.runtime.java.java_chat_model import ( | ||
| JavaChatModelConnectionImpl, | ||
| JavaChatModelSetupImpl, | ||
| ) | ||
| from flink_agents.runtime.java.java_embedding_model import ( | ||
| JavaEmbeddingModelConnectionImpl, | ||
| JavaEmbeddingModelSetupImpl, | ||
| ) | ||
| from flink_agents.runtime.java.java_resource_wrapper import ( | ||
| set_java_resource_metric_group, | ||
| ) | ||
|
|
||
|
|
||
| class _JavaResource: | ||
| def __init__(self) -> None: | ||
| self.metric_group: Any = None | ||
|
|
||
| def setMetricGroup(self, metric_group: Any) -> None: | ||
| self.metric_group = metric_group | ||
|
|
||
|
|
||
| class _MetricGroup: | ||
| def __init__(self) -> None: | ||
| self._j_metric_group = object() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "resource", | ||
| [ | ||
| JavaChatModelConnectionImpl( | ||
| j_resource=_JavaResource(), j_resource_adapter=None | ||
| ), | ||
| JavaChatModelSetupImpl( | ||
| j_resource=_JavaResource(), | ||
| j_resource_adapter=None, | ||
| connection="connection", | ||
| model="model", | ||
| ), | ||
| JavaEmbeddingModelConnectionImpl( | ||
| j_resource=_JavaResource(), j_resource_adapter=None | ||
| ), | ||
| JavaEmbeddingModelSetupImpl( | ||
| j_resource=_JavaResource(), | ||
| j_resource_adapter=None, | ||
| connection="connection", | ||
| model="model", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_java_resource_wrappers_forward_metric_group(resource): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new file covers the Python→Java direction well, but only that direction — Would a small unit test in this file's style be worth adding — a fake resource that captures its |
||
| metric_group = _MetricGroup() | ||
|
|
||
| resource.set_metric_group(metric_group) | ||
|
|
||
| assert resource.metric_group is metric_group | ||
| assert resource._j_resource.metric_group is metric_group._j_metric_group | ||
|
|
||
|
|
||
| def test_set_java_resource_metric_group_unwraps_flink_metric_group(): | ||
| java_resource = _JavaResource() | ||
| metric_group = _MetricGroup() | ||
|
|
||
| set_java_resource_metric_group(java_resource, metric_group) | ||
|
|
||
| assert java_resource.metric_group is metric_group._j_metric_group | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The abstract base
Resource(package...api.resource) now imports andinstanceof-checksPythonResourceWrapperfrom the more specialized...api.resource.pythonsub-package. That inverts the usual dependency direction — every Java-only resource (chat models, tools, prompts, vector stores) now carries a compile-time edge to the Python-bridge interface in its base type, and a hypothetical third forwarding flavor would mean editing this base method again rather than overriding it.One option that keeps the base oblivious to the bridge: have each
Python*wrapper overridesetMetricGroupto callsuper.setMetricGroup(...)and thensetPythonResourceMetricGroup(...), pushing the Python concern down into the wrappers wheregetPythonResourceAdapter()already lives. The PR already adds a newgetPythonResourceAdapter()override to all eight wrappers, so a per-wrappersetMetricGroupoverride would touch the same set of files — just in the Python layer instead of the base class. Was the centralizedinstanceofchosen deliberately, or mainly to avoid touching the wrappers?