-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin of OpenAI compatible endpoints
- Loading branch information
Showing
9 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
backend/src/main/java/ai/dragon/controller/api/ragapi/OpenAiCompatibleV1ApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ai.dragon.controller.api.ragapi; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import ai.dragon.dto.openai.completion.OpenAiChatCompletionChoice; | ||
import ai.dragon.dto.openai.completion.OpenAiChatCompletionRequest; | ||
import ai.dragon.dto.openai.completion.OpenAiChatCompletionResponse; | ||
import ai.dragon.dto.openai.completion.OpenAiCompletionChoice; | ||
import ai.dragon.dto.openai.completion.OpenAiCompletionRequest; | ||
import ai.dragon.dto.openai.completion.OpenAiCompletionResponse; | ||
import ai.dragon.dto.openai.completion.OpenAiCompletionUsage; | ||
import ai.dragon.dto.openai.completion.OpenAiCompletionMessage; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping("/api/ragapi/v1") | ||
@Tag(name = "Open AI Compatible", description = "Compatible Endpoints following Open AI API Format") | ||
public class OpenAiCompatibleV1ApiController { | ||
@PostMapping("/completions") | ||
@Operation(summary = "Creates a completion", description = "Creates a completion for the provided prompt and parameters.") | ||
public OpenAiCompletionResponse completions(@Valid @RequestBody OpenAiCompletionRequest request) throws Exception { | ||
OpenAiCompletionResponse response = new OpenAiCompletionResponse(); | ||
|
||
response.setId(UUID.randomUUID().toString()); | ||
response.setModel(request.getModel()); | ||
response.setCreated(System.currentTimeMillis() / 1000); | ||
response.setObject("text_completion"); | ||
response.setUsage(OpenAiCompletionUsage | ||
.builder() | ||
.completion_tokens(0) | ||
.prompt_tokens(0) | ||
.total_tokens(0) | ||
.build()); | ||
|
||
List<OpenAiCompletionChoice> choices = new ArrayList<>(); | ||
choices.add(OpenAiCompletionChoice | ||
.builder() | ||
.index(0) | ||
.finish_reason("stop") | ||
.text("Hello, how can I help you today?") | ||
.build()); | ||
|
||
response.setChoices(choices); | ||
|
||
return response; | ||
} | ||
|
||
@PostMapping("/chat/completions") | ||
@Operation(summary = "Creates a chat completion", description = "Creates a chat completion for the provided prompt and parameters.") | ||
public OpenAiChatCompletionResponse chatCompletions(@Valid @RequestBody OpenAiChatCompletionRequest request) | ||
throws Exception { | ||
OpenAiChatCompletionResponse response = new OpenAiChatCompletionResponse(); | ||
|
||
response.setId(UUID.randomUUID().toString()); | ||
response.setModel(request.getModel()); | ||
response.setCreated(System.currentTimeMillis() / 1000); | ||
response.setObject("chat.completion"); | ||
response.setUsage(OpenAiCompletionUsage | ||
.builder() | ||
.completion_tokens(0) | ||
.prompt_tokens(0) | ||
.total_tokens(0) | ||
.build()); | ||
|
||
List<OpenAiChatCompletionChoice> choices = new ArrayList<>(); | ||
choices.add(OpenAiChatCompletionChoice | ||
.builder() | ||
.index(0) | ||
.finish_reason("stop") | ||
.message(OpenAiCompletionMessage | ||
.builder() | ||
.role("assistant") | ||
.content("Hello, how can I help you today?") | ||
.build()) | ||
.build()); | ||
|
||
response.setChoices(choices); | ||
|
||
return response; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiChatCompletionChoice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class OpenAiChatCompletionChoice { | ||
private String finish_reason; | ||
private Integer index; | ||
private OpenAiCompletionMessage message; | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiChatCompletionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenAiChatCompletionRequest { | ||
@NotBlank | ||
@NotNull | ||
private String model; | ||
|
||
@NotEmpty | ||
@NotNull | ||
private List<OpenAiCompletionMessage> messages; | ||
|
||
private Integer max_tokens; | ||
private Boolean stream; | ||
private Double temperature; | ||
private String user; | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiChatCompletionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenAiChatCompletionResponse { | ||
private String id; | ||
private String object; | ||
private Long created; | ||
private String model; | ||
private List<OpenAiChatCompletionChoice> choices; | ||
private OpenAiCompletionUsage usage; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiCompletionChoice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class OpenAiCompletionChoice { | ||
private String finish_reason; | ||
private Integer index; | ||
private String text; | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiCompletionMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class OpenAiCompletionMessage { | ||
@NotNull | ||
@NotBlank | ||
private String role; | ||
|
||
@NotNull | ||
@NotBlank | ||
private String content; | ||
|
||
private String name; | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiCompletionRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenAiCompletionRequest { | ||
@NotBlank | ||
@NotNull | ||
private String model; | ||
|
||
@NotNull | ||
private Object prompt; | ||
|
||
private Integer max_tokens; | ||
private Boolean stream; | ||
private Double temperature; | ||
private String user; | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiCompletionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenAiCompletionResponse { | ||
private String id; | ||
private String object; | ||
private Long created; | ||
private String model; | ||
private List<OpenAiCompletionChoice> choices; | ||
private OpenAiCompletionUsage usage; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/ai/dragon/dto/openai/completion/OpenAiCompletionUsage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package ai.dragon.dto.openai.completion; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class OpenAiCompletionUsage { | ||
private Integer completion_tokens; | ||
private Integer prompt_tokens; | ||
private Integer total_tokens; | ||
} |