forked from teilomillet/gollm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
145 lines (121 loc) · 3.85 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
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
134
135
136
137
138
139
140
141
142
143
144
145
// File: prompt.go
package gollm
import (
"fmt"
"strings"
"github.com/invopop/jsonschema"
"github.com/xstarts/gollm/internal/llm"
)
// Prompt represents a structured prompt for an LLM
type Prompt struct {
Input string `json:"input" jsonschema:"required,description=The main input text for the LLM" validate:"required"`
Output string `json:"output,omitempty" jsonschema:"description=Specification for the expected output format"`
Directives []string `json:"directives,omitempty" jsonschema:"description=List of directives to guide the LLM"`
Context string `json:"context,omitempty" jsonschema:"description=Additional context for the LLM"`
MaxLength int `json:"maxLength,omitempty" jsonschema:"minimum=1,description=Maximum length of the response in words" validate:"omitempty,min=1"`
Examples []string `json:"examples,omitempty" jsonschema:"description=List of examples to guide the LLM"`
}
// SchemaOption is a function type for schema generation options
type SchemaOption func(*jsonschema.Reflector)
// WithExpandedStruct sets the ExpandedStruct option for schema generation
func WithExpandedStruct(expanded bool) SchemaOption {
return func(r *jsonschema.Reflector) {
r.ExpandedStruct = expanded
}
}
// Validate checks if the Prompt is valid according to its validation rules
func (p *Prompt) Validate() error {
return llm.Validate(p)
}
// GenerateJSONSchema returns the JSON Schema for the Prompt struct
func (p *Prompt) GenerateJSONSchema(opts ...SchemaOption) ([]byte, error) {
reflector := &jsonschema.Reflector{}
for _, opt := range opts {
opt(reflector)
}
schema := reflector.Reflect(p)
return schema.MarshalJSON()
}
// PromptOption is a function type that modifies a Prompt
type PromptOption func(*Prompt)
// NewPrompt creates a new Prompt with the given input and applies any provided options
func NewPrompt(input string, opts ...PromptOption) *Prompt {
p := &Prompt{
Input: input,
}
for _, opt := range opts {
opt(p)
}
return p
}
// WithDirectives adds directives to the Prompt
func WithDirectives(directives ...string) PromptOption {
return func(p *Prompt) {
p.Directives = append(p.Directives, directives...)
}
}
// WithOutput adds an output specification to the Prompt
func WithOutput(output string) PromptOption {
return func(p *Prompt) {
p.Output = output
}
}
// WithContext adds context to the Prompt
func WithContext(context string) PromptOption {
return func(p *Prompt) {
p.Context = context
}
}
// WithMaxLength sets the maximum length for the output
func WithMaxLength(length int) PromptOption {
return func(p *Prompt) {
p.MaxLength = length
}
}
// WithExamples adds examples to the Prompt
func WithExamples(examples ...string) PromptOption {
return func(p *Prompt) {
p.Examples = append(p.Examples, examples...)
}
}
// Apply applies the given options to the Prompt
func (p *Prompt) Apply(opts ...PromptOption) {
for _, opt := range opts {
opt(p)
}
}
// String returns the formatted prompt as a string
func (p *Prompt) String() string {
var builder strings.Builder
if p.Context != "" {
builder.WriteString("Context: ")
builder.WriteString(p.Context)
builder.WriteString("\n\n")
}
if len(p.Directives) > 0 {
builder.WriteString("Directives:\n")
for _, d := range p.Directives {
builder.WriteString("- ")
builder.WriteString(d)
builder.WriteString("\n")
}
builder.WriteString("\n")
}
builder.WriteString(p.Input)
if p.Output != "" {
builder.WriteString("\n\nExpected Output Format:\n")
builder.WriteString(p.Output)
}
if len(p.Examples) > 0 {
builder.WriteString("\n\nExamples:\n")
for _, example := range p.Examples {
builder.WriteString("- ")
builder.WriteString(example)
builder.WriteString("\n")
}
}
if p.MaxLength > 0 {
builder.WriteString(fmt.Sprintf("\n\nPlease limit your response to approximately %d words.", p.MaxLength))
}
return builder.String()
}