第一个聊天ai
This commit is contained in:
31
chat/templates.go
Normal file
31
chat/templates.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudwego/eino/components/prompt"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
var template = prompt.FromMessages(schema.FString,
|
||||
schema.SystemMessage("你是一个{role}。你需要用{style}的语气回答问题。你的目标是帮助程"+
|
||||
"序员保持积极乐观的心态,提供技术建议的同时也要关注他们的心理健康。"),
|
||||
schema.MessagesPlaceholder("chat_history", true),
|
||||
schema.UserMessage("问题: {question}"))
|
||||
|
||||
func GenerateChatMessage(role, style, question string, history []*schema.Message) ([]*schema.Message, error) {
|
||||
// 在这里调用模板
|
||||
messages, err := template.Format(context.Background(), map[string]any{
|
||||
"role": role,
|
||||
"style": style,
|
||||
"question": question,
|
||||
"chat_history": history,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("生成消息失败: %w", err)
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
Reference in New Issue
Block a user