Skip to content

Commit 2e27c79

Browse files
authored
Merge pull request #55 from MEITREX/ollama-addition
Ollama Change to DGX
2 parents 4783c5a + c77db6a commit 2e27c79

5 files changed

Lines changed: 121 additions & 84 deletions

File tree

src/main/java/de/unistuttgart/iste/meitrex/common/config/OllamaConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ public class OllamaConfig {
2020
// Should be the name of the folder in which the prompts reside in the resource directory
2121
@Value("${ollama.promptFolder:prompt_templates}")
2222
private String promptFolder;
23+
24+
@Value("${ollama.apiKey:}")
25+
private String apiKey;
2326
}

src/main/java/de/unistuttgart/iste/meitrex/common/ollama/OllamaClient.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
import org.springframework.stereotype.Service;
1010

1111
import javax.annotation.Nullable;
12-
import java.io.*;
12+
import java.io.BufferedReader;
13+
import java.io.FileNotFoundException;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.io.InputStreamReader;
1317
import java.net.URI;
1418
import java.net.http.HttpClient;
1519
import java.net.http.HttpRequest;
1620
import java.net.http.HttpResponse;
1721
import java.nio.charset.StandardCharsets;
22+
import java.time.Duration;
1823
import java.util.HashMap;
1924
import java.util.Map;
2025
import java.util.Optional;
@@ -98,7 +103,7 @@ public String fillTemplate(final String promptTemplate, final Map<String, String
98103
while (matcher.find()) {
99104
final String fullPlaceholder = matcher.group();
100105

101-
final String key = fullPlaceholder.substring(2, fullPlaceholder.length() - 2);
106+
final String key = fullPlaceholder.substring(2, fullPlaceholder.length() - 2).trim();
102107
final String value = args.get(key);
103108

104109
if (value != null) {
@@ -131,13 +136,16 @@ public <ResponseType> ResponseType startQuery(
131136
@Nullable final String modelOverride) {
132137
try {
133138
final String promptTemplate = getTemplate(templateFileName);
134-
135139
final String filledPrompt = fillTemplate(promptTemplate, argMap);
136140

137141
final TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {};
138142
final String jsonSchema = jsonSchemaService.getJsonSchema(responseType);
139143
final Map<String, Object> schemaObject = jsonMapper.readValue(jsonSchema, typeRef);
140144

145+
if (schemaObject.get("properties") instanceof Map<?, ?> properties) {
146+
schemaObject.put("required", properties.keySet());
147+
}
148+
141149
// Determine which model to use: override or default config
142150
final String modelToUse = (modelOverride != null && !modelOverride.isBlank())
143151
? modelOverride
@@ -192,18 +200,24 @@ public <ResponseType> ResponseType startQuery(
192200
* @throws IOException if the request or response handling fails
193201
* @throws InterruptedException if the HTTP call is interrupted
194202
*/
195-
private OllamaResponse queryLLM(OllamaRequest request) throws IOException, InterruptedException {
203+
private OllamaResponse queryLLM(final OllamaRequest request) throws IOException, InterruptedException {
196204
final String json = jsonMapper.writeValueAsString(request);
197205

198-
HttpRequest req = HttpRequest.newBuilder()
206+
final HttpRequest.Builder reqBuilder = HttpRequest.newBuilder()
199207
.uri(URI.create(this.config.getUrl() + "/" + this.config.getEndpoint()))
200208
.header("Content-Type", "application/json")
201-
.POST(HttpRequest.BodyPublishers.ofString(json))
202-
.build();
209+
.timeout(Duration.ofMinutes(10))
210+
.POST(HttpRequest.BodyPublishers.ofString(json));
211+
212+
if (this.config.getApiKey() != null && !this.config.getApiKey().isBlank()) {
213+
reqBuilder.header("Authorization", "Bearer " + this.config.getApiKey());
214+
}
215+
216+
final HttpResponse<String> response = client.send(reqBuilder.build(), HttpResponse.BodyHandlers.ofString());
203217

204-
HttpResponse<String> response = client.send(req, HttpResponse.BodyHandlers.ofString());
218+
log.info("RAW OLLAMA HTTP RESPONSE BODY: {}", response.body());
205219

206-
OllamaResponse result = jsonMapper.readValue(response.body(), OllamaResponse.class);
220+
final OllamaResponse result = jsonMapper.readValue(response.body(), OllamaResponse.class);
207221

208222
if (result.getError() != null) {
209223
throw new RuntimeException("Ollama returned error: " + result.getError());
@@ -221,9 +235,11 @@ private OllamaResponse queryLLM(OllamaRequest request) throws IOException, Inter
221235
* @return an optional of the parsed response
222236
* @param <ResponseType> the type to cast to
223237
*/
224-
public <ResponseType> Optional<ResponseType> parseResponse(OllamaResponse ollamaResponse, Class<ResponseType> responseType) {
238+
public <ResponseType> Optional<ResponseType> parseResponse(
239+
final OllamaResponse ollamaResponse,
240+
final Class<ResponseType> responseType) {
225241
final String response = ollamaResponse.getResponse();
226-
if(responseType == null || response == null) {
242+
if (responseType == null || response == null) {
227243
log.info("Response is null or empty: {}", response);
228244
return Optional.empty();
229245
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package de.unistuttgart.iste.meitrex.common.ollama;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4-
import lombok.Getter;
54
import java.util.Map;
65

7-
public record OllamaRequest(@JsonProperty("model") String model, @JsonProperty("prompt") String prompt,
8-
@JsonProperty("stream") boolean stream,
9-
@JsonProperty("format") Map<String, Object> format) {
10-
11-
public OllamaRequest(String model, String prompt, boolean stream, Map<String, Object> format) {
12-
this.model = model;
13-
this.prompt = prompt;
14-
this.stream = stream;
15-
this.format = format;
16-
}
17-
}
6+
public record OllamaRequest(
7+
@JsonProperty("model") String model,
8+
@JsonProperty("prompt") String prompt,
9+
@JsonProperty("stream") boolean stream,
10+
@JsonProperty("format") Map<String, Object> format
11+
) {}
1812

src/main/java/de/unistuttgart/iste/meitrex/common/ollama/OllamaResponse.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@
1010
public class OllamaResponse {
1111

1212
@JsonProperty("total_duration")
13-
long totalDuration;
13+
private long totalDuration;
1414
@JsonProperty("load_duration")
15-
long loadDuration;
15+
private long loadDuration;
1616
@JsonProperty("prompt_eval_count")
17-
long promptEvalCount;
17+
private long promptEvalCount;
1818
@JsonProperty("prompt_eval_duration")
19-
long promptEvalDuration;
19+
private long promptEvalDuration;
2020
@JsonProperty("eval_count")
21-
long evalCount;
21+
private long evalCount;
2222
@JsonProperty("eval_duration")
23-
long evalDuration;
23+
private long evalDuration;
2424
@JsonProperty("model")
25-
String model;
25+
private String model;
2626
@JsonProperty("created_at")
27-
String createdAt;
27+
private String createdAt;
2828
@JsonProperty("response")
29-
String response;
29+
private String response;
3030
@JsonProperty("done")
31-
boolean done;
31+
private boolean done;
3232
@JsonProperty("done_reason")
33-
String doneReason;
33+
private String doneReason;
3434
@JsonProperty("context")
35-
long[] context;
35+
private long[] context;
3636
@JsonProperty("error")
37-
String error;
38-
37+
private String error;
3938

4039
/**
41-
*
4240
* @return The total duration of the request in milliseconds.
4341
*/
4442
@JsonIgnore
@@ -47,7 +45,6 @@ public long getTotalDuration() {
4745
}
4846

4947
/**
50-
*
5148
* @return The duration of the model loading in milliseconds.
5249
*/
5350
@JsonIgnore
@@ -56,7 +53,6 @@ public long getLoadDuration() {
5653
}
5754

5855
/**
59-
*
6056
* @return The number of prompt evaluations.
6157
*/
6258
@JsonIgnore
@@ -65,7 +61,6 @@ public long getPromptEvalCount() {
6561
}
6662

6763
/**
68-
*
6964
* @return The duration of the prompt evaluation in milliseconds.
7065
*/
7166
@JsonIgnore
@@ -74,7 +69,6 @@ public long getPromptEvalDuration() {
7469
}
7570

7671
/**
77-
*
7872
* @return The number of evaluations.
7973
*/
8074
@JsonIgnore
@@ -83,7 +77,6 @@ public long getEvalCount() {
8377
}
8478

8579
/**
86-
*
8780
* @return The duration of the evaluation in milliseconds.
8881
*/
8982
@JsonIgnore
@@ -92,7 +85,6 @@ public long getEvalDuration() {
9285
}
9386

9487
/**
95-
*
9688
* @return The model used for the request.
9789
*/
9890
@JsonIgnore
@@ -101,7 +93,6 @@ public String getModel() {
10193
}
10294

10395
/**
104-
*
10596
* @return The creation time of the request in ISO 8601 format.
10697
*/
10798
@JsonIgnore
@@ -110,7 +101,6 @@ public String getCreatedAt() {
110101
}
111102

112103
/**
113-
*
114104
* @return The response from the model.
115105
*/
116106
@JsonIgnore
@@ -119,17 +109,14 @@ public String getResponse() {
119109
}
120110

121111
/**
122-
*
123112
* @return Whether the request is done or not.
124113
*/
125114
@JsonIgnore
126115
public boolean isDone() {
127116
return done;
128117
}
129118

130-
131119
/**
132-
*
133120
* @return The reason why the request is done.
134121
*/
135122
@JsonIgnore
@@ -138,7 +125,6 @@ public String getDoneReason() {
138125
}
139126

140127
/**
141-
*
142128
* @return The context of the request.
143129
*/
144130
@JsonIgnore
@@ -147,5 +133,7 @@ public long[] getContext() {
147133
}
148134

149135
@JsonIgnore
150-
public String getError() {return error;}
136+
public String getError() {
137+
return error;
138+
}
151139
}

0 commit comments

Comments
 (0)