Skip to content

Add tool context support to chat options and enhance function calling #1458

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

Closed
wants to merge 1 commit into from
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 @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -80,6 +81,10 @@ public class AnthropicChatOptions implements ChatOptions, FunctionCallingOptions

@JsonIgnore
private Boolean proxyToolCalls;

@JsonIgnore
private Map<String, Object> toolContext;

// @formatter:on

public static Builder builder() {
Expand Down Expand Up @@ -152,6 +157,16 @@ public Builder withProxyToolCalls(Boolean proxyToolCalls) {
return this;
}

public Builder withToolContext(Map<String, Object> toolContext) {
if (this.options.toolContext == null) {
this.options.toolContext = toolContext;
}
else {
this.options.toolContext.putAll(toolContext);
}
return this;
}

public AnthropicChatOptions build() {
return this.options;
}
Expand Down Expand Up @@ -263,6 +278,16 @@ public void setProxyToolCalls(Boolean proxyToolCalls) {
this.proxyToolCalls = proxyToolCalls;
}

@Override
public Map<String, Object> getToolContext() {
return this.toolContext;
}

@Override
public void setToolContext(Map<String, Object> toolContext) {
this.toolContext = toolContext;
}

@Override
public AnthropicChatOptions copy() {
return fromOptions(this);
Expand All @@ -279,6 +304,7 @@ public static AnthropicChatOptions fromOptions(AnthropicChatOptions fromOptions)
.withFunctionCallbacks(fromOptions.getFunctionCallbacks())
.withFunctions(fromOptions.getFunctions())
.withProxyToolCalls(fromOptions.getProxyToolCalls())
.withToolContext(fromOptions.getToolContext())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,17 @@
import java.util.Map;
import java.util.Set;

import com.azure.ai.openai.models.AzureChatEnhancementConfiguration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallingOptions;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.util.Assert;
import org.stringtemplate.v4.compiler.CodeGenerator.primary_return;

import com.azure.ai.openai.models.AzureChatEnhancementConfiguration;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* The configuration information for a chat completions request. Completions support a
Expand Down Expand Up @@ -199,6 +198,10 @@ public class AzureOpenAiChatOptions implements FunctionCallingOptions, ChatOptio
@JsonIgnore
private AzureChatEnhancementConfiguration enhancements;

@NestedConfigurationProperty
@JsonIgnore
private Map<String, Object> toolContext;

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -312,6 +315,16 @@ public Builder withEnhancements(AzureChatEnhancementConfiguration enhancements)
return this;
}

public Builder withToolContext(Map<String, Object> toolContext) {
if (this.options.toolContext == null) {
this.options.toolContext = toolContext;
}
else {
this.options.toolContext.putAll(toolContext);
}
return this;
}

public AzureOpenAiChatOptions build() {
return this.options;
}
Expand Down Expand Up @@ -498,6 +511,16 @@ public void setProxyToolCalls(Boolean proxyToolCalls) {
this.proxyToolCalls = proxyToolCalls;
}

@Override
public Map<String, Object> getToolContext() {
return this.toolContext;
}

@Override
public void setToolContext(Map<String, Object> toolContext) {
this.toolContext = toolContext;
}

@Override
public AzureOpenAiChatOptions copy() {
return fromOptions(this);
Expand All @@ -521,6 +544,7 @@ public static AzureOpenAiChatOptions fromOptions(AzureOpenAiChatOptions fromOpti
.withLogprobs(fromOptions.isLogprobs())
.withTopLogprobs(fromOptions.getTopLogProbs())
.withEnhancements(fromOptions.getEnhancements())
.withToolContext(fromOptions.getToolContext())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -145,6 +146,11 @@ public class MiniMaxChatOptions implements FunctionCallingOptions, ChatOptions {

@JsonIgnore
private Boolean proxyToolCalls;

@NestedConfigurationProperty
@JsonIgnore
private Map<String, Object> toolContext;

// @formatter:on

public static Builder builder() {
Expand Down Expand Up @@ -250,6 +256,16 @@ public Builder withProxyToolCalls(Boolean proxyToolCalls) {
return this;
}

public Builder withToolContext(Map<String, Object> toolContext) {
if (this.options.toolContext == null) {
this.options.toolContext = toolContext;
}
else {
this.options.toolContext.putAll(toolContext);
}
return this;
}

public MiniMaxChatOptions build() {
return this.options;
}
Expand Down Expand Up @@ -411,6 +427,16 @@ public void setProxyToolCalls(Boolean proxyToolCalls) {
this.proxyToolCalls = proxyToolCalls;
}

@Override
public Map<String, Object> getToolContext() {
return this.toolContext;
}

@Override
public void setToolContext(Map<String, Object> toolContext) {
this.toolContext = toolContext;
}

@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -429,6 +455,7 @@ public int hashCode() {
result = prime * result + ((tools == null) ? 0 : tools.hashCode());
result = prime * result + ((toolChoice == null) ? 0 : toolChoice.hashCode());
result = prime * result + ((proxyToolCalls == null) ? 0 : proxyToolCalls.hashCode());
result = prime * result + ((toolContext == null) ? 0 : toolContext.hashCode());
return result;
}

Expand Down Expand Up @@ -525,6 +552,14 @@ else if (!toolChoice.equals(other.toolChoice))
}
else if (!proxyToolCalls.equals(other.proxyToolCalls))
return false;

if (this.toolContext == null) {
if (other.toolContext != null)
return false;
}
else if (!toolContext.equals(other.toolContext))
return false;

return true;
}

Expand All @@ -550,6 +585,7 @@ public static MiniMaxChatOptions fromOptions(MiniMaxChatOptions fromOptions) {
.withFunctionCallbacks(fromOptions.getFunctionCallbacks())
.withFunctions(fromOptions.getFunctions())
.withProxyToolCalls(fromOptions.getProxyToolCalls())
.withToolContext(fromOptions.getToolContext())
.build();
}

Expand Down
Loading