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.
|
||||
*/
|
||||
@ -63,11 +62,9 @@ public class OllamaChatRequestBuilder {
|
||||
for (String imageUrl : imageUrls) {
|
||||
try {
|
||||
binaryImages.add(Utils.loadImageBytesFromUrl(imageUrl));
|
||||
}
|
||||
catch (URISyntaxException e){
|
||||
} catch (URISyntaxException e) {
|
||||
LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!", imageUrl), e);
|
||||
}
|
||||
catch (IOException e){
|
||||
} catch (IOException e) {
|
||||
LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!", imageUrl), e);
|
||||
}
|
||||
}
|
||||
@ -78,8 +75,7 @@ public class OllamaChatRequestBuilder {
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages) {
|
||||
this.request.getMessages().addAll(messages);
|
||||
return this;
|
||||
return new OllamaChatRequestBuilder(request.getModel(), messages);
|
||||
}
|
||||
|
||||
public OllamaChatRequestBuilder withOptions(Options options) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user