Skip to content

Commit

Permalink
Admin assistant updates (#14)
Browse files Browse the repository at this point in the history
* partial change

* finished changes to Update, Get one, Delete endpoints

* fix chwckstyle

* fix mapstruct issue

* simple restassured tests; they need more edge cases added

* Removing mongo constraints in code

---------

Co-authored-by: Javier Ochoa <javo@redhat.com>
  • Loading branch information
javo8a and Javier Ochoa authored Dec 6, 2024
1 parent 08a15ff commit b938aef
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 63 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
Expand Down Expand Up @@ -92,10 +96,6 @@
<artifactId>quarkus-langchain4j-hugging-face</artifactId>
<version>${quarkus.langchain4j.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
Expand Down Expand Up @@ -129,15 +129,15 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
Expand Down
110 changes: 95 additions & 15 deletions src/main/java/com/redhat/composer/api/AssistantAdminApi.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.redhat.composer.api;

import java.util.List;

import com.redhat.composer.model.mongo.AssistantEntity;
import com.redhat.composer.model.mongo.LlmConnectionEntity;
import com.redhat.composer.model.mongo.RetrieverConnectionEntity;
import com.redhat.composer.model.request.AssistantCreationRequest;
import com.redhat.composer.model.request.LLMRequest;
import com.redhat.composer.model.request.RetrieverRequest;
import com.redhat.composer.model.response.AssistantResponse;
import com.redhat.composer.services.AssistantInfoService;

import jakarta.inject.Inject;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import org.bson.types.ObjectId;

import java.util.List;

/**
* Admin API for Creating and Managing Assistants.
Expand All @@ -27,17 +28,43 @@ public class AssistantAdminApi {

/**
* Create a LLM Connection.
* @param request the LLMRequest
*
* @param entity the LLMRequest
* @return the LLMConnectionEntity
*/
@POST
@Path("llm")
public LlmConnectionEntity createLlm(LLMRequest request) {
return assistantService.createLlmConnection(request);
public LlmConnectionEntity createUpdateLlm(LlmConnectionEntity entity) {
return assistantService.createUpdateLlmConnection(entity);
}


/**
* Delete an LLM Connection.
*
* @param llmConnectionObjectIdHexString the llmConnectionId
*/
@DELETE
@Path("llm/{llmConnectionObjectIdHexString}")
public void deleteLlm(@PathParam("llmConnectionObjectIdHexString") String llmConnectionObjectIdHexString) {
assistantService.deleteLlmConnection(new ObjectId(llmConnectionObjectIdHexString));
}

/**
* Get single LLM Connections.
*
* @return the list of LLMConnectionEntity
*/
@GET
@Path("llm/{llmConnectionObjectIdHexString}")
public LlmConnectionEntity getLlm(
@PathParam("llmConnectionObjectIdHexString") String llmConnectionObjectIdHexString) {
return assistantService.getLlmConnection(new ObjectId(llmConnectionObjectIdHexString));
}

/**
* Get all LLM Connections.
*
* @return the list of LLMConnectionEntity
*/
@GET
Expand All @@ -48,42 +75,95 @@ public List<LlmConnectionEntity> getLlms() {

/**
* Create a Retriever Connection.
*
* @param request the RetrieverRequest
* @return the RetrieverConnectionEntity
*/
@POST
@Path("retrieverConnection")
public RetrieverConnectionEntity createRetrieverConnection(RetrieverRequest request) {
return assistantService.createRetrieverConnectionEntity(request);
public RetrieverConnectionEntity createUpdateRetrieverConnection(RetrieverRequest request) {
return assistantService.createUpdateRetrieverConnectionEntity(request);
}

/**
* Get all Retriever Connections.
*
* @return the list of RetrieverConnectionEntity
*/
@GET
@Path("retrieverConnection")
public List<RetrieverConnectionEntity> getRetrieverConnection() {
public List<RetrieverConnectionEntity> getRetrieverConnections() {
return assistantService.getRetrieverConnections();
}

/**
* Delete a Retriever Connection.
*
* @param retrieverConnectionObjectIdHexString the retrieverConnectionObjectId
*/
@DELETE
@Path("retrieverConnection/{retrieverConnectionObjectIdHexString}")
public void deleteRetrieverConnection(
@PathParam("retrieverConnectionObjectIdHexString") String retrieverConnectionObjectIdHexString) {
assistantService.deleteRetrieverConnection(new ObjectId(retrieverConnectionObjectIdHexString));
}

/**
* Get single LLM Connections.
*
* @return the list of LLMConnectionEntity
*/
@GET
@Path("retrieverConnection/{retrieverConnectionObjectIdHexString}")
public RetrieverConnectionEntity getRetrieverConnection(
@PathParam("retrieverConnectionObjectIdHexString") String retrieverConnectionObjectIdHexString) {
return assistantService.getRetrieverConnection(new ObjectId(retrieverConnectionObjectIdHexString));
}

/**
* Create an Assistant.
*
* @param request the AssistantCreationRequest
* @return the AssistantEntity
*/
@POST
public AssistantEntity createAssistant(AssistantCreationRequest request) {
return assistantService.createAssistant(request);
public AssistantResponse createAssistant(AssistantCreationRequest request) {
return assistantService.createUpdateAssistant(request);
}

/**
* Get all Assistants.
*
* @return the list of AssistantResponse
*/
@GET
public List<AssistantResponse> getAssistant() {
return assistantService.getAssistant();
public List<AssistantResponse> getAssistants() {
return assistantService.getAssistants();
}


/**
* Delete a Assistant.
*
* @param assistantObjectIdHexString the retrieverConnectionObjectId
*/
@DELETE
@Path("/{assistantObjectIdHexString}")
public void deleteAssistant(
@PathParam("assistantObjectIdHexString") String assistantObjectIdHexString) {
assistantService.deleteAssistant(new ObjectId(assistantObjectIdHexString));
}

/**
* Get single LLM Connections.
*
* @return the list of LLMConnectionEntity
*/
@GET
@Path("/{assistantObjectIdHexString}")
public AssistantEntity getAssistant(
@PathParam("assistantObjectIdHexString") String assistantObjectIdHexString) {
return assistantService.getAssistant(new ObjectId(assistantObjectIdHexString));
}


}
47 changes: 47 additions & 0 deletions src/main/java/com/redhat/composer/api/exception/ErrorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.redhat.composer.api.exception;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.jboss.logging.Logger;

/**
* Maps exceptions to error responses.
*/
@Provider
public class ErrorMapper implements ExceptionMapper<Exception> {

Logger log = Logger.getLogger(ErrorMapper.class);

@Inject
ObjectMapper objectMapper;

@Override
public Response toResponse(Exception exception) {
log.error("Failed to handle request", exception);

int code = 500;
if (exception instanceof WebApplicationException) {
code = ((WebApplicationException) exception).getResponse().getStatus();
} else if (exception instanceof IllegalArgumentException) {
code = 400; //bad-request
}

ObjectNode exceptionJson = objectMapper.createObjectNode();
exceptionJson.put("code", code);

if (exception.getMessage() != null) {
exceptionJson.put("error", exception.getMessage());
}

return Response.status(code)
.entity(exceptionJson)
.build();
}

}
8 changes: 3 additions & 5 deletions src/main/java/com/redhat/composer/model/mongo/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.redhat.composer.model.mongo;

import org.bson.types.ObjectId;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

import io.quarkus.mongodb.panache.PanacheMongoEntity;
import io.quarkus.mongodb.panache.common.jackson.ObjectIdDeserializer;
import org.bson.types.ObjectId;

/**
* BaseEntity.
*/
public class BaseEntity extends PanacheMongoEntity {


@JsonDeserialize(using = StringDeserializer.class)
@JsonDeserialize(using = ObjectIdDeserializer.class)
public ObjectId id;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@SuppressWarnings("all")
public class AssistantCreationRequest {

String id;
String name;
String displayName;
String description;
Expand All @@ -29,6 +30,14 @@ public AssistantCreationRequest(String name, String displayName, String descript
this.retrieverConnectionId = retrieverConnectionId;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return this.name;
}
Expand Down Expand Up @@ -87,6 +96,11 @@ public void setRetrieverConnectionId(String retrieverConnectionId) {
}


public AssistantCreationRequest id(String id) {
setId(id);
return this;
}

public AssistantCreationRequest name(String name) {
setName(name);
return this;
Expand Down Expand Up @@ -135,6 +149,7 @@ public int hashCode() {
@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
" name='" + getName() + "'" +
", displayName='" + getDisplayName() + "'" +
", description='" + getDescription() + "'" +
Expand Down
Loading

0 comments on commit b938aef

Please sign in to comment.