Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class OllamaChatModel extends ChatModelBase {
private final OllamaOptions defaultOptions;
private final Formatter<OllamaMessage, OllamaResponse, OllamaRequest> formatter;

private boolean stream = true;

/**
* Creates a new OllamaChatModel.
*
Expand Down Expand Up @@ -118,6 +120,29 @@ public String getModelName() {
return this.modelName;
}

/**
* Sets whether streaming responses should be used.
*
* <p>When {@code stream} is {@code false}, {@link #doStream(List, List, GenerateOptions)}
* dispatches to a non-streaming HTTP call and returns a single complete
* {@link ChatResponse} (suitable for consumption via {@code blockLast()}).
* When {@code true} (the default), responses are streamed incrementally.
*
* @param stream {@code true} to stream, {@code false} to use a single non-streaming call
*/
public void setStream(boolean stream) {
this.stream = stream;
}

/**
* Returns whether streaming responses are enabled.
*
* @return the current streaming flag
*/
public boolean isStream() {
return stream;
}

/**
* Chat with the model using Ollama-specific options.
* <p>
Expand Down Expand Up @@ -145,12 +170,16 @@ public ChatResponse chat(List<Msg> messages, GenerateOptions options) {
@Override
protected Flux<ChatResponse> doStream(
List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) {
boolean effectiveStream = stream;
if (options != null && options.getStream() != null) {
effectiveStream = options.getStream();
}
return streamWithHttpClient(
messages,
tools,
options != null ? options.getToolChoice() : null,
OllamaOptions.fromGenerateOptions(options),
true);
effectiveStream);
}

/**
Expand Down Expand Up @@ -250,6 +279,7 @@ public static class Builder {
private HttpTransport httpTransport;
private ProxyConfig proxyConfig;
private int contextWindowSize = -1;
private boolean stream = true;

public Builder modelName(String modelName) {
this.modelName = modelName;
Expand Down Expand Up @@ -333,6 +363,22 @@ public Builder contextWindowSize(int contextWindowSize) {
return this;
}

/**
* Sets whether streaming responses should be used.
*
* <p>When {@code stream} is {@code false}, {@link #doStream(List, List, GenerateOptions)}
* dispatches to a non-streaming HTTP call and returns a single complete
* response. When {@code true} (the default), responses are streamed
* incrementally.
*
* @param stream {@code true} to stream, {@code false} to use a non-streaming call
* @return this builder instance
*/
public Builder stream(boolean stream) {
this.stream = stream;
return this;
}

public OllamaChatModel build() {
OllamaOptions finalOptions =
defaultOptions != null
Expand All @@ -349,6 +395,7 @@ public OllamaChatModel build() {

OllamaChatModel model =
new OllamaChatModel(modelName, baseUrl, finalOptions, formatter, transport);
model.setStream(stream);
if (contextWindowSize >= 0) {
model.setContextWindowSize(contextWindowSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ private static void applyAdvancedOptions(
if (contextWindowSize != null) {
builder.contextWindowSize(contextWindowSize);
}
Boolean stream = context.getStream();
if (stream != null) {
builder.stream(stream);
}
}

private static String firstNonBlank(String preferred, String fallback) {
Expand Down
Loading