marshal json automatically for some template values (#5758)

This commit is contained in:
Michael Yang 2024-07-17 15:35:11 -07:00 committed by GitHub
parent b23424bb3c
commit b255445557
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 52 deletions

View file

@ -311,12 +311,14 @@ func (m *Model) parseToolCalls(s string) ([]api.ToolCall, bool) {
}
var b bytes.Buffer
if err := tmpl.Execute(&b, map[string][]map[string]any{
if err := tmpl.Execute(&b, map[string][]api.ToolCall{
"ToolCalls": {
{
"Function": map[string]any{
"Name": "@@name@@",
"Arguments": "@@arguments@@",
Function: api.ToolCallFunction{
Name: "@@name@@",
Arguments: api.ToolCallFunctionArguments{
"@@argument@@": 1,
},
},
},
},
@ -324,7 +326,7 @@ func (m *Model) parseToolCalls(s string) ([]api.ToolCall, bool) {
return nil, false
}
var kv map[string]string
var kv map[string]any
// execute the subtree with placeholders to identify the keys
// trim any commands that might exist in the template
if err := json.Unmarshal(bytes.TrimSuffix(b.Bytes(), []byte(",")), &kv); err != nil {
@ -334,10 +336,10 @@ func (m *Model) parseToolCalls(s string) ([]api.ToolCall, bool) {
// find the keys that correspond to the name and arguments fields
var name, arguments string
for k, v := range kv {
switch v {
case "@@name@@":
switch v.(type) {
case string:
name = k
case "@@arguments@@":
case map[string]any:
arguments = k
}
}