|
1 | 1 | package com.sap.ai.sdk.app.controllers; |
2 | 2 |
|
| 3 | +import com.sap.ai.sdk.foundationmodels.openai.OpenAiClient; |
| 4 | +import com.sap.ai.sdk.foundationmodels.openai.OpenAiModel; |
| 5 | +import com.sap.ai.sdk.foundationmodels.openai.spring.OpenAiChatModel; |
3 | 6 | import com.sap.ai.sdk.prompt.registry.PromptClient; |
4 | 7 | import com.sap.ai.sdk.prompt.registry.model.PromptTemplateDeleteResponse; |
5 | 8 | import com.sap.ai.sdk.prompt.registry.model.PromptTemplateListResponse; |
|
9 | 12 | import com.sap.ai.sdk.prompt.registry.model.PromptTemplateSubstitutionRequest; |
10 | 13 | import com.sap.ai.sdk.prompt.registry.model.PromptTemplateSubstitutionResponse; |
11 | 14 | import com.sap.ai.sdk.prompt.registry.model.SingleChatTemplate; |
| 15 | +import com.sap.ai.sdk.prompt.registry.spring.SpringAiConverter; |
12 | 16 | import java.io.File; |
13 | 17 | import java.io.IOException; |
14 | 18 | import java.util.List; |
15 | 19 | import java.util.Map; |
| 20 | +import lombok.val; |
| 21 | +import org.springframework.ai.chat.client.ChatClient; |
| 22 | +import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor; |
| 23 | +import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository; |
| 24 | +import org.springframework.ai.chat.memory.MessageWindowChatMemory; |
| 25 | +import org.springframework.ai.chat.messages.Message; |
| 26 | +import org.springframework.ai.chat.model.Generation; |
| 27 | +import org.springframework.ai.chat.prompt.Prompt; |
16 | 28 | import org.springframework.core.io.ClassPathResource; |
17 | 29 | import org.springframework.core.io.Resource; |
18 | 30 | import org.springframework.web.bind.annotation.GetMapping; |
@@ -99,4 +111,29 @@ List<PromptTemplateDeleteResponse> deleteTemplate() { |
99 | 111 | .map(template -> client.deletePromptTemplate(template.getId())) |
100 | 112 | .toList(); |
101 | 113 | } |
| 114 | + |
| 115 | + @GetMapping("/promptRegistryToSpringAi") |
| 116 | + Generation promptRegistryToSpringAi() { |
| 117 | + val openAiClient = new OpenAiChatModel(OpenAiClient.forModel(OpenAiModel.GPT_4O_MINI)); |
| 118 | + val repository = new InMemoryChatMemoryRepository(); |
| 119 | + val memory = MessageWindowChatMemory.builder().chatMemoryRepository(repository).build(); |
| 120 | + val advisor = MessageChatMemoryAdvisor.builder(memory).build(); |
| 121 | + val cl = ChatClient.builder(openAiClient).defaultAdvisors(advisor).build(); |
| 122 | + |
| 123 | + val promptResponse = |
| 124 | + new PromptClient() |
| 125 | + .parsePromptTemplateByNameVersion( |
| 126 | + "categorization", |
| 127 | + "0.0.1", |
| 128 | + "java-e2e-test", |
| 129 | + "default", |
| 130 | + false, |
| 131 | + PromptTemplateSubstitutionRequest.create() |
| 132 | + .inputParams(Map.of("inputExample", "I love football"))); |
| 133 | + |
| 134 | + final List<Message> messages = SpringAiConverter.promptTemplateToMessages(promptResponse); |
| 135 | + val prompt = new Prompt(messages); |
| 136 | + val response = cl.prompt(prompt).call().chatResponse(); |
| 137 | + return response != null ? response.getResult() : null; |
| 138 | + } |
102 | 139 | } |
0 commit comments