mirror of
https://github.com/likelovewant/ollama-for-amd.git
synced 2025-12-21 14:26:30 +00:00
Add Tool Call ID (#12956)
* routes/types: add tool call id --------- Co-authored-by: ParthSareen <parth.sareen@ollama.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
@@ -1812,6 +1813,15 @@ func (s *Server) PsHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, api.ProcessResponse{Models: models})
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
func (s *Server) ChatHandler(c *gin.Context) {
|
||||
checkpointStart := time.Now()
|
||||
|
||||
@@ -2130,6 +2140,9 @@ func (s *Server) ChatHandler(c *gin.Context) {
|
||||
|
||||
res.Message.Content = content
|
||||
res.Message.Thinking = thinking
|
||||
for i := range toolCalls {
|
||||
toolCalls[i].ID = toolCallId()
|
||||
}
|
||||
res.Message.ToolCalls = toolCalls
|
||||
|
||||
tb.WriteString(thinking)
|
||||
@@ -2174,6 +2187,9 @@ func (s *Server) ChatHandler(c *gin.Context) {
|
||||
if len(content) > 0 {
|
||||
res.Message.Content = content
|
||||
} else if len(toolCalls) > 0 {
|
||||
for i := range toolCalls {
|
||||
toolCalls[i].ID = toolCallId()
|
||||
}
|
||||
res.Message.ToolCalls = toolCalls
|
||||
res.Message.Content = ""
|
||||
} else if res.Message.Thinking != "" {
|
||||
|
||||
@@ -554,6 +554,14 @@ func TestGenerateChat(t *testing.T) {
|
||||
t.Error("expected tool calls, got nil")
|
||||
}
|
||||
|
||||
gotToolCall := resp.Message.ToolCalls[0]
|
||||
if gotToolCall.ID == "" {
|
||||
t.Error("expected tool call ID to be populated")
|
||||
}
|
||||
if !strings.HasPrefix(gotToolCall.ID, "call_") {
|
||||
t.Errorf("expected tool call ID to have call_ prefix, got %q", gotToolCall.ID)
|
||||
}
|
||||
|
||||
expectedToolCall := api.ToolCall{
|
||||
Function: api.ToolCallFunction{
|
||||
Name: "get_weather",
|
||||
@@ -564,7 +572,8 @@ func TestGenerateChat(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(resp.Message.ToolCalls[0], expectedToolCall); diff != "" {
|
||||
expectedToolCall.ID = gotToolCall.ID
|
||||
if diff := cmp.Diff(gotToolCall, expectedToolCall); diff != "" {
|
||||
t.Errorf("tool call mismatch (-got +want):\n%s", diff)
|
||||
}
|
||||
})
|
||||
@@ -669,6 +678,17 @@ func TestGenerateChat(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(resp.Message.ToolCalls) > 0 {
|
||||
for _, call := range resp.Message.ToolCalls {
|
||||
if call.ID == "" {
|
||||
t.Fatal("expected streaming tool call to have an ID")
|
||||
}
|
||||
if !strings.HasPrefix(call.ID, "call_") {
|
||||
t.Fatalf("expected streaming tool call ID to have call_ prefix, got %q", call.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if resp.Done {
|
||||
if len(resp.Message.ToolCalls) != 1 {
|
||||
t.Errorf("expected 1 tool call in final response, got %d", len(resp.Message.ToolCalls))
|
||||
@@ -687,6 +707,14 @@ func TestGenerateChat(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
if finalToolCall.ID == "" {
|
||||
t.Fatal("expected final tool call to have an ID")
|
||||
}
|
||||
if !strings.HasPrefix(finalToolCall.ID, "call_") {
|
||||
t.Fatalf("expected final tool call ID to have call_ prefix, got %q", finalToolCall.ID)
|
||||
}
|
||||
|
||||
expectedToolCall.ID = finalToolCall.ID
|
||||
if diff := cmp.Diff(finalToolCall, expectedToolCall); diff != "" {
|
||||
t.Errorf("final tool call mismatch (-got +want):\n%s", diff)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user