forked from Mirror/ollama4j
- Added verbose
param to OllamaAPI
- Setup logback logging - Updated readme
This commit is contained in:
parent
98a98d3276
commit
d50e30f147
1
.gitignore
vendored
1
.gitignore
vendored
@ -37,3 +37,4 @@ build/
|
|||||||
### Mac OS ###
|
### Mac OS ###
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/.idea/
|
/.idea/
|
||||||
|
/src/main/java/io/github/amithkoujalgi/ollama4j/core/localtests/
|
||||||
|
5
pom.xml
5
pom.xml
@ -106,6 +106,11 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.10.1</version>
|
<version>2.10.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>1.3.11</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||||
<artifactId>httpclient5</artifactId>
|
<artifactId>httpclient5</artifactId>
|
||||||
|
@ -13,6 +13,8 @@ import org.apache.hc.core5.http.HttpEntity;
|
|||||||
import org.apache.hc.core5.http.ParseException;
|
import org.apache.hc.core5.http.ParseException;
|
||||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@ -28,9 +30,9 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings({"DuplicatedCode", "ExtractMethodRecommender"})
|
@SuppressWarnings({"DuplicatedCode", "ExtractMethodRecommender"})
|
||||||
public class OllamaAPI {
|
public class OllamaAPI {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
|
||||||
|
|
||||||
private final String host;
|
private final String host;
|
||||||
|
private boolean verbose = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates the Ollama API.
|
* Instantiates the Ollama API.
|
||||||
@ -45,6 +47,14 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set/unset logging of responses
|
||||||
|
* @param verbose - true/false
|
||||||
|
*/
|
||||||
|
public void setVerbose(boolean verbose) {
|
||||||
|
this.verbose = verbose;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List available models from Ollama server.
|
* List available models from Ollama server.
|
||||||
*
|
*
|
||||||
@ -77,15 +87,15 @@ public class OllamaAPI {
|
|||||||
/**
|
/**
|
||||||
* Gets model details from the Ollama server.
|
* Gets model details from the Ollama server.
|
||||||
*
|
*
|
||||||
* @param model the model
|
* @param modelName the model
|
||||||
* @return the model details
|
* @return the model details
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws OllamaBaseException
|
* @throws OllamaBaseException
|
||||||
* @throws ParseException
|
* @throws ParseException
|
||||||
*/
|
*/
|
||||||
public ModelDetail getModelDetails(Model model) throws IOException, OllamaBaseException, ParseException {
|
public ModelDetail getModelDetails(String modelName) throws IOException, OllamaBaseException, ParseException {
|
||||||
String url = this.host + "/api/show";
|
String url = this.host + "/api/show";
|
||||||
String jsonData = String.format("{\"name\": \"%s\"}", model.getName());
|
String jsonData = String.format("{\"name\": \"%s\"}", modelName);
|
||||||
final HttpPost httpPost = new HttpPost(url);
|
final HttpPost httpPost = new HttpPost(url);
|
||||||
final StringEntity entity = new StringEntity(jsonData);
|
final StringEntity entity = new StringEntity(jsonData);
|
||||||
httpPost.setEntity(entity);
|
httpPost.setEntity(entity);
|
||||||
@ -143,15 +153,15 @@ public class OllamaAPI {
|
|||||||
* Create a custom model from a model file.
|
* Create a custom model from a model file.
|
||||||
* Read more about custom model file creation <a href="https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md">here</a>.
|
* Read more about custom model file creation <a href="https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md">here</a>.
|
||||||
*
|
*
|
||||||
* @param name the name of the custom model to be created
|
* @param modelName the name of the custom model to be created.
|
||||||
* @param modelFilePath the path to model file that exists on the Ollama server.
|
* @param modelFilePath the path to model file that exists on the Ollama server.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws ParseException
|
* @throws ParseException
|
||||||
* @throws OllamaBaseException
|
* @throws OllamaBaseException
|
||||||
*/
|
*/
|
||||||
public void createModel(String name, String modelFilePath) throws IOException, ParseException, OllamaBaseException {
|
public void createModel(String modelName, String modelFilePath) throws IOException, ParseException, OllamaBaseException {
|
||||||
String url = this.host + "/api/create";
|
String url = this.host + "/api/create";
|
||||||
String jsonData = String.format("{\"name\": \"%s\", \"path\": \"%s\"}", name, modelFilePath);
|
String jsonData = String.format("{\"name\": \"%s\", \"path\": \"%s\"}", modelName, modelFilePath);
|
||||||
final HttpPost httpPost = new HttpPost(url);
|
final HttpPost httpPost = new HttpPost(url);
|
||||||
final StringEntity entity = new StringEntity(jsonData);
|
final StringEntity entity = new StringEntity(jsonData);
|
||||||
httpPost.setEntity(entity);
|
httpPost.setEntity(entity);
|
||||||
@ -163,6 +173,13 @@ public class OllamaAPI {
|
|||||||
String responseString = "";
|
String responseString = "";
|
||||||
if (responseEntity != null) {
|
if (responseEntity != null) {
|
||||||
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||||
|
// FIXME: Ollama API returns HTTP status code 200 for model creation failure cases. Correct this if the issue is fixed in the Ollama API server.
|
||||||
|
if (responseString.contains("error")) {
|
||||||
|
throw new OllamaBaseException(responseString);
|
||||||
|
}
|
||||||
|
if (verbose) {
|
||||||
|
logger.info(responseString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||||
@ -193,6 +210,9 @@ public class OllamaAPI {
|
|||||||
String responseString = "";
|
String responseString = "";
|
||||||
if (responseEntity != null) {
|
if (responseEntity != null) {
|
||||||
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||||
|
if (verbose) {
|
||||||
|
logger.info(responseString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (statusCode == 404 && responseString.contains("model") && responseString.contains("not found")) {
|
if (statusCode == 404 && responseString.contains("model") && responseString.contains("not found")) {
|
||||||
return;
|
return;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package io.github.amithkoujalgi.ollama4j.core.models;
|
package io.github.amithkoujalgi.ollama4j.core.models;
|
||||||
|
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
|
||||||
public class ModelDetail {
|
public class ModelDetail {
|
||||||
private String license, modelfile, parameters, template;
|
private String license, modelfile, parameters, template;
|
||||||
|
|
||||||
@ -34,4 +36,9 @@ public class ModelDetail {
|
|||||||
public void setTemplate(String template) {
|
public void setTemplate(String template) {
|
||||||
this.template = template;
|
this.template = template;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
15
src/main/resources/logback.xml
Normal file
15
src/main/resources/logback.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
<logger name="org.apache" level="WARN"/>
|
||||||
|
<logger name="httpclient" level="WARN"/>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user