Skip to content

Commit

Permalink
SystemPrompt now optional (required with 01-preview)
Browse files Browse the repository at this point in the history
  • Loading branch information
goulven authored and goulven committed Dec 19, 2024
1 parent f3deaa5 commit a5630b5
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.groovy.parser.antlr4.util.StringUtils;
import org.open4goods.commons.config.yml.GenAiServiceType;
import org.open4goods.commons.config.yml.PromptConfig;
import org.open4goods.commons.config.yml.ui.GenAiConfig;
Expand All @@ -19,6 +20,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.ChatClient.CallResponseSpec;
import org.springframework.ai.chat.client.ChatClient.ChatClientRequestSpec;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.api.OpenAiApi;

Expand Down Expand Up @@ -87,18 +89,24 @@ public GenAiResponse<CallResponseSpec> prompt(String promptKey, Map<String, Obje

ret.setStart(System.currentTimeMillis());
// Evaluating prompts,
String systemPrompt = evaluationService.thymeleafEval(variables, pConf.getSystemPrompt());
String systemPrompt = null == pConf.getSystemPrompt() ? "" : evaluationService.thymeleafEval(variables, pConf.getSystemPrompt());
String userPrompt = evaluationService.thymeleafEval(variables, pConf.getUserPrompt());

// Checking there are no remainings unevaluated expression

// TODO(p2,safety) Detect if remaining variables

CallResponseSpec genAiResponse = ChatClient.create( models.get(promptKey)).prompt()
.system(systemPrompt)
ChatClientRequestSpec chatRequest = ChatClient.create( models.get(promptKey)).prompt()
.user(userPrompt)
.options(pConf.getOptions()).call();

.options(pConf.getOptions());

if (!StringUtils.isEmpty(systemPrompt)) {
chatRequest = chatRequest.system(systemPrompt);
}

CallResponseSpec genAiResponse = chatRequest.call();


ret.setBody(genAiResponse);
ret.setRaw(genAiResponse.content());

Expand All @@ -108,7 +116,6 @@ public GenAiResponse<CallResponseSpec> prompt(String promptKey, Map<String, Obje
updateConfig.setUserPrompt(userPrompt);
ret.setPrompt(updateConfig);


ret.setDuration(System.currentTimeMillis()-ret.getStart());

return ret;
Expand Down Expand Up @@ -136,8 +143,6 @@ public GenAiResponse<Map<String, Object>> jsonPrompt(String promptKey, Map<Strin
ret.setStart(internal.getStart());
ret.setPrompt(internal.getPrompt());



String response = internal.getBody().content();
response = response.replace("```json", "").replace("```", "");
ret.setRaw(response);
Expand Down

0 comments on commit a5630b5

Please sign in to comment.