Skip to content

Commit

Permalink
Merge pull request #1157 from mattjoyce/curly-brace-templates
Browse files Browse the repository at this point in the history
fix: process template variables in raw input
  • Loading branch information
eugeis authored Nov 24, 2024
2 parents 7732b6f + af16494 commit 4ed512b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions core/chatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/plugins/ai"
"github.com/danielmiessler/fabric/plugins/db/fsdb"
"github.com/danielmiessler/fabric/plugins/template"
)

const NoSessionPatternUserMessages = "no session, pattern or user messages provided"
Expand Down Expand Up @@ -74,6 +75,7 @@ func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (s


func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *fsdb.Session, err error) {
// If a session name is provided, retrieve it from the database
if request.SessionName != "" {
var sess *fsdb.Session
if sess, err = o.db.Sessions.Get(request.SessionName); err != nil {
Expand All @@ -89,6 +91,7 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
session.Append(&goopenai.ChatCompletionMessage{Role: common.ChatMessageRoleMeta, Content: request.Meta})
}

// if a context name is provided, retrieve it from the database
var contextContent string
if request.ContextName != "" {
var ctx *fsdb.Context
Expand All @@ -100,15 +103,20 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
}


//if there is no input from stdin
var messageContent string
// Process any template variables in the message content (user input)
// Double curly braces {{variable}} indicate template substitution
// should occur, whether in patterns or direct input
if request.Message != nil {
messageContent = request.Message.Content
request.Message.Content, err = template.ApplyTemplate(request.Message.Content, request.PatternVariables, "")
if err != nil {
return nil, err
}
}

var patternContent string
if request.PatternName != "" {
pattern, err := o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables, messageContent)
pattern, err := o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables, request.Message.Content)
// pattrn will now contain user input, and all variables will be resolved, or errored

if err != nil {
return nil, fmt.Errorf("could not get pattern %s: %v", request.PatternName, err)
Expand All @@ -125,7 +133,8 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
if raw {
if request.Message != nil {
if systemMessage != "" {
request.Message.Content = systemMessage + request.Message.Content
request.Message.Content = systemMessage
// system contains pattern which contains user input
}
} else {
if systemMessage != "" {
Expand Down
Binary file removed plugins/template/fabric
Binary file not shown.

0 comments on commit 4ed512b

Please sign in to comment.