Skip to content

Commit

Permalink
Begin of OpenAI compatible endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
amengus87 committed Jun 4, 2024
1 parent 6ee068a commit d4ffb0d
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 0 deletions.
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;
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}

0 comments on commit d4ffb0d

Please sign in to comment.