| 21 | import java.util.Map; |
| 22 | |
| 23 | @Slf4j |
| 24 | public class OpenAIAdapter implements AIModelAdapter { |
| 25 | |
| 26 | private static final String MODEL_ID = "openai"; |
| 27 | private static final String MODEL_NAME = "OpenAI"; |
| 28 | |
| 29 | private final OpenAIConfig config; |
| 30 | private final ChatClient chatClient; |
| 31 | private final EmbeddingModel embeddingModel; |
| 32 | |
| 33 | public OpenAIAdapter(OpenAIConfig config, ChatClient chatClient, EmbeddingModel embeddingModel) { |
| 34 | this.config = config; |
| 35 | this.chatClient = chatClient; |
| 36 | this.embeddingModel = embeddingModel; |
| 37 | log.info("OpenAIAdapter initialized (enabled: {})", config.isEnabled()); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String getModelId() { |
| 42 | return MODEL_ID; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String getModelName() { |
| 47 | return MODEL_NAME; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean isEnabled() { |
| 52 | return config.isEnabled(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public String chat(List<Map<String, String>> messages) { |
| 57 | if (!isEnabled()) { |
| 58 | throw new IllegalStateException("OpenAI model is not enabled"); |
| 59 | } |
| 60 | List<Message> aiMessages = convertMessages(messages); |
| 61 | Prompt prompt = new Prompt(aiMessages); |
| 62 | String response = chatClient.prompt(prompt).call().content(); |
| 63 | log.debug("OpenAI chat completed, response length: {}", response != null ? response.length() : 0); |
| 64 | return response; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Flux<String> stream(List<Map<String, String>> messages) { |
| 69 | if (!isEnabled()) { |
| 70 | return Flux.error(new IllegalStateException("OpenAI model is not enabled")); |
| 71 | } |
| 72 | List<Message> aiMessages = convertMessages(messages); |
| 73 | Prompt prompt = new Prompt(aiMessages); |
| 74 | return chatClient.prompt(prompt).stream().content(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public float[] embed(String text) { |
| 79 | if (!isEnabled()) { |
| 80 | throw new IllegalStateException("OpenAI model is not enabled"); |
nothing calls this directly
no outgoing calls
no test coverage detected