-
Notifications
You must be signed in to change notification settings - Fork 76
[Feature][runtime] Support the use of Python ChatModel in Java #314
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
Open
GreatEugenius
wants to merge
1
commit into
apache:main
Choose a base branch
from
GreatEugenius:python-chatmodel-in-java
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,439
−48
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...rc/main/java/org/apache/flink/agents/api/chat/model/python/PythonChatModelConnection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.flink.agents.api.chat.model.python; | ||
|
|
||
| import org.apache.flink.agents.api.chat.messages.ChatMessage; | ||
| import org.apache.flink.agents.api.chat.model.BaseChatModelConnection; | ||
| import org.apache.flink.agents.api.resource.Resource; | ||
| import org.apache.flink.agents.api.resource.ResourceDescriptor; | ||
| import org.apache.flink.agents.api.resource.ResourceType; | ||
| import org.apache.flink.agents.api.resource.python.PythonResourceAdapter; | ||
| import org.apache.flink.agents.api.resource.python.PythonResourceWrapper; | ||
| import org.apache.flink.agents.api.tools.Tool; | ||
| import pemja.core.object.PyObject; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| /** | ||
| * Python-based implementation of ChatModelConnection that wraps a Python chat model object. This | ||
| * class serves as a bridge between Java and Python chat model environments, but unlike {@link | ||
| * PythonChatModelSetup}, it does not provide direct chat functionality in Java. | ||
| */ | ||
| public class PythonChatModelConnection extends BaseChatModelConnection | ||
| implements PythonResourceWrapper { | ||
| private PyObject chatModel; | ||
|
|
||
| /** | ||
| * Creates a new PythonChatModelConnection. | ||
| * | ||
| * @param adapter The Python resource adapter (required by PythonResourceProvider's | ||
| * reflection-based instantiation but not used directly in this implementation) | ||
| * @param chatModel The Python chat model object | ||
| * @param descriptor The resource descriptor | ||
| * @param getResource Function to retrieve resources by name and type | ||
| */ | ||
| public PythonChatModelConnection( | ||
| PythonResourceAdapter adapter, | ||
| PyObject chatModel, | ||
| ResourceDescriptor descriptor, | ||
| BiFunction<String, ResourceType, Resource> getResource) { | ||
| super(descriptor, getResource); | ||
| this.chatModel = chatModel; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getPythonResource() { | ||
| return chatModel; | ||
| } | ||
|
|
||
| @Override | ||
| public ChatMessage chat( | ||
| List<ChatMessage> messages, List<Tool> tools, Map<String, Object> arguments) { | ||
| throw new UnsupportedOperationException( | ||
| "Chat method of PythonChatModelConnection cannot be called directly from Java runtime. " | ||
| + "This connection serves as a Python resource wrapper only. " | ||
| + "Chat operations should be performed on the Python side using the underlying Python chat model object."); | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
api/src/main/java/org/apache/flink/agents/api/chat/model/python/PythonChatModelSetup.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.flink.agents.api.chat.model.python; | ||
|
|
||
| import org.apache.flink.agents.api.chat.messages.ChatMessage; | ||
| import org.apache.flink.agents.api.chat.model.BaseChatModelSetup; | ||
| import org.apache.flink.agents.api.resource.Resource; | ||
| import org.apache.flink.agents.api.resource.ResourceDescriptor; | ||
| import org.apache.flink.agents.api.resource.ResourceType; | ||
| import org.apache.flink.agents.api.resource.python.PythonResourceAdapter; | ||
| import org.apache.flink.agents.api.resource.python.PythonResourceWrapper; | ||
| import pemja.core.object.PyObject; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| /** | ||
| * Python-based implementation of ChatModelSetup that bridges Java and Python chat model | ||
| * functionality. This class wraps a Python chat model setup object and provides Java interface | ||
| * compatibility while delegating actual chat operations to the underlying Python implementation. | ||
| */ | ||
| public class PythonChatModelSetup extends BaseChatModelSetup implements PythonResourceWrapper { | ||
| static final String FROM_JAVA_CHAT_MESSAGE = "python_java_utils.from_java_chat_message"; | ||
|
|
||
| static final String TO_JAVA_CHAT_MESSAGE = "python_java_utils.to_java_chat_message"; | ||
|
|
||
| private final PyObject chatModelSetup; | ||
| private final PythonResourceAdapter adapter; | ||
|
|
||
| public PythonChatModelSetup( | ||
| PythonResourceAdapter adapter, | ||
| PyObject chatModelSetup, | ||
| ResourceDescriptor descriptor, | ||
| BiFunction<String, ResourceType, Resource> getResource) { | ||
| super(descriptor, getResource); | ||
| this.chatModelSetup = chatModelSetup; | ||
| this.adapter = adapter; | ||
| } | ||
|
|
||
| @Override | ||
| public ChatMessage chat(List<ChatMessage> messages, Map<String, Object> parameters) { | ||
| if (chatModelSetup == null) { | ||
| throw new IllegalStateException( | ||
| "ChatModelSetup is not initialized. Cannot perform chat operation."); | ||
| } | ||
|
|
||
| Map<String, Object> kwargs = new HashMap<>(parameters); | ||
|
|
||
| List<Object> pythonMessages = new ArrayList<>(); | ||
| for (ChatMessage message : messages) { | ||
| pythonMessages.add(toPythonChatMessage(message)); | ||
| } | ||
|
|
||
| kwargs.put("messages", pythonMessages); | ||
|
|
||
| Object pythonMessageResponse = adapter.callMethod(chatModelSetup, "chat", kwargs); | ||
| return fromPythonChatMessage(pythonMessageResponse); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Java ChatMessage object to its Python equivalent. | ||
| * | ||
| * @param message the Java ChatMessage to convert | ||
| * @return the Python representation of the chat message | ||
| */ | ||
| private Object toPythonChatMessage(ChatMessage message) { | ||
| return adapter.invoke(FROM_JAVA_CHAT_MESSAGE, message); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a Python chat message object back to a Java ChatMessage. | ||
| * | ||
| * @param pythonChatMessage the Python chat message object to convert | ||
| * @return the Java ChatMessage representation | ||
| */ | ||
| private ChatMessage fromPythonChatMessage(Object pythonChatMessage) { | ||
| ChatMessage message = (ChatMessage) adapter.invoke(TO_JAVA_CHAT_MESSAGE, pythonChatMessage); | ||
|
|
||
| return message; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getPythonResource() { | ||
| return chatModelSetup; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> getParameters() { | ||
| return Map.of(); | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
api/src/main/java/org/apache/flink/agents/api/resource/python/PythonResourceAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.flink.agents.api.resource.python; | ||
|
|
||
| import pemja.core.object.PyObject; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Adapter interface for managing Python resources and facilitating Java-Python interoperability. | ||
| * This interface provides methods to interact with Python objects, invoke Python methods, and | ||
| * handle data conversion between Java and Python environments. | ||
| */ | ||
| public interface PythonResourceAdapter { | ||
|
|
||
| /** | ||
| * Retrieves a Python resource by name and type. | ||
| * | ||
| * @param resourceName the name of the resource to retrieve | ||
| * @param resourceType the type of the resource | ||
| * @return the retrieved resource object | ||
| */ | ||
| Object getResource(String resourceName, String resourceType); | ||
|
|
||
| /** | ||
| * Initializes a Python resource instance from the specified module and class. | ||
| * | ||
| * @param module the Python module containing the target class | ||
| * @param clazz the Python class name to instantiate | ||
| * @param kwargs keyword arguments to pass to the Python class constructor | ||
| * @return a PyObject representing the initialized Python resource | ||
| */ | ||
| PyObject initPythonResource(String module, String clazz, Map<String, Object> kwargs); | ||
|
|
||
| /** | ||
| * Invokes a method on a Python object with the specified parameters. | ||
| * | ||
| * @param obj the Python object on which to call the method | ||
| * @param methodName the name of the method to invoke | ||
| * @param kwargs keyword arguments to pass to the method | ||
| * @return the result of the method invocation | ||
| */ | ||
| Object callMethod(Object obj, String methodName, Map<String, Object> kwargs); | ||
|
|
||
| /** | ||
| * Invokes a method with the specified name and arguments. | ||
| * | ||
| * @param name the name of the method to invoke | ||
| * @param args the arguments to pass to the method | ||
| * @return the result of the method invocation | ||
| */ | ||
| Object invoke(String name, Object... args); | ||
| } |
32 changes: 32 additions & 0 deletions
32
api/src/main/java/org/apache/flink/agents/api/resource/python/PythonResourceWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package org.apache.flink.agents.api.resource.python; | ||
|
|
||
| /** | ||
| * Wrapper interface for Python resource objects. This interface provides a unified way to access | ||
| * the underlying Python resource from Java objects that encapsulate Python functionality. | ||
| */ | ||
| public interface PythonResourceWrapper { | ||
|
|
||
| /** | ||
| * Retrieves the underlying Python resource object. | ||
| * | ||
| * @return the wrapped Python resource object | ||
| */ | ||
| Object getPythonResource(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.