-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from devoxx/issue-177
Feat #177: Show Ollama language models window context
- Loading branch information
Showing
13 changed files
with
179 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/devoxx/genie/service/OllamaApiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.devoxx.genie.service; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import okhttp3.*; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
|
||
public class OllamaApiService { | ||
private static final String OLLAMA_API_URL = "http://localhost:11434/api/show"; | ||
private static final OkHttpClient client = new OkHttpClient(); | ||
private static final Gson gson = new Gson(); | ||
|
||
public static int getModelContext(@NotNull String modelName) throws IOException { | ||
RequestBody body = RequestBody.create( | ||
MediaType.parse("application/json"), | ||
"{\"name\":\"" + modelName + "\"}" | ||
); | ||
|
||
Request request = new Request.Builder() | ||
.url(OLLAMA_API_URL) | ||
.post(body) | ||
.build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); | ||
|
||
JsonObject jsonObject = gson.fromJson(response.body().string(), JsonObject.class); | ||
return findContextLength(jsonObject); | ||
} | ||
} | ||
|
||
private static int findContextLength(@NotNull JsonObject jsonObject) { | ||
JsonElement modelInfo = jsonObject.get("model_info"); | ||
if (modelInfo != null && modelInfo.isJsonObject()) { | ||
JsonObject modelInfoObject = modelInfo.getAsJsonObject(); | ||
for (String key : modelInfoObject.keySet()) { | ||
if (key.endsWith(".context_length")) { | ||
return modelInfoObject.get(key).getAsInt(); | ||
} | ||
} | ||
} | ||
|
||
// Fallback: check if context_length exists directly in the root | ||
JsonElement contextLength = jsonObject.get("context_length"); | ||
if (contextLength != null) { | ||
return contextLength.getAsInt(); | ||
} | ||
|
||
return -1; // Return -1 if not found | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.