在 Blades 框架中,Prompt 是与大型语言模型(LLM)交互的核心组件。它表示用户与助手之间交换的消息序列,支持多种类型的消息(系统消息、用户消息、助手消息)以及模板化功能,使开发者能够构建动态和上下文感知的 AI 应用程序。
Prompt 是一个包含多个消息的消息序列容器。
type Prompt struct { Messages []*Message `json:"messages"`}Blades 支持多种消息类型,每种都有特定的角色:
最基本的 Prompt 创建方式是直接指定消息内容:
prompt := blades.NewPrompt( blades.UserMessage("What is the capital of France?"),)可以通过添加系统消息来为模型提供上下文和指令:
prompt := blades.NewPrompt( blades.SystemMessage("You are a helpful assistant that provides detailed and accurate information."), blades.UserMessage("What is the capital of France?"),)Blades 提供了强大的模板功能,允许动态生成 Prompt 内容。使用 PromptTemplate 构建器可以创建带有变量的模板化 Prompt:
params := map[string]any{ "topic": "The Future of Artificial Intelligence", "audience": "General reader",}
prompt, err := blades.NewPromptTemplate(). System("Please summarize {{.topic}} in three key points.", params). User("Respond concisely and accurately for a {{.audience}} audience.", params). Build()if err != nil { log.Fatal(err)}// Create a new sessionsession := blades.NewSession("conversation_123", map[string]any{ "style": "robot",})
ctx := blades.NewSessionContext(context.Background(), session)
// 在模板中使用 session 变量prompt, err := blades.NewPromptTemplate(). System("Respond as a {{.style}}.", nil). User("Tell me a joke.", nil). BuildContext(ctx)Blades的Prompt还支持多种功能,如多部分消息和流式处理等。
Blades 支持包含多种内容类型的消息,包括文本、文件引用和二进制数据:
message := blades.UserMessage( blades.TextPart{Text: "Analyze this image:"}, blades.FilePart{ Name: "chart.png", URI: "file:///path/to/chart.png", MIMEType: "image/png", },)Prompt 可以与流式处理结合使用,实现实时响应:
stream, err := agent.RunStream(context.Background(), prompt)if err != nil { log.Fatal(err)}defer stream.Close()
for stream.Next() { message, err := stream.Current() if err != nil { log.Printf("Error: %v", err) continue } fmt.Print(message.Text())}package main
import ( "context" "log"
"github.com/go-kratos/blades" "github.com/go-kratos/blades/contrib/openai")
func main() { agent := blades.NewAgent( "Basic Agent", blades.WithModel("gpt-5"), blades.WithProvider(openai.NewChatProvider()), blades.WithInstructions("You are a helpful assistant that provides detailed and accurate information."), )
prompt := blades.NewPrompt( blades.UserMessage("What is the capital of France?"), )
result, err := agent.Run(context.Background(), prompt) if err != nil { log.Fatal(err) }
log.Println(result.Text())}package main
import ( "context" "log"
"github.com/go-kratos/blades" "github.com/go-kratos/blades/contrib/openai")
func main() { agent := blades.NewAgent( "Template Agent", blades.WithModel("gpt-5"), blades.WithProvider(openai.NewChatProvider()), )
// Define templates and params params := map[string]any{ "topic": "The Future of Artificial Intelligence", "audience": "General reader", }
// Build prompt using the template builder prompt, err := blades.NewPromptTemplate(). System("Please summarize {{.topic}} in three key points.", params). User("Respond concisely and accurately for a {{.audience}} audience.", params). Build() if err != nil { log.Fatal(err) }
log.Println("Generated Prompt:", prompt.String())
// Run the agent with the templated prompt result, err := agent.Run(context.Background(), prompt) if err != nil { log.Fatal(err) }
log.Println(result.Text())}