Skip to content

Commit

Permalink
Checkstyle changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaland committed Nov 12, 2024
1 parent 6303fe3 commit 3e03ba1
Show file tree
Hide file tree
Showing 55 changed files with 546 additions and 274 deletions.
7 changes: 7 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC "-//Puppy Crawl//DTD Suppressions 1.1//EN" "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<suppress checks=".*" files=".*Response.java"/>
<suppress checks=".*" files=".*Request.java"/>
<suppress checks=".*" files=".*Entity.java"/>
</suppressions>
3 changes: 1 addition & 2 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
-->

<module name="Checker">

<property name="charset" value="UTF-8"/>

<property name="severity" value="${org.checkstyle.google.severity}" default="warning"/>
Expand Down Expand Up @@ -54,7 +53,7 @@

<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="100"/>
<property name="max" value="120"/>
<property name="ignorePattern"
value="^package.*|^import.*|href\s*=\s*&quot;[^&quot;]*&quot;|http://|https://|ftp://"/>
</module>
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
<version>${checkstyle-maven-plugin.version}</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<suppressionsLocation>suppressions.xml</suppressionsLocation>
</configuration>
<executions>
<execution>
Expand All @@ -265,6 +266,7 @@
<version>${maven-checkstyle-plugin.version}</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<suppressionsLocation>suppressions.xml</suppressionsLocation>
</configuration>
</plugin>
</plugins>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/redhat/composer/api/AssistantAdminApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

