Skip to content

Commit

Permalink
feat: try to introduce OpenAI v1 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Sep 22, 2024
1 parent 52bf699 commit 4192731
Show file tree
Hide file tree
Showing 4 changed files with 338 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.util.Objects;
import java.util.UUID;

import com.fasterxml.jackson.core.type.TypeReference;

import de.l3s.interweb.core.util.JsonUtils;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;

Expand All @@ -17,9 +13,11 @@
import org.hibernate.annotations.CreationTimestamp;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;

import de.l3s.interweb.core.chat.Message;
import de.l3s.interweb.core.chat.Role;
import de.l3s.interweb.core.util.JsonUtils;

@Entity
@Cacheable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ChatsResource {
SecurityIdentity securityIdentity;

@GET
public Uni<List<Chat>> chats(
public Uni<List<Chat>> list(
@QueryParam("user") String user,
@QueryParam("sort") @DefaultValue("-created") String order,
@QueryParam("page") @DefaultValue("1") Integer page,
Expand All @@ -41,7 +41,7 @@ public Uni<List<Chat>> chats(

@GET
@Path("{uuid}")
public Uni<Conversation> chat(@PathParam("uuid") UUID id) {
public Uni<Conversation> get(@PathParam("uuid") UUID id) {
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);

return Chat.findById(apikey, id).call(chat -> Mutiny.fetch(chat.getMessages())).map(chat -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package de.l3s.interweb.server.features.openai;

import de.l3s.interweb.core.ObjectWrapper;
import de.l3s.interweb.core.chat.CompletionsQuery;
import de.l3s.interweb.core.chat.CompletionsResults;
import de.l3s.interweb.core.models.Model;
import de.l3s.interweb.server.Roles;
import de.l3s.interweb.server.features.api.ApiChatRequest;
import de.l3s.interweb.server.features.api.ApiKey;
import de.l3s.interweb.server.features.chat.ChatService;
import de.l3s.interweb.server.features.models.ModelsService;
import io.quarkus.security.identity.SecurityIdentity;
import io.smallrye.mutiny.Uni;
import io.vertx.core.eventbus.EventBus;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Context;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.util.List;

@Tag(name = "OpenAI Compatible API v1")
@Path("/v1")
@RolesAllowed({Roles.APPLICATION})
public class OpenaiV1Resource {

@Inject
EventBus bus;

@Inject
ChatService chatService;

@Context
SecurityIdentity securityIdentity;

@Inject
ModelsService modelsService;

@POST
@Path("/completions")
public Uni<CompletionsResults> completions() {
throw new UnsupportedOperationException("Not implemented yet");
}

@POST
@Path("/chat/completions")
public Uni<CompletionsResults> chatCompletions(@Valid CompletionsQuery query) {
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);
return chatService.completions(query).chain(results -> {
results.setChatId(null); // reset chatId if it was set
bus.send("api-request-chat", ApiChatRequest.of(results, apikey));
return Uni.createFrom().item(results);
});
}

@GET
@Path("/models")
public Uni<ObjectWrapper<List<Model>>> models() {
return modelsService.getModels().map(models -> new ObjectWrapper<>("list", models));
}

@GET
@Path("/models/{model}")
public Uni<Model> chat(@PathParam("model") String model) {
return modelsService.getModel(model);
}

@POST
@Path("/embeddings")
public Uni<CompletionsResults> embeddings() {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Loading

0 comments on commit 4192731

Please sign in to comment.