-
Notifications
You must be signed in to change notification settings - Fork 8
/
tool.go
133 lines (118 loc) · 4.08 KB
/
tool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gptscript
import (
"fmt"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// ToolDef struct represents a tool with various configurations.
type ToolDef struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
MaxTokens int `json:"maxTokens,omitempty"`
ModelName string `json:"modelName,omitempty"`
ModelProvider bool `json:"modelProvider,omitempty"`
JSONResponse bool `json:"jsonResponse,omitempty"`
Chat bool `json:"chat,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
Cache *bool `json:"cache,omitempty"`
InternalPrompt *bool `json:"internalPrompt"`
Arguments *openapi3.Schema `json:"arguments,omitempty"`
Tools []string `json:"tools,omitempty"`
GlobalTools []string `json:"globalTools,omitempty"`
GlobalModelName string `json:"globalModelName,omitempty"`
Context []string `json:"context,omitempty"`
ExportContext []string `json:"exportContext,omitempty"`
Export []string `json:"export,omitempty"`
Agents []string `json:"agents,omitempty"`
Credentials []string `json:"credentials,omitempty"`
ExportCredentials []string `json:"exportCredentials,omitempty"`
InputFilters []string `json:"inputFilters,omitempty"`
ExportInputFilters []string `json:"exportInputFilters,omitempty"`
OutputFilters []string `json:"outputFilters,omitempty"`
ExportOutputFilters []string `json:"exportOutputFilters,omitempty"`
Instructions string `json:"instructions,omitempty"`
Type string `json:"type,omitempty"`
MetaData map[string]string `json:"metadata,omitempty"`
}
func ToolDefsToNodes(tools []ToolDef) []Node {
nodes := make([]Node, 0, len(tools))
for _, tool := range tools {
nodes = append(nodes, Node{
ToolNode: &ToolNode{
Tool: Tool{
ToolDef: tool,
},
},
})
}
return nodes
}
func ObjectSchema(kv ...string) *openapi3.Schema {
s := &openapi3.Schema{
Type: &openapi3.Types{"object"},
Properties: openapi3.Schemas{},
}
for i, v := range kv {
if i%2 == 1 {
s.Properties[kv[i-1]] = &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: v,
Type: &openapi3.Types{"string"},
},
}
}
}
return s
}
type Document struct {
Nodes []Node `json:"nodes,omitempty"`
}
type Node struct {
TextNode *TextNode `json:"textNode,omitempty"`
ToolNode *ToolNode `json:"toolNode,omitempty"`
}
type TextNode struct {
Fmt string `json:"fmt,omitempty"`
Text string `json:"text,omitempty"`
}
func (n *TextNode) combine() {
if n != nil && n.Fmt != "" {
n.Text = fmt.Sprintf("!%s\n%s", n.Fmt, n.Text)
n.Fmt = ""
}
}
func (n *TextNode) process() {
if n != nil && strings.HasPrefix(n.Text, "!") {
n.Fmt, n.Text, _ = strings.Cut(strings.TrimPrefix(n.Text, "!"), "\n")
}
}
type ToolNode struct {
Fmt string `json:"fmt,omitempty"`
Tool Tool `json:"tool,omitempty"`
}
type Tool struct {
ToolDef `json:",inline"`
ID string `json:"id,omitempty"`
ToolMapping map[string][]ToolReference `json:"toolMapping,omitempty"`
LocalTools map[string]string `json:"localTools,omitempty"`
Source ToolSource `json:"source,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
}
type ToolReference struct {
Named string `json:"named,omitempty"`
Reference string `json:"reference,omitempty"`
Arg string `json:"arg,omitempty"`
ToolID string `json:"toolID,omitempty"`
}
type ToolSource struct {
Location string `json:"location,omitempty"`
LineNo int `json:"lineNo,omitempty"`
Repo *Repo `json:"repo,omitempty"`
}
type Repo struct {
VCS string
Root string
Path string
Name string
Revision string
}