Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.

Commit ed4772a

Browse files
authored
1.2.4 - Openai integration (#120)
1 parent ec4ea6e commit ed4772a

File tree

17 files changed

+368
-8
lines changed

17 files changed

+368
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Lien du repository Maven : [FunixProductions - Core spring package](https://mvn.
1212
<parent>
1313
<groupId>com.funixproductions.core</groupId>
1414
<artifactId>funixproductions-core</artifactId>
15-
<version>1.2.3</version>
15+
<version>1.2.4</version>
1616
</parent>
1717

1818
<repository>

crud/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.funixproductions.core</groupId>
99
<artifactId>funixproductions-core</artifactId>
10-
<version>1.2.3</version>
10+
<version>1.2.4</version>
1111
<relativePath>../pom.xml</relativePath>
1212
</parent>
1313

exceptions/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.funixproductions.core</groupId>
99
<artifactId>funixproductions-core</artifactId>
10-
<version>1.2.3</version>
10+
<version>1.2.4</version>
1111
</parent>
1212

1313
<groupId>com.funixproductions.core.exceptions</groupId>

integrations/openai/pom.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>com.funixproductions.core.integrations</groupId>
9+
<artifactId>funixproductions-core-integrations</artifactId>
10+
<version>1.2.4</version>
11+
</parent>
12+
13+
<groupId>com.funixproductions.core.integrations.openai</groupId>
14+
<artifactId>funixproductions-core-openai</artifactId>
15+
16+
<properties>
17+
<maven.compiler.source>${java.version}</maven.compiler.source>
18+
<maven.compiler.target>${java.version}</maven.compiler.target>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.funixproductions.core.crud</groupId>
25+
<artifactId>crud</artifactId>
26+
<version>${com.funixproductions.core.version}</version>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.clients;
2+
3+
import com.funixproductions.core.integrations.openai.chatgpt.dtos.ChatGptRequest;
4+
import com.funixproductions.core.integrations.openai.chatgpt.dtos.ChatGptResponse;
5+
import org.springframework.cloud.openfeign.FeignClient;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestHeader;
10+
11+
@FeignClient(
12+
name = "chat-gpt-client",
13+
url = "https://api.openai.com/v1/responses"
14+
)
15+
public interface ChatGptClient {
16+
17+
@PostMapping(
18+
consumes = MediaType.APPLICATION_JSON_VALUE,
19+
produces = MediaType.APPLICATION_JSON_VALUE
20+
)
21+
ChatGptResponse sendRequest(
22+
@RequestBody ChatGptRequest request,
23+
@RequestHeader("Authorization") String token
24+
);
25+
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.configs;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Getter
9+
@Setter
10+
@Configuration
11+
@ConfigurationProperties(prefix = "funixproductions.openai.chatgpt")
12+
public class ChatGptConfig {
13+
14+
/**
15+
* The OpenAI API key.
16+
*/
17+
private String apiKey;
18+
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.dtos;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
import java.util.Collection;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
public class ChatGptRequest {
11+
12+
private final String model;
13+
14+
private final Collection<Input> input;
15+
16+
@Getter
17+
@AllArgsConstructor
18+
public static class Input {
19+
private final String role;
20+
private final String content;
21+
}
22+
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.dtos;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import org.springframework.lang.Nullable;
8+
9+
import java.util.List;
10+
import java.util.NoSuchElementException;
11+
12+
@Getter
13+
@Setter
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
public class ChatGptResponse {
17+
18+
private List<Output> output;
19+
20+
@Getter
21+
@Setter
22+
@AllArgsConstructor
23+
@NoArgsConstructor
24+
public static class Output {
25+
private List<Content> content;
26+
}
27+
28+
@Getter
29+
@Setter
30+
@AllArgsConstructor
31+
@NoArgsConstructor
32+
public static class Content {
33+
private String text;
34+
}
35+
36+
@Nullable
37+
public String getText() {
38+
try {
39+
if (this.output != null) {
40+
final Output output = this.output.getFirst();
41+
42+
if (output != null && output.getContent() != null) {
43+
final Content content = output.getContent().getFirst();
44+
45+
if (content != null) {
46+
return content.getText();
47+
}
48+
}
49+
}
50+
51+
return null;
52+
} catch (NoSuchElementException e) {
53+
return null;
54+
}
55+
}
56+
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.enums;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public enum ChatGptModel {
9+
/**
10+
* <a href="https://platform.openai.com/docs/models/o4-mini">o4-mini</a>
11+
*/
12+
GPT_o4_MINI("o4-mini"),
13+
14+
/**
15+
* <a href="https://platform.openai.com/docs/models/gpt-4.1">Flagship GPT model for complex tasks</a>
16+
*/
17+
GPT_4_1("gpt-4.1"),
18+
19+
/**
20+
* <a href="https://platform.openai.com/docs/models/gpt-4o">Fast, intelligent, flexible GPT model</a>
21+
*/
22+
GPT_4o("gpt-4o"),
23+
24+
/**
25+
* <a href="https://platform.openai.com/docs/models/gpt-3.5-turbo">Legacy GPT model for cheaper chat and non-chat tasks</a>
26+
*/
27+
GPT_3_5_TURBO("gpt-3.5-turbo");
28+
29+
private final String model;
30+
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.funixproductions.core.integrations.openai.chatgpt.services;
2+
3+
import com.funixproductions.core.exceptions.ApiException;
4+
import com.funixproductions.core.integrations.openai.chatgpt.clients.ChatGptClient;
5+
import com.funixproductions.core.integrations.openai.chatgpt.configs.ChatGptConfig;
6+
import com.funixproductions.core.integrations.openai.chatgpt.dtos.ChatGptRequest;
7+
import com.funixproductions.core.integrations.openai.chatgpt.dtos.ChatGptResponse;
8+
import com.funixproductions.core.integrations.openai.chatgpt.enums.ChatGptModel;
9+
import com.google.common.base.Strings;
10+
import lombok.NonNull;
11+
import lombok.RequiredArgsConstructor;
12+
import org.springframework.lang.Nullable;
13+
import org.springframework.stereotype.Service;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
@Service
19+
@RequiredArgsConstructor
20+
public class ChatGptService {
21+
22+
private final ChatGptClient chatGptClient;
23+
private final ChatGptConfig chatGptConfig;
24+
25+
/**
26+
* Send a request to the OpenAI API.
27+
* @param model the model to use for the request.
28+
* @param devPrompt developer messages are instructions provided by the application developer, prioritized ahead of user messages.
29+
* @param prompt user messages are instructions provided by an end user, prioritized behind developer messages.
30+
* @return the response from the OpenAI API.
31+
*/
32+
public String sendGptRequest(
33+
final @NonNull ChatGptModel model,
34+
final @Nullable String devPrompt,
35+
final @NonNull String prompt
36+
) throws ApiException {
37+
try {
38+
final List<ChatGptRequest.Input> inputs = new ArrayList<>();
39+
40+
inputs.add(new ChatGptRequest.Input("user", prompt));
41+
if (!Strings.isNullOrEmpty(devPrompt)) {
42+
inputs.add(new ChatGptRequest.Input("developer", devPrompt));
43+
}
44+
45+
final ChatGptResponse response = this.chatGptClient.sendRequest(
46+
new ChatGptRequest(model.getModel(), inputs),
47+
"Bearer " + this.chatGptConfig.getApiKey()
48+
);
49+
final String text = response.getText();
50+
51+
if (Strings.isNullOrEmpty(text)) {
52+
throw new ApiException("Failed to get response from OpenAI API, empty response");
53+
} else {
54+
return text;
55+
}
56+
} catch (ApiException e) {
57+
throw e;
58+
} catch (Exception e) {
59+
throw new ApiException("Failed to send request to OpenAI API", e);
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
 (0)