https://github.com/vicjun22/openai-chat-integration_spring-boot
OpenAI: Chat integration with Spring Boot
https://github.com/vicjun22/openai-chat-integration_spring-boot
java maven openai springboot tutorial
Last synced: 4 months ago
JSON representation
OpenAI: Chat integration with Spring Boot
- Host: GitHub
- URL: https://github.com/vicjun22/openai-chat-integration_spring-boot
- Owner: Vicjun22
- Created: 2025-05-16T20:43:15.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-05-19T18:02:07.000Z (9 months ago)
- Last Synced: 2025-09-27T08:54:39.558Z (4 months ago)
- Topics: java, maven, openai, springboot, tutorial
- Language: Java
- Homepage:
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OpenAI: Chat integration with Spring Boot


This repository demonstrates how to integrate OpenAI chat functionality into a Spring Boot project.
### 1. Create a Spring Boot Project
First, create a new Spring Boot project using your preferred method (e.g., [Spring Initializr](https://start.spring.io/) or an IDE like IntelliJ IDEA or Eclipse). Make sure your project is set up with Maven.
### 2. Add dependencies to the `pom.xml` file:
```xml
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-webflux
com.fasterxml.jackson.core
jackson-databind
```
### 3. Define your secrets in `application.yml`
```yaml
openai:
api-key: ${OPENAI_TOKEN}
assistant:
name: ${ASSISTANT_NAME}
model: ${ASSISTANT_MODEL}
instructions: ${ASSISTANT_INSTRUCTIONS}
```
### 4. Create an OpenAI configuration class:
```java
@Configuration
public class OpenAiConfig {
private final String apiKey;
public OpenAiConfig(@Value("${openai.api-key}") String apiKey) {
this.apiKey = apiKey;
}
@Bean
public WebClient openAiWebClient() {
return WebClient.builder()
.baseUrl("https://api.openai.com/v1")
.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("OpenAI-Beta", "assistants=v2")
.build();
}
}
```
Now that we’ve completed the configuration, we can move on to explaining the available endpoints.
## ⚙️ Assistant Controller
`POST /assistant/new`
Sends a request to create a new assistant using the configuration defined in `application.yml`.
```curl
curl --location --request POST 'http://localhost:8080/assistant/new'
```
**Response:**
```text
ID: asst_123abc456def...
```
`GET /assistant/{assistantId}`
Returns the details of an existing assistant by ID.
```curl
curl --location 'http://localhost:8080/assistant/asst_js0TkCNlOgIxqt6ir3gGmt2O'
```
`POST /assistant/{assistantId}/upload`
Uploads a file and attaches it to the specified assistant.
The file is also stored in a vector store and linked to the assistant for retrieval and search purposes.
```curl
curl --location 'http://localhost:8080/assistant/asst_js0TkCNlOgIxqt6ir3gGmt2O/upload' \
--form 'file=@"C:/Users/Documents/data.json"'
```
**Response:**
```json
{
"fileId": "file_abc123...",
"vectorStoreId": "vs_xyz456..."
}
```
`GET /assistant/{assistantId}/files`
Lists all files attached to the assistant.
```curl
curl --location 'http://localhost:8080/assistant/asst_js0TkCNlOgIxqt6ir3gGmt2O/files' \
--header 'OpenAI-Beta: assistants=v2'
```
## ⚙️ Thread Controller
`POST /threads/chat`
Starts or continues a conversation thread between the user and an assistant.
**If a `threadId` is not provided, a new thread is created.**
The message is added to the thread, the assistant processes it, and the response is returned after execution is complete.
```curl
curl --location 'http://localhost:8080/threads/chat' \
--data '{
"assistantId": "asst_js0TkCNlOgIxqt6ir3gGmt2O",
"threadId": "thread_rzSQwwxikoALUUlSQ35OIfcM",
"message": "What are the contents of the uploaded file?"
}'
```
**Request:**
```json
{
"assistantId": "asst_js0TkCNlOgIxqt6ir3gGmt2O",
"threadId": "", // Optional: leave empty to create a new thread
"message": "What are the contents of the uploaded file?"
}
```
**Response:**
```json
{
"threadId": "thread_xyz789...",
"runId": "run_abc456...",
"answer": "The file contains a list of user transactions from January 2024..."
}
```
**Behavior:**
- If `threadId` is omitted or blank, a new thread is automatically created.
- The message is sent to the assistant, which processes it.
- The system waits for the assistant’s response before replying to the client.
## 📁 Project Resources
The `resources/assets` directory contains the following files:
- `data.json`:
An example input file containing structured data (in Brazilian Portuguese).
- `PROJECT_OPENAI.postman_collection.json`:
A Postman collection with predefined requests to test the API endpoints.
- `instructions.txt`:
A file with prompt instructions for the Assistant (written in Brazilian Portuguese).
---
## 🔗 Useful Links
- [Spring Initializr](https://start.spring.io/) - Quickly bootstrap a Spring Boot project.
- [OpenAI Platform](https://platform.openai.com/docs/overview) - Learn more about OpenAI and its developer platform.
---
Feel free to contribute by submitting pull requests or reporting issues.
Contributions are welcome! 🚀
Cheers,
Victor