code.oscarkilo.com/klex-git

Hash:
27bf6395e8809981ff065d8055105bfc4b770684
Author:
Igor Naverniouk <[email protected]>
Date:
Mon Oct 7 19:53:59 2024 -0700
Message:
JSON schema for tools
diff --git a/api/messages.go b/api/messages.go
index cc0f44a..77df339 100644
--- a/api/messages.go
+++ b/api/messages.go
@@ -11,6 +11,7 @@ type MessagesRequest struct {
MaxTokens int `json:"max_tokens"`
System string `json:"system,omitempty"`
Temperature float64 `json:"temperature"`
+ Tools []Tool `json:"tools,omitempty"`
}

type ContentBlock struct {
@@ -57,3 +58,19 @@ type MessagesResponse struct {
Usage Usage `json:"usage"`
Error *ErrorResponse `json:"error"`
}
+
+type Tool struct {
+ Name string `json:"name"`
+ Description string `json:"description"`
+ InputSchema JSONSchema `json:"input_schema"`
+}
+
+// JSONSchema is the subset of json-shcema.org that we need.
+type JSONSchema struct {
+ Type string `json:"type"`
+ Properties map[string]JSONSchema `json:"properties,omitempty"`
+ Items *JSONSchema `json:"items,omitempty"`
+ AdditionalProperties bool `json:"additionalProperties,omitempty"`
+ UnevaluatedProperties bool `json:"unevaluatedProperties,omitempty"`
+ Requeired []string `json:"required,omitempty"`
+}
a/api/messages.go
b/api/messages.go
1
package api
1
package api
2
2
3
type ChatMessage struct {
3
type ChatMessage struct {
4
Role string `json:"role"`
4
Role string `json:"role"`
5
Content []ContentBlock `json:"content"`
5
Content []ContentBlock `json:"content"`
6
}
6
}
7
7
8
type MessagesRequest struct {
8
type MessagesRequest struct {
9
Model string `json:"model"`
9
Model string `json:"model"`
10
Messages []ChatMessage `json:"messages"`
10
Messages []ChatMessage `json:"messages"`
11
MaxTokens int `json:"max_tokens"`
11
MaxTokens int `json:"max_tokens"`
12
System string `json:"system,omitempty"`
12
System string `json:"system,omitempty"`
13
Temperature float64 `json:"temperature"`
13
Temperature float64 `json:"temperature"`
14
Tools []Tool `json:"tools,omitempty"`
14
}
15
}
15
16
16
type ContentBlock struct {
17
type ContentBlock struct {
17
// Type is "text", "image", "tool_use", or "tool_result".
18
// Type is "text", "image", "tool_use", or "tool_result".
18
Type string `json:"type"`
19
Type string `json:"type"`
19
Text string `json:"text"`
20
Text string `json:"text"`
20
Source *ContentSource `json:"source"`
21
Source *ContentSource `json:"source"`
21
}
22
}
22
23
23
type ContentSource struct {
24
type ContentSource struct {
24
// Type can only be "base64".
25
// Type can only be "base64".
25
Type string `json:"type"`
26
Type string `json:"type"`
26
27
27
// MediaType can be one of:
28
// MediaType can be one of:
28
// - "image/jpeg",
29
// - "image/jpeg",
29
// - "image/png",
30
// - "image/png",
30
// - "image/gif", or
31
// - "image/gif", or
31
// - "image/webp".
32
// - "image/webp".
32
MediaType string `json:"media_type,omitempty"`
33
MediaType string `json:"media_type,omitempty"`
33
34
34
Data string `json:"data,omitempty"`
35
Data string `json:"data,omitempty"`
35
}
36
}
36
37
37
type Usage struct {
38
type Usage struct {
38
InputTokens int `json:"input_tokens"`
39
InputTokens int `json:"input_tokens"`
39
CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
40
CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
40
CacheReadInputTokens *int `json:"cache_read_input_tokens"`
41
CacheReadInputTokens *int `json:"cache_read_input_tokens"`
41
OutputTokens int `json:"output_tokens"`
42
OutputTokens int `json:"output_tokens"`
42
}
43
}
43
44
44
type ErrorResponse struct {
45
type ErrorResponse struct {
45
Type string `json:"type"`
46
Type string `json:"type"`
46
Message string `json:"message"`
47
Message string `json:"message"`
47
}
48
}
48
49
49
type MessagesResponse struct {
50
type MessagesResponse struct {
50
Id string `json:"id"`
51
Id string `json:"id"`
51
Type string `json:"type"`
52
Type string `json:"type"`
52
Role string `json:"role"`
53
Role string `json:"role"`
53
Content []ContentBlock `json:"content"`
54
Content []ContentBlock `json:"content"`
54
Model string `json:"model"`
55
Model string `json:"model"`
55
StopReason string `json:"stop_reason"`
56
StopReason string `json:"stop_reason"`
56
StopSequence bool `json:"stop_sequence"`
57
StopSequence bool `json:"stop_sequence"`
57
Usage Usage `json:"usage"`
58
Usage Usage `json:"usage"`
58
Error *ErrorResponse `json:"error"`
59
Error *ErrorResponse `json:"error"`
59
}
60
}
61
62
type Tool struct {
63
Name string `json:"name"`
64
Description string `json:"description"`
65
InputSchema JSONSchema `json:"input_schema"`
66
}
67
68
// JSONSchema is the subset of json-shcema.org that we need.
69
type JSONSchema struct {
70
Type string `json:"type"`
71
Properties map[string]JSONSchema `json:"properties,omitempty"`
72
Items *JSONSchema `json:"items,omitempty"`
73
AdditionalProperties bool `json:"additionalProperties,omitempty"`
74
UnevaluatedProperties bool `json:"unevaluatedProperties,omitempty"`
75
Requeired []string `json:"required,omitempty"`
76
}