import com.redhat.composer.model.mongo.AssistantEntity;
import com.redhat.composer.model.mongo.LLMConnectionEntity;
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;
Expand Down Expand Up @@ -34,7 +34,7 @@ public class AssistantAdminApi {
*/
@POST
@Path("llm")
public LLMConnectionEntity createLlm(LLMRequest request) {
public LlmConnectionEntity createLlm(LLMRequest request) {
return assistantService.createLLMConnection(request);
}

Expand All @@ -44,7 +44,7 @@ public LLMConnectionEntity createLlm(LLMRequest request) {
*/
@GET
@Path("llm")
public List<LLMConnectionEntity> getLlms() {
public List<LlmConnectionEntity> getLlms() {
return assistantService.getLLMConnections();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@
import jakarta.ws.rs.core.MediaType;

/**
* Api For Testing Embedding
* Api For Testing the LLM.
*/
@Path("/llm")
public class LlmAPI {
public class LlmApi {

Logger log = Logger.getLogger(LlmAPI.class);
Logger log = Logger.getLogger(LlmApi.class);

@Inject
SynchronousModelFactory synchronousModelFactory;

@Inject
StreamingModelFactory streamingModelFactory;

/**
* Generate a response for a message.
* @param request the LLMRequest
* @param message message from LLM
* @return the response
*/
@POST
@Path("/generate")
@Authenticated
Expand All @@ -48,6 +54,12 @@ public String syncRequest(LLMRequest request, @QueryParam("message") String mess
return llm.generate(message);
}

/**
* Generate a response for a message.
* @param request the LLMRequest
* @param message streaming message from LLM
* @return the response
*/
@POST
@Path("/generate/streaming")
public Multi<String> streamingRequest(LLMRequest request, @QueryParam("message") String message) {
Expand All @@ -64,27 +76,6 @@ public Multi<String> streamingRequest(LLMRequest request, @QueryParam("message")
return response;
}


// Don't think we want to use this.
@POST
@Produces(MediaType.SERVER_SENT_EVENTS)
@RestStreamElementType("text/plain")
@Path("/generate/streaming/sse")
public Multi<String> streamingRequestSSE(LLMRequest request, @QueryParam("message") String message) {
log.info("Generating response for message: " + message);
if (request == null) {
request = new LLMRequest();
}
StreamingBaseModel model = streamingModelFactory.getModel(request.getModelType());

StreamingChatLanguageModel llm = model.getChatModel(request);
Assistant assistant = AiServices.create(Assistant.class, llm);

Multi<String> response = assistant.chat(message);
return response;
}


interface Assistant {
Multi<String> chat(String message);
}
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/com/redhat/composer/api/VectorRetriverAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import jakarta.ws.rs.QueryParam;

/**
* Api For Testing Store Retrievers
* Api For Testing Store Retrievers.
*/
@Path("/retriver")
@Authenticated
Expand All @@ -28,21 +28,25 @@ public class VectorRetriverAPI {
@Inject
MapperUtil mapperUtil;

/**
* Retrieve sources.
* @param request the RetrieverRequest
* @param message the message
* @return the list of SourceResponse
*/
@POST
@Path("/sources")
public List<SourceResponse> retrieveSources(RetrieverRequest request, @QueryParam("message") String message) {
return retrieveService.retrieveContent(request, message).stream().map(VectorRetriverAPI::toSourceResponse).toList();
}

@POST
@Path("/sources/metadata")
public List<Map<String,Object>> retrieveSourceMetadata(RetrieverRequest request, @QueryParam("message") String message) {
return retrieveService.retrieveContent(request, message)
.stream()
.map(content -> content.textSegment().metadata().toMap()) // Accessing metadata inside textSegment
.toList();
public List<SourceResponse> retrieveSources(RetrieverRequest request,
@QueryParam("message") String message) {
return retrieveService.retrieveContent(request, message).stream()
.map(VectorRetriverAPI::toSourceResponse).toList();
}

/**
* Retrieve sources.
* @param content the Content returned from the retriever
* @return source info
*/
public static SourceResponse toSourceResponse(Content content) {
SourceResponse response = new SourceResponse();
response.setContent(content.textSegment().text());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

import jakarta.enterprise.context.ApplicationScoped;

/**
* Factory for AI Services.
*/
@ApplicationScoped
public class AIServicesFactory {
public class AiServicesFactory {

public static final String MISTRAL7B_AI_SERVICE = "mistral7b";

public static final String MISTRAL7B_QUARKUS_AI_SERVICE = "mistral7b_quarkus";

public static final String HEALTHCARE_SERVICE = "healthcare";

public Class<? extends BaseAIService> getAiService(String aiServiceType) {
/**
* Get the AI service class.
* @param aiServiceType the AI service type
* @return the AI service class
*/
public Class<? extends BaseAiService> getAiService(String aiServiceType) {
switch (aiServiceType) {
case MISTRAL7B_AI_SERVICE:
return Mistral7BAiService.class;
return Mistral7bAiService.class;
case HEALTHCARE_SERVICE:
return HealthCareService.class;
default:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.redhat.composer.config.llm.aiservices;

import dev.langchain4j.service.TokenStream;
import io.smallrye.mutiny.Multi;

/**
* Base AI Service Interface.
*/
public interface BaseAiService{

/**
* Returns TokenStream given input.
* @param context Context information such as chat history and source information
* @param input User Message
* @return the TokenStream
*/
TokenStream chatToken(String context, String input);

/**
* Returns a Multi of String given input.
* @param context Context information such as chat history and source information
* @param input User Message
* @return the Multi of String
*/
Multi<String> chatStream(String context, String input);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* Mistral7BAiService
*/

public interface HealthCareService extends BaseAIService {
public interface HealthCareService extends BaseAiService {

final static String systemMessage = """
static final String systemMessage = """
You are a helpful, respectful and honest assistant answering questions about healthcare.
""";
""";

final static String userMessage ="""
static final String userMessage = """
<context>
{context}
</context>
Expand All @@ -24,16 +24,22 @@ public interface HealthCareService extends BaseAIService {
Question: {input}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
""";

@SystemMessage(systemMessage)
@UserMessage(userMessage)
TokenStream chatToken(String context, String input);



@SystemMessage(systemMessage)
@UserMessage(userMessage)
Multi<String> chatStream(String context, String input);
""";

/**
* Returns TokenStream given input.
* @param context Context information such as chat history and source information
*/
@SystemMessage(systemMessage)
@UserMessage(userMessage)
TokenStream chatToken(String context, String input);


/**
* Returns a Multi of String given input.
*/
@SystemMessage(systemMessage)
@UserMessage(userMessage)
Multi<String> chatStream(String context, String input);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import dev.langchain4j.service.UserMessage;
import io.smallrye.mutiny.Multi;


/**
* Mistral7BAiService
* Mistral7BAiService.
*/
@SuppressWarnings("LineLengthCheck")
public interface Mistral7bAiService extends BaseAiService {

public interface Mistral7BAiService extends BaseAIService {

final static String systemMessage = """
static final String systemMessage = """
You are a helpful, respectful and honest assistant answering questions about products from the company called Red Hat.
You will be given a question you need to answer about this product.
If a question is about a specific product you will be given the product name and version, and references to provide you with additional information.
Expand All @@ -21,9 +22,9 @@ public interface Mistral7BAiService extends BaseAIService {
Please ensure that your responses are socially unbiased and positive in nature.
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct.
If you don't know the answer to a question, please don't share false information.
""";
""";

final static String userMessage ="""
static final String userMessage = """
<context>
{context}
</context>
Expand All @@ -32,16 +33,27 @@ public interface Mistral7BAiService extends BaseAIService {
Question: {input}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
""";

@SystemMessage(systemMessage)
@UserMessage(userMessage)
TokenStream chatToken(String context, String input);



@SystemMessage(systemMessage)
@UserMessage(userMessage)
Multi<String> chatStream(String context, String input);
""";

/**
* Returns TokenStream given input.
* @param context Context information such as chat history and source information
* @param input User Message
* @return the TokenStream
*/
@SystemMessage(systemMessage)
@UserMessage(userMessage)
TokenStream chatToken(String context, String input);


/**
* Returns a Multi of String given input.
* @param context Context information such as chat history and source information
* @param input User Message
* @return the Multi of String
*/
@SystemMessage(systemMessage)
@UserMessage(userMessage)
Multi<String> chatStream(String context, String input);

}
Loading

0 comments on commit 3e03ba1

Please sign in to comment.