forked from Mirror/ollama4j
- Updated newly supported Ollama models
- Added `ConsoleOutputStreamHandler`
This commit is contained in:
parent
2df878c953
commit
899fa38805
@ -4,7 +4,7 @@ sidebar_position: 7
|
|||||||
|
|
||||||
# Chat
|
# Chat
|
||||||
|
|
||||||
This API lets you create a conversation with LLMs. Using this API enables you to ask questions to the model including
|
This API lets you create a conversation with LLMs. Using this API enables you to ask questions to the model including
|
||||||
information using the history of already asked questions and the respective answers.
|
information using the history of already asked questions and the respective answers.
|
||||||
|
|
||||||
## Create a new conversation and use chat history to augment follow up questions
|
## Create a new conversation and use chat history to augment follow up questions
|
||||||
@ -20,8 +20,8 @@ public class Main {
|
|||||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(OllamaModelType.LLAMA2);
|
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(OllamaModelType.LLAMA2);
|
||||||
|
|
||||||
// create first user question
|
// create first user question
|
||||||
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER,"What is the capital of France?")
|
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER, "What is the capital of France?")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// start conversation with model
|
// start conversation with model
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
@ -29,7 +29,7 @@ public class Main {
|
|||||||
System.out.println("First answer: " + chatResult.getResponse());
|
System.out.println("First answer: " + chatResult.getResponse());
|
||||||
|
|
||||||
// create next userQuestion
|
// create next userQuestion
|
||||||
requestModel = builder.withMessages(chatResult.getChatHistory()).withMessage(OllamaChatMessageRole.USER,"And what is the second largest city?").build();
|
requestModel = builder.withMessages(chatResult.getChatHistory()).withMessage(OllamaChatMessageRole.USER, "And what is the second largest city?").build();
|
||||||
|
|
||||||
// "continue" conversation with model
|
// "continue" conversation with model
|
||||||
chatResult = ollamaAPI.chat(requestModel);
|
chatResult = ollamaAPI.chat(requestModel);
|
||||||
@ -41,32 +41,38 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You will get a response similar to:
|
You will get a response similar to:
|
||||||
|
|
||||||
> First answer: Should be Paris!
|
> First answer: Should be Paris!
|
||||||
>
|
>
|
||||||
> Second answer: Marseille.
|
> Second answer: Marseille.
|
||||||
>
|
>
|
||||||
> Chat History:
|
> Chat History:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
[ {
|
[
|
||||||
"role" : "user",
|
{
|
||||||
"content" : "What is the capital of France?",
|
"role": "user",
|
||||||
"images" : [ ]
|
"content": "What is the capital of France?",
|
||||||
}, {
|
"images": []
|
||||||
"role" : "assistant",
|
},
|
||||||
"content" : "Should be Paris!",
|
{
|
||||||
"images" : [ ]
|
"role": "assistant",
|
||||||
}, {
|
"content": "Should be Paris!",
|
||||||
"role" : "user",
|
"images": []
|
||||||
"content" : "And what is the second largest city?",
|
},
|
||||||
"images" : [ ]
|
{
|
||||||
}, {
|
"role": "user",
|
||||||
"role" : "assistant",
|
"content": "And what is the second largest city?",
|
||||||
"content" : "Marseille.",
|
"images": []
|
||||||
"images" : [ ]
|
},
|
||||||
} ]
|
{
|
||||||
|
"role": "assistant",
|
||||||
|
"content": "Marseille.",
|
||||||
|
"images": []
|
||||||
|
}
|
||||||
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Create a conversation where the answer is streamed
|
## Create a conversation where the answer is streamed
|
||||||
@ -81,30 +87,50 @@ public class Main {
|
|||||||
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
||||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
|
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
|
||||||
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER,
|
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER,
|
||||||
"What is the capital of France? And what's France's connection with Mona Lisa?")
|
"What is the capital of France? And what's France's connection with Mona Lisa?")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// define a handler (Consumer<String>)
|
// define a handler (Consumer<String>)
|
||||||
OllamaStreamHandler streamHandler = (s) -> {
|
OllamaStreamHandler streamHandler = (s) -> {
|
||||||
System.out.println(s);
|
System.out.println(s);
|
||||||
};
|
};
|
||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel,streamHandler);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel, streamHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You will get a response similar to:
|
You will get a response similar to:
|
||||||
|
|
||||||
> The
|
> The
|
||||||
> The capital
|
> The capital
|
||||||
> The capital of
|
> The capital of
|
||||||
> The capital of France
|
> The capital of France
|
||||||
> The capital of France is
|
> The capital of France is
|
||||||
> The capital of France is Paris
|
> The capital of France is Paris
|
||||||
> The capital of France is Paris.
|
> The capital of France is Paris.
|
||||||
|
|
||||||
|
## Use a simple Console Output Stream Handler
|
||||||
|
|
||||||
|
```
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.impl.ConsoleOutputStreamHandler;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
String host = "http://localhost:11434/";
|
||||||
|
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
||||||
|
|
||||||
|
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(OllamaModelType.LLAMA2);
|
||||||
|
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER, "List all cricket world cup teams of 2019. Name the teams!")
|
||||||
|
.build();
|
||||||
|
OllamaStreamHandler streamHandler = new ConsoleOutputStreamHandler();
|
||||||
|
ollamaAPI.chat(requestModel, streamHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Create a new conversation with individual system prompt
|
## Create a new conversation with individual system prompt
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
@ -117,8 +143,8 @@ public class Main {
|
|||||||
|
|
||||||
// create request with system-prompt (overriding the model defaults) and user question
|
// create request with system-prompt (overriding the model defaults) and user question
|
||||||
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.SYSTEM, "You are a silent bot that only says 'NI'. Do not say anything else under any circumstances!")
|
OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.SYSTEM, "You are a silent bot that only says 'NI'. Do not say anything else under any circumstances!")
|
||||||
.withMessage(OllamaChatMessageRole.USER,"What is the capital of France? And what's France's connection with Mona Lisa?")
|
.withMessage(OllamaChatMessageRole.USER, "What is the capital of France? And what's France's connection with Mona Lisa?")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// start conversation with model
|
// start conversation with model
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
@ -128,6 +154,7 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You will get a response similar to:
|
You will get a response similar to:
|
||||||
|
|
||||||
> NI.
|
> NI.
|
||||||
@ -139,34 +166,40 @@ public class Main {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
String host = "http://localhost:11434/";
|
String host = "http://localhost:11434/";
|
||||||
|
|
||||||
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
OllamaAPI ollamaAPI = new OllamaAPI(host);
|
||||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(OllamaModelType.LLAVA);
|
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(OllamaModelType.LLAVA);
|
||||||
|
|
||||||
// Load Image from File and attach to user message (alternatively images could also be added via URL)
|
// Load Image from File and attach to user message (alternatively images could also be added via URL)
|
||||||
OllamaChatRequestModel requestModel =
|
OllamaChatRequestModel requestModel =
|
||||||
builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
|
builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
|
||||||
List.of(getImageFileFromClasspath("dog-on-a-boat.jpg"))).build();
|
List.of(getImageFileFromClasspath("dog-on-a-boat.jpg"))).build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
System.out.println("First answer: " + chatResult.getResponse());
|
System.out.println("First answer: " + chatResult.getResponse());
|
||||||
|
|
||||||
builder.reset();
|
builder.reset();
|
||||||
|
|
||||||
// Use history to ask further questions about the image or assistant answer
|
// Use history to ask further questions about the image or assistant answer
|
||||||
requestModel =
|
requestModel =
|
||||||
builder.withMessages(chatResult.getChatHistory())
|
builder.withMessages(chatResult.getChatHistory())
|
||||||
.withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
|
.withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
|
||||||
|
|
||||||
chatResult = ollamaAPI.chat(requestModel);
|
chatResult = ollamaAPI.chat(requestModel);
|
||||||
System.out.println("Second answer: " + chatResult.getResponse());
|
System.out.println("Second answer: " + chatResult.getResponse());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You will get a response similar to:
|
You will get a response similar to:
|
||||||
|
|
||||||
> First Answer: The image shows a dog sitting on the bow of a boat that is docked in calm water. The boat has two levels, with the lower level containing seating and what appears to be an engine cover. The dog seems relaxed and comfortable on the boat, looking out over the water. The background suggests it might be late afternoon or early evening, given the warm lighting and the low position of the sun in the sky.
|
> First Answer: The image shows a dog sitting on the bow of a boat that is docked in calm water. The boat has two
|
||||||
|
> levels, with the lower level containing seating and what appears to be an engine cover. The dog seems relaxed and
|
||||||
|
> comfortable on the boat, looking out over the water. The background suggests it might be late afternoon or early
|
||||||
|
> evening, given the warm lighting and the low position of the sun in the sky.
|
||||||
>
|
>
|
||||||
> Second Answer: Based on the image, it's difficult to definitively determine the breed of the dog. However, the dog appears to be medium-sized with a short coat and a brown coloration, which might suggest that it is a Golden Retriever or a similar breed. Without more details like ear shape and tail length, it's not possible to identify the exact breed confidently.
|
> Second Answer: Based on the image, it's difficult to definitively determine the breed of the dog. However, the dog
|
||||||
|
> appears to be medium-sized with a short coat and a brown coloration, which might suggest that it is a Golden Retriever
|
||||||
|
> or a similar breed. Without more details like ear shape and tail length, it's not possible to identify the exact breed
|
||||||
|
> confidently.
|
@ -0,0 +1,14 @@
|
|||||||
|
package io.github.amithkoujalgi.ollama4j.core.impl;
|
||||||
|
|
||||||
|
import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
|
||||||
|
|
||||||
|
public class ConsoleOutputStreamHandler implements OllamaStreamHandler {
|
||||||
|
private final StringBuffer response = new StringBuffer();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(String message) {
|
||||||
|
String substr = message.substring(response.length());
|
||||||
|
response.append(substr);
|
||||||
|
System.out.print(substr);
|
||||||
|
}
|
||||||
|
}
|
@ -8,72 +8,75 @@ package io.github.amithkoujalgi.ollama4j.core.types;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("ALL")
|
@SuppressWarnings("ALL")
|
||||||
public class OllamaModelType {
|
public class OllamaModelType {
|
||||||
public static final String GEMMA = "gemma";
|
public static final String GEMMA = "gemma";
|
||||||
public static final String LLAMA2 = "llama2";
|
public static final String LLAMA2 = "llama2";
|
||||||
public static final String MISTRAL = "mistral";
|
public static final String LLAMA3 = "llama3";
|
||||||
public static final String MIXTRAL = "mixtral";
|
public static final String MISTRAL = "mistral";
|
||||||
public static final String LLAVA = "llava";
|
public static final String MIXTRAL = "mixtral";
|
||||||
public static final String NEURAL_CHAT = "neural-chat";
|
public static final String LLAVA = "llava";
|
||||||
public static final String CODELLAMA = "codellama";
|
public static final String LLAVA_PHI3 = "llava-phi3";
|
||||||
public static final String DOLPHIN_MIXTRAL = "dolphin-mixtral";
|
public static final String NEURAL_CHAT = "neural-chat";
|
||||||
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
public static final String CODELLAMA = "codellama";
|
||||||
public static final String LLAMA2_UNCENSORED = "llama2-uncensored";
|
public static final String DOLPHIN_MIXTRAL = "dolphin-mixtral";
|
||||||
public static final String PHI = "phi";
|
public static final String MISTRAL_OPENORCA = "mistral-openorca";
|
||||||
public static final String ORCA_MINI = "orca-mini";
|
public static final String LLAMA2_UNCENSORED = "llama2-uncensored";
|
||||||
public static final String DEEPSEEK_CODER = "deepseek-coder";
|
public static final String PHI = "phi";
|
||||||
public static final String DOLPHIN_MISTRAL = "dolphin-mistral";
|
public static final String PHI3 = "phi3";
|
||||||
public static final String VICUNA = "vicuna";
|
public static final String ORCA_MINI = "orca-mini";
|
||||||
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
public static final String DEEPSEEK_CODER = "deepseek-coder";
|
||||||
public static final String ZEPHYR = "zephyr";
|
public static final String DOLPHIN_MISTRAL = "dolphin-mistral";
|
||||||
public static final String OPENHERMES = "openhermes";
|
public static final String VICUNA = "vicuna";
|
||||||
public static final String QWEN = "qwen";
|
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
|
||||||
public static final String WIZARDCODER = "wizardcoder";
|
public static final String ZEPHYR = "zephyr";
|
||||||
public static final String LLAMA2_CHINESE = "llama2-chinese";
|
public static final String OPENHERMES = "openhermes";
|
||||||
public static final String TINYLLAMA = "tinyllama";
|
public static final String QWEN = "qwen";
|
||||||
public static final String PHIND_CODELLAMA = "phind-codellama";
|
public static final String WIZARDCODER = "wizardcoder";
|
||||||
public static final String OPENCHAT = "openchat";
|
public static final String LLAMA2_CHINESE = "llama2-chinese";
|
||||||
public static final String ORCA2 = "orca2";
|
public static final String TINYLLAMA = "tinyllama";
|
||||||
public static final String FALCON = "falcon";
|
public static final String PHIND_CODELLAMA = "phind-codellama";
|
||||||
public static final String WIZARD_MATH = "wizard-math";
|
public static final String OPENCHAT = "openchat";
|
||||||
public static final String TINYDOLPHIN = "tinydolphin";
|
public static final String ORCA2 = "orca2";
|
||||||
public static final String NOUS_HERMES = "nous-hermes";
|
public static final String FALCON = "falcon";
|
||||||
public static final String YI = "yi";
|
public static final String WIZARD_MATH = "wizard-math";
|
||||||
public static final String DOLPHIN_PHI = "dolphin-phi";
|
public static final String TINYDOLPHIN = "tinydolphin";
|
||||||
public static final String STARLING_LM = "starling-lm";
|
public static final String NOUS_HERMES = "nous-hermes";
|
||||||
public static final String STARCODER = "starcoder";
|
public static final String YI = "yi";
|
||||||
public static final String CODEUP = "codeup";
|
public static final String DOLPHIN_PHI = "dolphin-phi";
|
||||||
public static final String MEDLLAMA2 = "medllama2";
|
public static final String STARLING_LM = "starling-lm";
|
||||||
public static final String STABLE_CODE = "stable-code";
|
public static final String STARCODER = "starcoder";
|
||||||
public static final String WIZARDLM_UNCENSORED = "wizardlm-uncensored";
|
public static final String CODEUP = "codeup";
|
||||||
public static final String BAKLLAVA = "bakllava";
|
public static final String MEDLLAMA2 = "medllama2";
|
||||||
public static final String EVERYTHINGLM = "everythinglm";
|
public static final String STABLE_CODE = "stable-code";
|
||||||
public static final String SOLAR = "solar";
|
public static final String WIZARDLM_UNCENSORED = "wizardlm-uncensored";
|
||||||
public static final String STABLE_BELUGA = "stable-beluga";
|
public static final String BAKLLAVA = "bakllava";
|
||||||
public static final String SQLCODER = "sqlcoder";
|
public static final String EVERYTHINGLM = "everythinglm";
|
||||||
public static final String YARN_MISTRAL = "yarn-mistral";
|
public static final String SOLAR = "solar";
|
||||||
public static final String NOUS_HERMES2_MIXTRAL = "nous-hermes2-mixtral";
|
public static final String STABLE_BELUGA = "stable-beluga";
|
||||||
public static final String SAMANTHA_MISTRAL = "samantha-mistral";
|
public static final String SQLCODER = "sqlcoder";
|
||||||
public static final String STABLELM_ZEPHYR = "stablelm-zephyr";
|
public static final String YARN_MISTRAL = "yarn-mistral";
|
||||||
public static final String MEDITRON = "meditron";
|
public static final String NOUS_HERMES2_MIXTRAL = "nous-hermes2-mixtral";
|
||||||
public static final String WIZARD_VICUNA = "wizard-vicuna";
|
public static final String SAMANTHA_MISTRAL = "samantha-mistral";
|
||||||
public static final String STABLELM2 = "stablelm2";
|
public static final String STABLELM_ZEPHYR = "stablelm-zephyr";
|
||||||
public static final String MAGICODER = "magicoder";
|
public static final String MEDITRON = "meditron";
|
||||||
public static final String YARN_LLAMA2 = "yarn-llama2";
|
public static final String WIZARD_VICUNA = "wizard-vicuna";
|
||||||
public static final String NOUS_HERMES2 = "nous-hermes2";
|
public static final String STABLELM2 = "stablelm2";
|
||||||
public static final String DEEPSEEK_LLM = "deepseek-llm";
|
public static final String MAGICODER = "magicoder";
|
||||||
public static final String LLAMA_PRO = "llama-pro";
|
public static final String YARN_LLAMA2 = "yarn-llama2";
|
||||||
public static final String OPEN_ORCA_PLATYPUS2 = "open-orca-platypus2";
|
public static final String NOUS_HERMES2 = "nous-hermes2";
|
||||||
public static final String CODEBOOGA = "codebooga";
|
public static final String DEEPSEEK_LLM = "deepseek-llm";
|
||||||
public static final String MISTRALLITE = "mistrallite";
|
public static final String LLAMA_PRO = "llama-pro";
|
||||||
public static final String NEXUSRAVEN = "nexusraven";
|
public static final String OPEN_ORCA_PLATYPUS2 = "open-orca-platypus2";
|
||||||
public static final String GOLIATH = "goliath";
|
public static final String CODEBOOGA = "codebooga";
|
||||||
public static final String NOMIC_EMBED_TEXT = "nomic-embed-text";
|
public static final String MISTRALLITE = "mistrallite";
|
||||||
public static final String NOTUX = "notux";
|
public static final String NEXUSRAVEN = "nexusraven";
|
||||||
public static final String ALFRED = "alfred";
|
public static final String GOLIATH = "goliath";
|
||||||
public static final String MEGADOLPHIN = "megadolphin";
|
public static final String NOMIC_EMBED_TEXT = "nomic-embed-text";
|
||||||
public static final String WIZARDLM = "wizardlm";
|
public static final String NOTUX = "notux";
|
||||||
public static final String XWINLM = "xwinlm";
|
public static final String ALFRED = "alfred";
|
||||||
public static final String NOTUS = "notus";
|
public static final String MEGADOLPHIN = "megadolphin";
|
||||||
public static final String DUCKDB_NSQL = "duckdb-nsql";
|
public static final String WIZARDLM = "wizardlm";
|
||||||
public static final String ALL_MINILM = "all-minilm";
|
public static final String XWINLM = "xwinlm";
|
||||||
|
public static final String NOTUS = "notus";
|
||||||
|
public static final String DUCKDB_NSQL = "duckdb-nsql";
|
||||||
|
public static final String ALL_MINILM = "all-minilm";
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user