forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_template.go
91 lines (75 loc) · 2.71 KB
/
prompt_template.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
package prompts
import (
"errors"
"fmt"
"github.com/tmc/langchaingo/schema"
)
var (
// ErrInputVariableReserved is returned when there is a conflict with a reserved variable name.
ErrInputVariableReserved = errors.New("conflict with reserved variable name")
// ErrInvalidPartialVariableType is returned when the partial variable is not a string or a function.
ErrInvalidPartialVariableType = errors.New("invalid partial variable type")
)
// PromptTemplate contains common fields for all prompt templates.
type PromptTemplate struct {
// Template is the prompt template.
Template string
// A list of variable names the prompt template expects.
InputVariables []string
// TemplateFormat is the format of the prompt template.
TemplateFormat TemplateFormat
// OutputParser is a function that parses the output of the prompt template.
OutputParser schema.OutputParser[any]
// PartialVariables represents a map of variable names to values or functions that return values.
// If the value is a function, it will be called when the prompt template is rendered.
PartialVariables map[string]any
}
// NewPromptTemplate returns a new prompt template.
func NewPromptTemplate(template string, inputVars []string) PromptTemplate {
return PromptTemplate{
Template: template,
InputVariables: inputVars,
TemplateFormat: TemplateFormatGoTemplate,
}
}
var (
_ Formatter = PromptTemplate{}
_ FormatPrompter = PromptTemplate{}
)
// Format formats the prompt template and returns a string value.
func (p PromptTemplate) Format(values map[string]any) (string, error) {
resolvedValues, err := resolvePartialValues(p.PartialVariables, values)
if err != nil {
return "", err
}
return RenderTemplate(p.Template, p.TemplateFormat, resolvedValues)
}
// FormatPrompt formats the prompt template and returns a string prompt value.
func (p PromptTemplate) FormatPrompt(values map[string]any) (schema.PromptValue, error) { //nolint:ireturn
f, err := p.Format(values)
if err != nil {
return nil, err
}
return StringPromptValue(f), nil //nolint:ireturn
}
// GetInputVariables returns the input variables the prompt expect.
func (p PromptTemplate) GetInputVariables() []string {
return p.InputVariables
}
func resolvePartialValues(partialValues map[string]any, values map[string]any) (map[string]any, error) {
resolvedValues := make(map[string]any)
for variable, value := range partialValues {
switch value := value.(type) {
case string:
resolvedValues[variable] = value
case func() string:
resolvedValues[variable] = value()
default:
return nil, fmt.Errorf("%w: %v", ErrInvalidPartialVariableType, variable)
}
}
for variable, value := range values {
resolvedValues[variable] = value
}
return resolvedValues, nil
}