-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathprompt.go
53 lines (46 loc) · 1.65 KB
/
prompt.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
package system
import (
"encoding/json"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// Suffix is default suffix of gptscript files
const Suffix = ".gpt"
// DefaultPromptParameter is used as the key in a json map to indication that we really wanted
// to just send pure text but the interface required JSON (as that is the fundamental interface of tools in OpenAI)
var DefaultPromptParameter = "defaultPromptParameter"
var DefaultToolSchema = openapi3.Schema{
Type: &openapi3.Types{"object"},
Properties: openapi3.Schemas{
DefaultPromptParameter: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: "Prompt to send to the tool. This may be an instruction or question.",
Type: &openapi3.Types{"string"},
},
},
},
}
var DefaultChatSchema = openapi3.Schema{
Type: &openapi3.Types{"object"},
Properties: openapi3.Schemas{
DefaultPromptParameter: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: "Prompt to send to the assistant. This may be an instruction or question.",
Type: &openapi3.Types{"string"},
},
},
},
}
// IsDefaultPrompt Checks if the content is a json blob that has the defaultPromptParameter in it. If so
// it will extract out the value and return it. If not it will return the original content as is and false.
func IsDefaultPrompt(content string) (string, bool) {
if strings.Contains(content, DefaultPromptParameter) && strings.HasPrefix(content, "{") {
data := map[string]any{}
if err := json.Unmarshal([]byte(content), &data); err == nil && len(data) == 1 {
if v, _ := data[DefaultPromptParameter].(string); v != "" {
return v, true
}
}
}
return content, false
}