Skip to content

Commit 3b98b94

Browse files
sunyuhan1998ilayaperumalg
authored andcommitted
doc: Add documentation for OpenAiFileApi
Signed-off-by: Sun Yuhan <[email protected]>
1 parent fa68d1e commit 3b98b94

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,46 @@ Follow the https://github.com/spring-projects/spring-ai/blob/main/models/spring-
666666
* The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java[OpenAiApiToolFunctionCallIT.java] tests show how to use the low-level API to call tool functions.
667667
Based on the link:https://platform.openai.com/docs/guides/function-calling/parallel-function-calling[OpenAI Function Calling] tutorial.
668668

669+
== Low-level OpenAiFileApi Client [[low-level-file-api]]
670+
671+
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiFileApi.java[OpenAiFileApi] provides a lightweight Java client for the OpenAI Files API, enabling file management operations such as uploading, listing, retrieving, deleting files, and accessing file contents. link:https://platform.openai.com/docs/api-reference/files[OpenAI File API]
672+
673+
Here is a simple snippet showing how to use the API programmatically:
674+
675+
[source,java]
676+
----
677+
OpenAiFileApi openAiFileApi = OpenAiFileApi.builder()
678+
.apiKey(new SimpleApiKey(System.getenv("OPENAI_API_KEY")))
679+
.build();
680+
681+
// Upload a file
682+
byte[] fileBytes = Files.readAllBytes(Paths.get("evals.jsonl"));
683+
OpenAiFileApi.UploadFileRequest uploadRequest = OpenAiFileApi.UploadFileRequest.builder()
684+
.file(fileBytes)
685+
.fileName("evals-data.jsonl")
686+
.purpose(OpenAiFileApi.Purpose.EVALS)
687+
.build();
688+
ResponseEntity<OpenAiFileApi.FileObject> uploadResponse = openAiFileApi.uploadFile(uploadRequest);
689+
690+
// List files
691+
OpenAiFileApi.ListFileRequest listRequest = OpenAiFileApi.ListFileRequest.builder()
692+
.purpose(OpenAiFileApi.Purpose.EVALS)
693+
.build();
694+
ResponseEntity<OpenAiFileApi.FileObjectResponse> listResponse = openAiFileApi.listFiles(listRequest);
695+
696+
// Retrieve file information
697+
ResponseEntity<OpenAiFileApi.FileObject> fileInfo = openAiFileApi.retrieveFile("file-id");
698+
699+
// Delete a file
700+
ResponseEntity<OpenAiFileApi.DeleteFileResponse> deleteResponse = openAiFileApi.deleteFile("file-id");
701+
702+
// Retrieve file content
703+
ResponseEntity<String> fileContent = openAiFileApi.retrieveFileContent("file-id");
704+
----
705+
706+
=== Low-level File API Examples
707+
* The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiFileApiIT.java[OpenAiFileApiIT.java] tests provide some general examples of how to use the lightweight file api library.
708+
669709
== API Key Management
670710

671711
Spring AI provides flexible API key management through the `ApiKey` interface and its implementations. The default implementation, `SimpleApiKey`, is suitable for most use cases, but you can also create custom implementations for more complex scenarios.

0 commit comments

Comments
 (0)