Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,30 @@ jobs:
fail-fast: false
matrix:
os: [ 'ubuntu-latest' ]
python-version: [ '3.11' ]
steps:
- uses: actions/checkout@v4
- name: Install java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'
- name: Install flink-agents Java
run: bash tools/build.sh -j
- name: Install python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
version: "latest"
- name: Install flink-agents
run: bash tools/build.sh
- name: Install ollama
run: bash tools/start_ollama_server.sh
- name: Run Java IT
run: tools/ut.sh -j -e
run: |
export PYTHONPATH="${{ github.workspace }}/python/.venv/lib/python${{ matrix.python-version }}/site-packages:$PYTHONPATH"
tools/ut.sh -j -e
cross_language_tests:
name: cross-language [${{ matrix.os }}] [${{ matrix.python-version}}]
Expand Down
6 changes: 6 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ under the License.
<version>${flink.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>pemja</artifactId>
<version>${pemja.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
*/
public class ChatMessage {

/** The key for the message type in the metadata. */
public static final String MESSAGE_TYPE = "messageType";

private MessageRole role;
private String content;
private List<Map<String, Object>> toolCalls;
Expand Down Expand Up @@ -68,7 +65,6 @@ public ChatMessage(
this.content = content != null ? content : "";
this.toolCalls = toolCalls != null ? toolCalls : new ArrayList<>();
this.extraArgs = extraArgs != null ? new HashMap<>(extraArgs) : new HashMap<>();
this.extraArgs.put(MESSAGE_TYPE, this.role);
}

public MessageRole getRole() {
Expand All @@ -77,7 +73,6 @@ public MessageRole getRole() {

public void setRole(MessageRole role) {
this.role = role;
this.extraArgs.put(MESSAGE_TYPE, this.role);
}

public String getContent() {
Expand All @@ -102,7 +97,6 @@ public Map<String, Object> getExtraArgs() {

public void setExtraArgs(Map<String, Object> extraArgs) {
this.extraArgs = extraArgs != null ? extraArgs : new HashMap<>();
this.extraArgs.put(MESSAGE_TYPE, this.role);
}

@JsonIgnore
Expand Down
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.");
}
}
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();
}
}
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);
}
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();
}
Loading