forked from Mirror/ollama4j
Updated withMessages
method of OllamaChatRequestBuilder
to reset the messages
This commit is contained in:
parent
11a98a72a1
commit
3aa0fc77cb
@ -82,6 +82,33 @@ You will get a response similar to:
|
||||
]
|
||||
```
|
||||
|
||||
## Conversational loop
|
||||
|
||||
```java
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
OllamaAPI ollamaAPI = new OllamaAPI();
|
||||
ollamaAPI.setRequestTimeoutSeconds(60);
|
||||
|
||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance("<your-model>");
|
||||
|
||||
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER, "<your-first-message>").build();
|
||||
OllamaChatResult initialChatResult = ollamaAPI.chat(requestModel);
|
||||
System.out.println(initialChatResult.getResponse());
|
||||
|
||||
List<OllamaChatMessage> history = initialChatResult.getChatHistory();
|
||||
|
||||
while (true) {
|
||||
OllamaChatResult chatResult = ollamaAPI.chat(builder.withMessages(history).withMessage(OllamaChatMessageRole.USER, "<your-new-message").build());
|
||||
System.out.println(chatResult.getResponse());
|
||||
history = chatResult.getChatHistory();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Create a conversation where the answer is streamed
|
||||
|
||||
```java
|
||||
|
@ -1,5 +1,10 @@
|
||||
package io.github.ollama4j.models.chat;
|
||||
|
||||
import io.github.ollama4j.utils.Options;
|
||||
import io.github.ollama4j.utils.Utils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
@ -8,12 +13,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.github.ollama4j.utils.Options;
|
||||
import io.github.ollama4j.utils.Utils;
|
||||
|
||||
/**
|
||||
* Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern.
|
||||
*/
|
||||
@ -21,88 +20,85 @@ public class OllamaChatRequestBuilder {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
|
||||
|
||||
private OllamaChatRequestBuilder(String model, List<OllamaChatMessage> messages){
|
||||
private OllamaChatRequestBuilder(String model, List<OllamaChatMessage> messages) {
|
||||
request = new OllamaChatRequest(model, messages);
|
||||
}
|
||||
|
||||
private OllamaChatRequest request;
|
||||
|
||||
public static OllamaChatRequestBuilder getInstance(String model){
|
||||
public static OllamaChatRequestBuilder getInstance(String model) {
|
||||
return new OllamaChatRequestBuilder(model, new ArrayList<>());
|
||||
}
|
||||
|
||||
public OllamaChatRequest build(){
|
||||
public OllamaChatRequest build() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
public void reset() {
|
||||
request = new OllamaChatRequest(request.getModel(), new ArrayList<>());
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List<File> images){
|
||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List<File> images) {
|
||||
List<OllamaChatMessage> messages = this.request.getMessages();
|
||||
|
||||
List<byte[]> binaryImages = images.stream().map(file -> {
|
||||
try {
|
||||
return Files.readAllBytes(file.toPath());
|
||||
} catch (IOException e) {
|
||||
LOG.warn(String.format("File '%s' could not be accessed, will not add to message!",file.toPath()), e);
|
||||
LOG.warn(String.format("File '%s' could not be accessed, will not add to message!", file.toPath()), e);
|
||||
return new byte[0];
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
messages.add(new OllamaChatMessage(role,content,binaryImages));
|
||||
messages.add(new OllamaChatMessage(role, content, binaryImages));
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls){
|
||||
public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls) {
|
||||
List<OllamaChatMessage> messages = this.request.getMessages();
|
||||
List<byte[]> binaryImages = null;
|
||||
if(imageUrls.length>0){
|
||||
if (imageUrls.length > 0) {
|
||||
binaryImages = new ArrayList<>();
|
||||
for (String imageUrl : imageUrls) {
|
||||
try{
|
||||
try {
|
||||
binaryImages.add(Utils.loadImageBytesFromUrl(imageUrl));
|
||||
}
|
||||
catch (URISyntaxException e){
|
||||
LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!",imageUrl), e);
|
||||
}
|
||||
catch (IOException e){
|
||||
LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!",imageUrl), e);
|
||||
} catch (URISyntaxException e) {
|
||||
LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!", imageUrl), e);
|
||||
} catch (IOException e) {
|
||||
LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!", imageUrl), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
messages.add(new OllamaChatMessage(role,content,binaryImages));
|
||||
messages.add(new OllamaChatMessage(role, content, binaryImages));
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages){
|
||||
this.request.getMessages().addAll(messages);
|
||||
return this;
|
||||
public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages) {
|
||||
return new OllamaChatRequestBuilder(request.getModel(), messages);
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withOptions(Options options){
|
||||
public OllamaChatRequestBuilder withOptions(Options options) {
|
||||
this.request.setOptions(options.getOptionsMap());
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withGetJsonResponse(){
|
||||
public OllamaChatRequestBuilder withGetJsonResponse() {
|
||||
this.request.setReturnFormatJson(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withTemplate(String template){
|
||||
public OllamaChatRequestBuilder withTemplate(String template) {
|
||||
this.request.setTemplate(template);
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withStreaming(){
|
||||
public OllamaChatRequestBuilder withStreaming() {
|
||||
this.request.setStream(true);
|
||||
return this;
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withKeepAlive(String keepAlive){
|
||||
public OllamaChatRequestBuilder withKeepAlive(String keepAlive) {
|
||||
this.request.setKeepAlive(keepAlive);
|
||||
return this;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user