Merge pull request #5780 from ollama/mxyng/tools

fix parsing tool calls: break on unexpected eofs
This commit is contained in:
Michael Yang 2024-07-18 12:14:10 -07:00 committed by GitHub
commit d0634b1596
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -348,7 +348,7 @@ func (m *Model) parseToolCalls(s string) ([]api.ToolCall, bool) {
for offset := 0; offset < len(s); { for offset := 0; offset < len(s); {
var obj map[string]any var obj map[string]any
decoder := json.NewDecoder(strings.NewReader(s[offset:])) decoder := json.NewDecoder(strings.NewReader(s[offset:]))
if err := decoder.Decode(&obj); errors.Is(err, io.EOF) { if err := decoder.Decode(&obj); errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
break break
} else if syntax := &(json.SyntaxError{}); errors.As(err, &syntax) { } else if syntax := &(json.SyntaxError{}); errors.As(err, &syntax) {
// skip over any syntax errors // skip over any syntax errors
@ -357,6 +357,7 @@ func (m *Model) parseToolCalls(s string) ([]api.ToolCall, bool) {
// skip over any unmarshalable types // skip over any unmarshalable types
offset += int(unmarshalType.Offset) offset += int(unmarshalType.Offset)
} else if err != nil { } else if err != nil {
slog.Error("parseToolCalls", "error", err)
return nil, false return nil, false
} else { } else {
offset += int(decoder.InputOffset()) offset += int(decoder.InputOffset())