Documentation
¶
Overview ¶
Package prompt defines the ChatTemplate component interface for building structured message lists from templates and runtime variables.
Overview ¶
A ChatTemplate takes a variables map and produces a []*schema.Message slice ready to pass to a [model.BaseChatModel]. It is typically the first node in a pipeline, sitting before the ChatModel.
The built-in DefaultChatTemplate supports three template syntaxes:
- FString: {variable} substitution
- GoTemplate: Go's text/template with conditionals and loops
- Jinja2: Jinja2 template syntax
Construction ¶
Use FromMessages to build a template from a list of message templates:
tmpl := prompt.FromMessages(schema.FString,
schema.SystemMessage("You are a helpful assistant."),
schema.UserMessage("Answer this: {question}"),
)
msgs, err := tmpl.Format(ctx, map[string]any{"question": "What is eino?"})
Use schema.MessagesPlaceholder to insert a dynamic list of messages (e.g. conversation history) at a fixed position in the template:
tmpl := prompt.FromMessages(schema.FString,
schema.SystemMessage("You are a helpful assistant."),
schema.MessagesPlaceholder("history", true),
schema.UserMessage("{question}"),
)
Common Pitfall ¶
Variable mismatches (a key present in the template but missing from the variables map) produce a runtime error — there is no compile-time check.
See https://www.cloudwego.io/docs/eino/core_modules/components/chat_template_guide/
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetImplSpecificOptions ¶
GetImplSpecificOptions extracts the implementation specific options from Option list, optionally providing a base options with default values.
Types ¶
type CallbackInput ¶
type CallbackInput struct {
// Variables is the variables for the callback.
Variables map[string]any
// Templates is the templates for the callback.
Templates []schema.MessagesTemplate
// Extra is the extra information for the callback.
Extra map[string]any
}
CallbackInput is the input for the callback.
func ConvCallbackInput ¶
func ConvCallbackInput(src callbacks.CallbackInput) *CallbackInput
ConvCallbackInput converts the callback input to the prompt callback input.
type CallbackOutput ¶
type CallbackOutput struct {
// Result is the result for the callback.
Result []*schema.Message
// Templates is the templates for the callback.
Templates []schema.MessagesTemplate
// Extra is the extra information for the callback.
Extra map[string]any
}
CallbackOutput is the output for the callback.
func ConvCallbackOutput ¶
func ConvCallbackOutput(src callbacks.CallbackOutput) *CallbackOutput
ConvCallbackOutput converts the callback output to the prompt callback output.
type ChatTemplate ¶
type ChatTemplate interface {
Format(ctx context.Context, vs map[string]any, opts ...Option) ([]*schema.Message, error)
}
ChatTemplate formats a variables map into a list of messages for a ChatModel.
Format substitutes the values from vs into the template's message list and returns the resulting []*schema.Message. The exact substitution syntax (FString, GoTemplate, Jinja2) is determined at construction time.
Variable keys present in the template but absent from vs produce a runtime error — there is no compile-time safety. Prefer consistent variable naming across templates and callers.
In a Graph or Chain, ChatTemplate typically precedes ChatModel. Use compose.WithOutputKey to convert the prior node's output into the map[string]any that Format expects.
See FromMessages and schema.MessagesPlaceholder for construction helpers.
type DefaultChatTemplate ¶
type DefaultChatTemplate struct {
// contains filtered or unexported fields
}
DefaultChatTemplate is the default chat template implementation.
func FromMessages ¶
func FromMessages(formatType schema.FormatType, templates ...schema.MessagesTemplate) *DefaultChatTemplate
FromMessages creates a new DefaultChatTemplate from the given templates and format type. eg.
template := prompt.FromMessages(schema.FString, &schema.Message{Content: "Hello, {name}!"}, &schema.Message{Content: "how are you?"})
// in chain, or graph
chain := compose.NewChain[map[string]any, []*schema.Message]()
chain.AppendChatTemplate(template)
func (*DefaultChatTemplate) Format ¶
func (t *DefaultChatTemplate) Format(ctx context.Context, vs map[string]any, _ ...Option) (result []*schema.Message, err error)
Format formats the chat template with the given context and variables.
func (*DefaultChatTemplate) GetType ¶
func (t *DefaultChatTemplate) GetType() string
GetType returns the type of the chat template (Default).
func (*DefaultChatTemplate) IsCallbacksEnabled ¶
func (t *DefaultChatTemplate) IsCallbacksEnabled() bool
IsCallbacksEnabled checks if the callbacks are enabled for the chat template.
type Option ¶
type Option struct {
// contains filtered or unexported fields
}
Option is a call-time option for a ChatTemplate. The built-in DefaultChatTemplate has no common options — this type exists primarily for custom ChatTemplate implementations that need per-call configuration.
func WrapImplSpecificOptFn ¶
WrapImplSpecificOptFn wraps an implementation-specific option function so it can be passed alongside any future standard options. For use by custom ChatTemplate implementors.