18
18
import com .developer .nefarious .zjoule .plugin .memory .IMemoryObject ;
19
19
import com .developer .nefarious .zjoule .plugin .models .Role ;
20
20
21
+ /**
22
+ * A client implementation for interacting with the Ollama AI chat system.
23
+ * <p>
24
+ * The {@code OllamaClient} facilitates chat interactions by sending requests
25
+ * to an Ollama chat API endpoint and processing responses. It also manages
26
+ * message history using an in-memory storage system.
27
+ * </p>
28
+ */
21
29
public class OllamaClient implements IAIClient {
22
-
23
- private HttpClient httpClient ;
24
-
25
- private IMemoryMessageHistory memoryMessageHistory ;
26
-
27
- private IMemoryObject <String > memoryOllamaEndpoint ;
28
-
29
- private IOllamaClientHelper helper ;
30
-
31
- public OllamaClient (
32
- final IMemoryMessageHistory memoryMessageHistory ,
33
- final IMemoryObject <String > memoryOllamaEndpoint ,
34
- final IOllamaClientHelper ollamaClientHelper ) {
35
- this .httpClient = HttpClient .newHttpClient ();
36
- this .memoryMessageHistory = memoryMessageHistory ;
37
- this .memoryOllamaEndpoint = memoryOllamaEndpoint ;
38
- helper = ollamaClientHelper ;
39
- }
40
-
41
- @ Override
42
- public IChatMessage chatCompletion (final List <IChatMessage > messages ) throws IOException , InterruptedException {
43
- URI endpoint = createChatEndpoint ();
44
-
45
- BodyPublisher requestBody = helper .createRequestBody (messages );
46
-
30
+
31
+ /** The HTTP client used for sending chat requests. */
32
+ private HttpClient httpClient ;
33
+
34
+ /** Memory storage for maintaining chat message history. */
35
+ private IMemoryMessageHistory memoryMessageHistory ;
36
+
37
+ /** Memory storage for retrieving the Ollama chat API endpoint. */
38
+ private IMemoryObject <String > memoryOllamaEndpoint ;
39
+
40
+ /** Helper class for constructing request payloads and processing responses. */
41
+ private IOllamaClientHelper helper ;
42
+
43
+ /**
44
+ * Constructs an {@code OllamaClient} with the specified dependencies.
45
+ *
46
+ * @param memoryMessageHistory the in-memory storage for chat message history.
47
+ * @param memoryOllamaEndpoint the in-memory storage containing the Ollama chat API endpoint.
48
+ * @param ollamaClientHelper the helper class for handling request and response transformations.
49
+ */
50
+ public OllamaClient (
51
+ final IMemoryMessageHistory memoryMessageHistory ,
52
+ final IMemoryObject <String > memoryOllamaEndpoint ,
53
+ final IOllamaClientHelper ollamaClientHelper ) {
54
+ this .httpClient = HttpClient .newHttpClient ();
55
+ this .memoryMessageHistory = memoryMessageHistory ;
56
+ this .memoryOllamaEndpoint = memoryOllamaEndpoint ;
57
+ helper = ollamaClientHelper ;
58
+ }
59
+
60
+ /**
61
+ * Sends a chat completion request to the Ollama API.
62
+ * <p>
63
+ * This method constructs a request using the provided chat messages and sends
64
+ * it to the configured API endpoint. It then processes the response and returns
65
+ * the generated AI message.
66
+ * </p>
67
+ *
68
+ * @param messages the list of chat messages forming the conversation history.
69
+ * @return the AI-generated response as an {@link IChatMessage}.
70
+ * @throws IOException if an I/O error occurs when sending the request.
71
+ * @throws InterruptedException if the request is interrupted while waiting for a response.
72
+ */
73
+ @ Override
74
+ public IChatMessage chatCompletion (final List <IChatMessage > messages ) throws IOException , InterruptedException {
75
+ URI endpoint = createChatEndpoint ();
76
+ BodyPublisher requestBody = helper .createRequestBody (messages );
77
+
47
78
HttpRequest request = HttpRequest .newBuilder ()
48
- .uri (endpoint )
49
- .POST (requestBody )
50
- .build ();
51
-
79
+ .uri (endpoint )
80
+ .POST (requestBody )
81
+ .build ();
82
+
52
83
HttpResponse <String > response = httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
53
-
84
+
54
85
return helper .convertResponseToObject (response .body ());
55
- }
86
+ }
56
87
57
- @ Override
58
- public IChatMessage createMessage (final Role role , final String userPrompt ) {
59
- return new OllamaChatMessage (role , userPrompt );
60
- }
88
+ /**
89
+ * Creates a new chat message with the specified role and user input.
90
+ *
91
+ * @param role the role of the message (e.g., user or assistant).
92
+ * @param userPrompt the message content.
93
+ * @return an instance of {@link IChatMessage} representing the user input.
94
+ */
95
+ @ Override
96
+ public IChatMessage createMessage (final Role role , final String userPrompt ) {
97
+ return new OllamaChatMessage (role , userPrompt );
98
+ }
61
99
62
- @ Override
63
- public List <IChatMessage > getMessageHistory () {
64
- MessageHistory messageHistory = memoryMessageHistory .load ();
100
+ /**
101
+ * Retrieves the chat message history stored in memory.
102
+ *
103
+ * @return a list of {@link IChatMessage} representing the chat history,
104
+ * or an empty list if no history is available.
105
+ */
106
+ @ Override
107
+ public List <IChatMessage > getMessageHistory () {
108
+ MessageHistory messageHistory = memoryMessageHistory .load ();
65
109
if (messageHistory == null ) {
66
110
return Collections .emptyList ();
67
111
}
@@ -72,23 +116,32 @@ public List<IChatMessage> getMessageHistory() {
72
116
}
73
117
74
118
return messages .stream ().map (message ->
75
- new OllamaChatMessage (message .getRole (), message .getContent ()))
119
+ new OllamaChatMessage (message .getRole (), message .getContent ()))
76
120
.collect (Collectors .toList ());
77
- }
121
+ }
78
122
79
- @ Override
80
- public void setMessageHistory (final List <IChatMessage > chatMessages ) {
81
- MessageHistory newMessageHistory = new MessageHistory ();
123
+ /**
124
+ * Stores the given chat message history in memory.
125
+ *
126
+ * @param chatMessages the list of chat messages to save.
127
+ */
128
+ @ Override
129
+ public void setMessageHistory (final List <IChatMessage > chatMessages ) {
130
+ MessageHistory newMessageHistory = new MessageHistory ();
82
131
newMessageHistory .setMessages (chatMessages .stream ().map (
83
- chatMessage -> new Message (chatMessage .getRole (), chatMessage .getMessage ()))
84
- .collect (Collectors .toList ()));
132
+ chatMessage -> new Message (chatMessage .getRole (), chatMessage .getMessage ()))
133
+ .collect (Collectors .toList ()));
85
134
memoryMessageHistory .save (newMessageHistory );
86
- }
87
-
88
- private URI createChatEndpoint () {
135
+ }
136
+
137
+ /**
138
+ * Creates a URI representing the chat endpoint for sending requests.
139
+ *
140
+ * @return the {@link URI} of the chat API endpoint.
141
+ */
142
+ private URI createChatEndpoint () {
89
143
String endpoint = memoryOllamaEndpoint .load ();
90
144
String endpointInStringFormat = endpoint + "/api/chat" ;
91
145
return URI .create (endpointInStringFormat );
92
146
}
93
-
94
- }
147
+ }
0 commit comments