This repository demonstrates how to integrate OpenAI chat functionality into a Spring Boot project.
First, create a new Spring Boot project using your preferred method (e.g., Spring Initializr or an IDE like IntelliJ IDEA or Eclipse). Make sure your project is set up with Maven.
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- WebFlux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- JSON parsing -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
openai:
api-key: ${OPENAI_TOKEN}
assistant:
name: ${ASSISTANT_NAME}
model: ${ASSISTANT_MODEL}
instructions: ${ASSISTANT_INSTRUCTIONS}
@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.
POST /assistant/new
Sends a request to create a new assistant using the configuration defined in application.yml
.
curl --location --request POST 'http://localhost:8080/assistant/new'
Response:
ID: asst_123abc456def...
GET /assistant/{assistantId}
Returns the details of an existing assistant by ID.
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 --location 'http://localhost:8080/assistant/asst_js0TkCNlOgIxqt6ir3gGmt2O/upload' \
--form 'file=@"C:/Users/Documents/data.json"'
Response:
{
"fileId": "file_abc123...",
"vectorStoreId": "vs_xyz456..."
}
GET /assistant/{assistantId}/files
Lists all files attached to the assistant.
curl --location 'http://localhost:8080/assistant/asst_js0TkCNlOgIxqt6ir3gGmt2O/files' \
--header 'OpenAI-Beta: assistants=v2'
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 --location 'http://localhost:8080/threads/chat' \
--data '{
"assistantId": "asst_js0TkCNlOgIxqt6ir3gGmt2O",
"threadId": "thread_rzSQwwxikoALUUlSQ35OIfcM",
"message": "What are the contents of the uploaded file?"
}'
Request:
{
"assistantId": "asst_js0TkCNlOgIxqt6ir3gGmt2O",
"threadId": "", // Optional: leave empty to create a new thread
"message": "What are the contents of the uploaded file?"
}
Response:
{
"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.
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).
- Spring Initializr - Quickly bootstrap a Spring Boot project.
- OpenAI Platform - Learn more about OpenAI and its developer platform.
Feel free to contribute by submitting pull requests or reporting issues.
Contributions are welcome! 🚀
Cheers,
Victor