LangChainGo 中的提示詞工程(Prompt Engineering)
在使用 LangchainGo(簡稱 langchaingo)構建智能應用時,提示詞工程(Prompt Engineering) 是一個關鍵環節。它可以幫助我們更精準地引導 LLM(大語言模型)生成符合預期的內容。本文將介紹在 langchaingo 中如何使用提示詞工程。
什麼是提示詞工程?
提示詞工程是人與機器進行溝通的橋樑,更是用戶引導 AI 精準執行任務的關鍵。
提示詞基礎策略
- 明確目標: 用戶需要明確地告訴 AI 應用的任務目標。只有清楚告訴 AI 應用需求完成什麼樣的任務時,才能設計出有效的提示詞。
- 簡潔明確: 用戶提供的提示詞應該簡潔明瞭,應避免使用模糊或者過於複雜的表達。
- 逐步細化: 對於複雜的任務,可以採用逐步細化的策略,即通過一系列簡單的提示詞,分步驟的指導 AI 模型一步步完成整個任務流程。
提示詞高級技巧
-
使用條件語句: 可以在提示詞中使用條件語句引導 AI 模型根據不同的情況完成不同的輸出。
-
利用歷史信息: 在構建交互式 AI 應用時,我們可以將之前的歷史對話或者交互歷史作爲提示詞的一部分,幫助 AI 模型更好地理解上下文,從而生成更加相關和連貫的輸出。
-
動態調整: 根據 AI 應用的反饋和效果,應動態的調整提示詞,以達到最佳的性能。
基礎提示詞(Basic Prompting)
在 langchaingo 中,最簡單的提示詞是一個字符串,我們可以直接將其傳遞給 LLM 進行推理:
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms/ollama"
)
func main() {
llm, err := ollama.New(ollama.WithModel("qwen2:7b"))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
prompt := "請用一句話描述go語言"
res, err := llm.Call(ctx, prompt)
if err != nil {
log.Fatal(err)
}
fmt.Println("AI Answer : ", res)
}
解析
-
ollama.New:初始化 Ollama LLM,使用 WithModel 方法傳入對應的 llama 模型名稱。
-
llm.Call(ctx, prompt):將提示詞傳遞給 LLM,獲取 AI 生成的文本。
運行結果如下所示:
使用模板優化提示詞(Prompt Templates)
在實際應用中,我們往往需要動態構建提示詞。Prompt Template 是一種高效的方式,用於快速生成根據模板定製的提示詞。它適用於那些需要固定模型與動態內容相結合的場景。
例如:
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms/ollama"
"github.com/tmc/langchaingo/prompts"
)
func main() {
llm, err := ollama.New(
ollama.WithModel("qwen2:7b"),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// 定義prompt template提示詞模板
promptTemplate := prompts.NewPromptTemplate(
"請使用一句話描述 {{.topic}}",
[]string{"topic"},
)
// 輸出模板
fmt.Println(promptTemplate.Template)
// 渲染模板
prompt, err := promptTemplate.Format(map[string]any{
"topic": "nim-lang這門語言",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(prompt)
res, err := llm.Call(ctx, prompt)
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
}
解析
-
prompts.NewPromptTemplate():創建一個帶有變量的提示詞模板。
-
Format(map[string]string{"topic": "nim-lang 這門語言"}):動態填充 topic 變量。
-
llm.Call(ctx, prompt):調用 LLM 生成回答。
運行結果如下所示:
除了上面的方法之外我們還可以使用 ChatPromptTemplate,它提供了一種模擬對話流的方式,非常適合創建更加動態和互動性強的文本生成場景。
例子如下所示:
package main
import (
"context"
"fmt"
"log"
"github.com/tmc/langchaingo/llms/ollama"
"github.com/tmc/langchaingo/prompts"
)
func main() {
llm, err := ollama.New(ollama.WithModel("qwen2:7b"))
if err != nil {
log.Fatal(err)
}
// 創建一個新的聊天提示模板
prompt := prompts.NewChatPromptTemplate([]prompts.MessageFormatter{
prompts.NewSystemMessagePromptTemplate("你是一個智能翻譯助手", []string{}),
prompts.NewHumanMessagePromptTemplate("將這段文本從{{.input}}翻譯成{{.output}}:\n{{.question}}",
[]string{"input", "output", "question"}),
})
val, err := prompt.FormatPrompt(map[string]any{
"input": "中文",
"output": "英文",
"question": "你好,世界",
})
// 調用LLM
ctx := context.Background()
res, err := llm.Call(ctx, val.String())
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
}
解析
-
使用 NewChatPromptTemplate 創建對話模板,包含多個 MessageFormatter。
-
NewSystemMessagePromptTemplate 定義系統角色(如客服助手)。
-
NewHumanMessagePromptTemplate 定義用戶輸入模板,{{.question}} 等變量爲佔位符。
運行效果如下所示:
除了上述介紹的方式來創建提示詞外,langchaingo 還提供了其他方法來創建提示詞,這裏我就不一一介紹和演示了。
總結
langchaingo 提供了靈活的提示詞工程工具,幫助我們優化 LLM 的輸入,提高回答質量。合理利用這些技術,可以讓 LLM 更精準地理解和執行任務,提升 AI 應用的可靠性!
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/hWkJ9e2j0xy4lVXPF4cv9g