Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
astappiev committed Nov 10, 2024
1 parent b2add58 commit cdc4c02
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public void setPrice(UsagePrice price) {
this.price = price;
}

public boolean isFree() {
return price != null && price.getInput() == 0 && price.getOutput() == 0;
}

public String getProvider() {
return provider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public Uni<SecurityIdentity> authenticate(ApiKeyAuthenticationRequest request, A
.addCredential(key)
.setAnonymous(false)
.addRole(Roles.APPLICATION)
.addPermissionChecker(permission -> {
if (permission.getName().equals("search")) {
return Uni.createFrom().item(key.user.allowSearch);
}
if (permission.getName().equals("evaluate") && permission.getActions().equals("paid")) {
return Uni.createFrom().item(key.user.allowPaidModels);
}
return null;
})
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ public class ChatResource {
@POST
@Path("/completions")
public Uni<CompletionsResults> completions(@Valid CompletionsQuery query) {
User user = (User) securityIdentity.getPrincipal();
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);
return chatService.completions(query, user.allowPaidModels).chain(results -> {
return chatService.completions(query).chain(results -> {
results.setChatId(null); // reset chatId if it was set
bus.send("api-request-chat", ApiChatRequest.of(results, apikey));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.l3s.interweb.server.features.chat;

import io.quarkus.security.PermissionsAllowed;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

Expand All @@ -17,18 +19,38 @@ public class ChatService {
@Inject
ModelsService modelsService;

public Uni<CompletionsResults> completions(CompletionsQuery query, boolean allowPaidModels) {
return modelsService.getModel(query.getModel()).flatMap(model -> {
ModelsConnector connector = modelsService.getConnector(model.getProvider());
if (connector instanceof ChatConnector chatConnector) {
if (model.getPrice().getInput() > 0 && !allowPaidModels) {
return Uni.createFrom().failure(new ConnectorException("Please contact the administrator to enable this model"));
}
return completions(query, model, chatConnector);
public Uni<Model> getModel(CompletionsQuery query) {
return modelsService.getModel(query.getModel());
}

public Uni<CompletionsResults> completions(CompletionsQuery query) {
return modelsService.getModel(query.getModel()).chain(model -> completions(query, model));
}

@PermissionsAllowed("evaluate:free")
public Uni<CompletionsResults> completions(CompletionsQuery query, Model model) {
ModelsConnector connector = modelsService.getConnector(model.getProvider());
if (connector instanceof ChatConnector chatConnector) {
if (model.getPrice().getInput() > 0) {
return Uni.createFrom().failure(new ConnectorException("Please contact the administrator to enable this model"));
}
return completions(query, model, chatConnector);
}

return Uni.createFrom().failure(new ConnectorException("Model `" + query.getModel() + "` is not a chat model"));
}

@PermissionsAllowed("evaluate:paid")
public Uni<CompletionsResults> completionsPaid(CompletionsQuery query, Model model) {
ModelsConnector connector = modelsService.getConnector(model.getProvider());
if (connector instanceof ChatConnector chatConnector) {
if (model.getPrice().getInput() > 0) {
return Uni.createFrom().failure(new ConnectorException("Please contact the administrator to enable this model"));
}
return completions(query, model, chatConnector);
}

return Uni.createFrom().failure(new ConnectorException("Model `" + query.getModel() + "` is not a chat model"));
});
return Uni.createFrom().failure(new ConnectorException("Model `" + query.getModel() + "` is not a chat model"));
}

private Uni<CompletionsResults> completions(CompletionsQuery query, Model model, ChatConnector connector) {
Expand Down Expand Up @@ -59,6 +81,6 @@ public Uni<String> generateTitle(final Chat chat) {
Summarize the conversation in 5 words or less, in a way that sounds like a book title.
Don't use any formatting. You can use emojis. Only print the title, nothing else.
""".formatted(sb), Role.user);
return completions(query, false).map(results -> results.getLastMessage().getContent().trim());
return completions(query).map(results -> results.getLastMessage().getContent().trim());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public Uni<CompletionsResults> completions() {
@POST
@Path("/chat/completions")
public Uni<CompletionsResults> chatCompletions(@Valid CompletionsQuery query) {
User user = (User) securityIdentity.getPrincipal();
ApiKey apikey = securityIdentity.getCredential(ApiKey.class);
return chatService.completions(query, user.allowPaidModels).chain(results -> {
return chatService.getModel(query).chain(model -> {

});
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;
import java.util.UUID;

import io.quarkus.security.PermissionsAllowed;

import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -71,12 +73,8 @@ public Uni<SearchResults> search(@Parameter(description = "The search query", ex
}

@POST
@PermissionsAllowed("search")
public Uni<SearchResults> search(@NotNull @Valid SearchQuery query, @HeaderParam("Cache-Control") String cacheControl) {
final User user = (User) securityIdentity.getPrincipal();
if (!user.allowSearch) {
return Uni.createFrom().failure(new ForbiddenException("Please contact the administrator to enable search"));
}

long start = System.currentTimeMillis();
if (NO_CACHE.equals(cacheControl)) {
query.setIgnoreCache(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class RequestApiKeyAuthTest {

@BeforeEach
public void setup() {
User user = Mockito.mock(User.class);
Mockito.when(user.allowSearch).thenReturn(true);

PanacheMock.mock(ApiKey.class);
ApiKey testKey = Mockito.mock(ApiKey.class);
Mockito.when(testKey.user).thenReturn(Mockito.mock(User.class));
Mockito.when(testKey.user).thenReturn(user);
Mockito.when(ApiKey.findByApikey("testkey")).thenReturn(Uni.createFrom().item(testKey));

SearchQuery searchQuery = new SearchQuery();
Expand Down

0 comments on commit cdc4c02

Please sign in to comment.