Skip to content

Commit

Permalink
Merge pull request #635 from nkwangleiGIT/main
Browse files Browse the repository at this point in the history
fix potential issue in the context of prompt
  • Loading branch information
bjwswang authored Jan 25, 2024
2 parents de79064 + 64bafd9 commit 528f1de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/appruntime/chain/llmchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (l *LLMChain) Run(ctx context.Context, cli dynamic.Interface, args map[stri
// Add the answer to the context if it's not empty
if args["_answer"] != nil {
klog.Infoln("get answer from upstream:", args["_answer"])
args["context"] = args["_answer"]
args["context"] = fmt.Sprintf("%s\n%s", args["context"], args["_answer"])
}
args = runTools(ctx, args, instance.Spec.Tools)
chain := chains.NewLLMChain(llm, prompt)
Expand Down
13 changes: 10 additions & 3 deletions pkg/appruntime/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package prompt
import (
"context"
"fmt"
"strings"

"github.com/tmc/langchaingo/prompts"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -30,6 +31,10 @@ import (
"github.com/kubeagi/arcadia/pkg/appruntime/base"
)

const (
promptContextPlaceholder = "{{.context}}"
)

type Prompt struct {
base.BaseNode
prompts.ChatPromptTemplate
Expand Down Expand Up @@ -58,9 +63,11 @@ func (p *Prompt) Run(ctx context.Context, cli dynamic.Interface, args map[string
ps = append(ps, prompts.NewSystemMessagePromptTemplate(instance.Spec.SystemMessage, []string{}))
}
if instance.Spec.UserMessage != "" {
// Add the context by default, and leave it empty
// so we can add more contexts as needed in all agents/chains
instance.Spec.UserMessage = fmt.Sprintf("{{.context}}\n%s", instance.Spec.UserMessage)
if !strings.Contains(instance.Spec.UserMessage, promptContextPlaceholder) {
// Add the context by default if it does not exist, and leave it empty
// so we can add more contexts as needed in all agents/chains
instance.Spec.UserMessage = fmt.Sprintf("%s\n%s", promptContextPlaceholder, instance.Spec.UserMessage)
}
ps = append(ps, prompts.NewHumanMessagePromptTemplate(instance.Spec.UserMessage, []string{"question"}))
}
template := prompts.NewChatPromptTemplate(ps)
Expand Down

0 comments on commit 528f1de

Please sign in to comment.