Skip to content

Commit 00c7c3d

Browse files
authored
[Feature][Java] Add OpenAI chat model integration (#320)
1 parent 680bba9 commit 00c7c3d

File tree

9 files changed

+740
-5
lines changed

9 files changed

+740
-5
lines changed

e2e-test/flink-agents-end-to-end-tests-integration/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ under the License.
6464
<artifactId>flink-agents-integrations-chat-models-azureai</artifactId>
6565
<version>${project.version}</version>
6666
</dependency>
67+
<dependency>
68+
<groupId>org.apache.flink</groupId>
69+
<artifactId>flink-agents-integrations-chat-models-openai</artifactId>
70+
<version>${project.version}</version>
71+
</dependency>
6772
<dependency>
6873
<groupId>org.apache.flink</groupId>
6974
<artifactId>flink-agents-integrations-chat-models-ollama</artifactId>
@@ -76,4 +81,4 @@ under the License.
7681
</dependency>
7782
</dependencies>
7883

79-
</project>
84+
</project>

e2e-test/flink-agents-end-to-end-tests-integration/src/test/java/org/apache/flink/agents/integration/test/ChatModelIntegrationAgent.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.apache.flink.agents.integrations.chatmodels.azureai.AzureAIChatModelSetup;
3838
import org.apache.flink.agents.integrations.chatmodels.ollama.OllamaChatModelConnection;
3939
import org.apache.flink.agents.integrations.chatmodels.ollama.OllamaChatModelSetup;
40+
import org.apache.flink.agents.integrations.chatmodels.openai.OpenAIChatModelConnection;
41+
import org.apache.flink.agents.integrations.chatmodels.openai.OpenAIChatModelSetup;
4042

4143
import java.util.Collections;
4244
import java.util.List;
@@ -80,6 +82,11 @@ public static ResourceDescriptor chatModelConnection() {
8082
.addInitialArgument("endpoint", endpoint)
8183
.addInitialArgument("apiKey", apiKey)
8284
.build();
85+
} else if (provider.equals("OPENAI")) {
86+
String apiKey = System.getenv().get("OPENAI_API_KEY");
87+
return ResourceDescriptor.Builder.newBuilder(OpenAIChatModelConnection.class.getName())
88+
.addInitialArgument("api_key", apiKey)
89+
.build();
8390
} else {
8491
throw new RuntimeException(String.format("Unknown model provider %s", provider));
8592
}
@@ -105,6 +112,14 @@ public static ResourceDescriptor chatModel() {
105112
"tools",
106113
List.of("calculateBMI", "convertTemperature", "createRandomNumber"))
107114
.build();
115+
} else if (provider.equals("OPENAI")) {
116+
return ResourceDescriptor.Builder.newBuilder(OpenAIChatModelSetup.class.getName())
117+
.addInitialArgument("connection", "chatModelConnection")
118+
.addInitialArgument("model", "gpt-4o-mini")
119+
.addInitialArgument(
120+
"tools",
121+
List.of("calculateBMI", "convertTemperature", "createRandomNumber"))
122+
.build();
108123
} else {
109124
throw new RuntimeException(String.format("Unknown model provider %s", provider));
110125
}

e2e-test/flink-agents-end-to-end-tests-integration/src/test/java/org/apache/flink/agents/integration/test/ChatModelIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ChatModelIntegrationTest() throws IOException {
5252
}
5353

5454
@ParameterizedTest()
55-
@ValueSource(strings = {"OLLAMA", "AZURE"})
55+
@ValueSource(strings = {"OLLAMA", "AZURE", "OPENAI"})
5656
public void testChatModeIntegration(String provider) throws Exception {
5757
Assumptions.assumeTrue(
5858
(OLLAMA.equals(provider) && ollamaReady)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
Unless required by applicable law or agreed to in writing,
12+
software distributed under the License is distributed on an
13+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations
16+
under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.apache.flink</groupId>
25+
<artifactId>flink-agents-integrations-chat-models</artifactId>
26+
<version>0.2-SNAPSHOT</version>
27+
<relativePath>../pom.xml</relativePath>
28+
</parent>
29+
30+
<artifactId>flink-agents-integrations-chat-models-openai</artifactId>
31+
<name>Flink Agents : Integrations: Chat Models: OpenAI</name>
32+
<packaging>jar</packaging>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apache.flink</groupId>
37+
<artifactId>flink-agents-api</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.flink</groupId>
42+
<artifactId>flink-agents-plan</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.openai</groupId>
48+
<artifactId>openai-java</artifactId>
49+
<version>${openai.version}</version>
50+
</dependency>
51+
</dependencies>
52+
53+
</project>
54+

0 commit comments

Comments
 (0)