View Javadoc
1   package fr.tiogars.chat.domains.prompt.services;
2   
3   import fr.tiogars.chat.domains.prompt.models.PromptRequest;
4   import fr.tiogars.chat.domains.prompt.models.PromptResponse;
5   import org.springframework.stereotype.Service;
6   
7   import java.time.Duration;
8   import java.time.Instant;
9   
10  /**
11   * Service for handling AI prompts.
12   * This service uses the ChatClientAdapter to communicate with AI models.
13   */
14  @Service
15  public class PromptService {
16  
17      private final ChatClientAdapter chatClient;
18  
19      public PromptService(ChatClientAdapter chatClient) {
20          this.chatClient = chatClient;
21      }
22  
23      /**
24       * Process a prompt request and return the AI response.
25       *
26       * @param request the prompt request
27       * @return the AI response
28       */
29      public PromptResponse processPrompt(PromptRequest request) {
30          Instant start = Instant.now();
31  
32          String response;
33          if (request.systemPrompt() != null && !request.systemPrompt().isBlank()) {
34              response = chatClient.call(request.systemPrompt(), request.prompt());
35          } else {
36              response = chatClient.call(request.prompt());
37          }
38  
39          Instant end = Instant.now();
40          long processingTimeMs = Duration.between(start, end).toMillis();
41  
42          return new PromptResponse(
43                  response,
44                  request.model() != null ? request.model() : "unknown",
45                  response.length(),
46                  processingTimeMs,
47                  "stop"
48          );
49      }
50  }