-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Added support for the "think" for Ollama #3386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
524f12c
af9958b
91fb151
f77e08a
b188adc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
* @author Christian Tzolov | ||
* @author Thomas Vitale | ||
* @author Ilayaperumal Gopinathan | ||
* @author Sun Yuhan | ||
* @since 0.8.0 | ||
* @see <a href= | ||
* "https://github.com/ollama/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">Ollama | ||
|
@@ -318,6 +319,14 @@ public class OllamaOptions implements ToolCallingChatOptions, EmbeddingOptions { | |
@JsonProperty("truncate") | ||
private Boolean truncate; | ||
|
||
/** | ||
* The model should think before responding, if supported. | ||
* If this value is not specified, it defaults to null, and Ollama will return | ||
* the thought process within the `content` field of the response, wrapped in `<thinking>` tags. | ||
*/ | ||
@JsonProperty("think") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like 'think' is not a part of the options map in ollama, but a 'top level' field in the request object.
and the golang type supporting this feature also shows the same structure. https://github.com/ollama/ollama/blob/45f56355d557b7130c7c07bbd6e1b634a758d946/api/types.go#L91 So it shouldn't be added to the options map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment in the code
seems to contradict what was documented on the ollama web site that shows the 'think' response as a separate field from 'content', and not nested inside the 'content' field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we want to also expose it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Of course, no problem. I will continue improving this PR and update my progress here in a timely manner. Before proceeding, I want to confirm whether I’ve correctly understood your point: Are you suggesting that we should not add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Firstly, the content in the comment is actually a summary I derived through practical testing. I think this is a form of backward compatibility Ollama implemented for users still employing the old parameter-passing method. Before Ollama supported the "think" flag, if we made a request to a model that supports thinking like this:
Ollama would enable thinking by default and return the thought process wrapped in
This remains the case in the latest version of Ollama: if the
Only when we specify the
Response:
I think it's the same for Spring AI. We should maintain compatibility with users who are using older versions, meaning that if the "think" flag is not specified, the returned format should remain unchanged. |
||
private Boolean think; | ||
|
||
@JsonIgnore | ||
private Boolean internalToolExecutionEnabled; | ||
|
||
|
@@ -365,6 +374,7 @@ public static OllamaOptions fromOptions(OllamaOptions fromOptions) { | |
.format(fromOptions.getFormat()) | ||
.keepAlive(fromOptions.getKeepAlive()) | ||
.truncate(fromOptions.getTruncate()) | ||
.think(fromOptions.getThink()) | ||
.useNUMA(fromOptions.getUseNUMA()) | ||
.numCtx(fromOptions.getNumCtx()) | ||
.numBatch(fromOptions.getNumBatch()) | ||
|
@@ -704,6 +714,14 @@ public void setTruncate(Boolean truncate) { | |
this.truncate = truncate; | ||
} | ||
|
||
public Boolean getThink() { | ||
return this.think; | ||
} | ||
|
||
public void setThink(Boolean think) { | ||
this.think = think; | ||
} | ||
|
||
@Override | ||
@JsonIgnore | ||
public List<ToolCallback> getToolCallbacks() { | ||
|
@@ -804,7 +822,8 @@ public boolean equals(Object o) { | |
&& Objects.equals(this.repeatPenalty, that.repeatPenalty) | ||
&& Objects.equals(this.presencePenalty, that.presencePenalty) | ||
&& Objects.equals(this.frequencyPenalty, that.frequencyPenalty) | ||
&& Objects.equals(this.mirostat, that.mirostat) && Objects.equals(this.mirostatTau, that.mirostatTau) | ||
&& Objects.equals(this.think, that.think) && Objects.equals(this.mirostat, that.mirostat) | ||
&& Objects.equals(this.mirostatTau, that.mirostatTau) | ||
&& Objects.equals(this.mirostatEta, that.mirostatEta) | ||
&& Objects.equals(this.penalizeNewline, that.penalizeNewline) && Objects.equals(this.stop, that.stop) | ||
&& Objects.equals(this.toolCallbacks, that.toolCallbacks) | ||
|
@@ -814,13 +833,13 @@ public boolean equals(Object o) { | |
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.model, this.format, this.keepAlive, this.truncate, this.useNUMA, this.numCtx, | ||
this.numBatch, this.numGPU, this.mainGPU, this.lowVRAM, this.f16KV, this.logitsAll, this.vocabOnly, | ||
this.useMMap, this.useMLock, this.numThread, this.numKeep, this.seed, this.numPredict, this.topK, | ||
this.topP, this.minP, this.tfsZ, this.typicalP, this.repeatLastN, this.temperature, this.repeatPenalty, | ||
this.presencePenalty, this.frequencyPenalty, this.mirostat, this.mirostatTau, this.mirostatEta, | ||
this.penalizeNewline, this.stop, this.toolCallbacks, this.toolNames, this.internalToolExecutionEnabled, | ||
this.toolContext); | ||
return Objects.hash(this.model, this.format, this.keepAlive, this.truncate, this.think, this.useNUMA, | ||
this.numCtx, this.numBatch, this.numGPU, this.mainGPU, this.lowVRAM, this.f16KV, this.logitsAll, | ||
this.vocabOnly, this.useMMap, this.useMLock, this.numThread, this.numKeep, this.seed, this.numPredict, | ||
this.topK, this.topP, this.minP, this.tfsZ, this.typicalP, this.repeatLastN, this.temperature, | ||
this.repeatPenalty, this.presencePenalty, this.frequencyPenalty, this.mirostat, this.mirostatTau, | ||
this.mirostatEta, this.penalizeNewline, this.stop, this.toolCallbacks, this.toolNames, | ||
this.internalToolExecutionEnabled, this.toolContext); | ||
} | ||
|
||
public static class Builder { | ||
|
@@ -852,6 +871,11 @@ public Builder truncate(Boolean truncate) { | |
return this; | ||
} | ||
|
||
public Builder think(Boolean think) { | ||
this.options.think = think; | ||
return this; | ||
} | ||
|
||
public Builder useNUMA(Boolean useNUMA) { | ||
this.options.useNUMA = useNUMA; | ||
return this; | ||
|
Uh oh!
There was an error while loading. Please reload this page.