Add Tool Call ID (#12956)

* routes/types: add tool call id

---------

Co-authored-by: ParthSareen <parth.sareen@ollama.com>
This commit is contained in:
Grace
2025-11-04 16:43:33 -08:00
committed by GitHub
parent ba8c035846
commit 809b9c68fa
5 changed files with 122 additions and 16 deletions

View File

@@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"log/slog"
"math/rand"
"net/http"
"slices"
"strings"
@@ -229,20 +228,11 @@ func ToUsage(r api.ChatResponse) Usage {
}
}
func toolCallId() string {
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, 8)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return "call_" + strings.ToLower(string(b))
}
// ToToolCalls converts api.ToolCall to OpenAI ToolCall format
func ToToolCalls(tc []api.ToolCall) []ToolCall {
toolCalls := make([]ToolCall, len(tc))
for i, tc := range tc {
toolCalls[i].ID = toolCallId()
toolCalls[i].ID = tc.ID
toolCalls[i].Type = "function"
toolCalls[i].Function.Name = tc.Function.Name
toolCalls[i].Index = tc.Function.Index

View File

@@ -4,6 +4,8 @@ import (
"encoding/base64"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/ollama/ollama/api"
)
@@ -148,3 +150,71 @@ func TestNewError(t *testing.T) {
}
}
}
func TestToToolCallsPreservesIDs(t *testing.T) {
original := []api.ToolCall{
{
ID: "call_abc123",
Function: api.ToolCallFunction{
Index: 2,
Name: "get_weather",
Arguments: api.ToolCallFunctionArguments{
"location": "Seattle",
},
},
},
{
ID: "call_def456",
Function: api.ToolCallFunction{
Index: 7,
Name: "get_time",
Arguments: api.ToolCallFunctionArguments{
"timezone": "UTC",
},
},
},
}
toolCalls := make([]api.ToolCall, len(original))
copy(toolCalls, original)
got := ToToolCalls(toolCalls)
if len(got) != len(original) {
t.Fatalf("expected %d tool calls, got %d", len(original), len(got))
}
expected := []ToolCall{
{
ID: "call_abc123",
Type: "function",
Index: 2,
Function: struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}{
Name: "get_weather",
Arguments: `{"location":"Seattle"}`,
},
},
{
ID: "call_def456",
Type: "function",
Index: 7,
Function: struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
}{
Name: "get_time",
Arguments: `{"timezone":"UTC"}`,
},
},
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("tool calls mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(original, toolCalls); diff != "" {
t.Errorf("input tool calls mutated (-want +got):\n%s", diff)
}
}