AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


LangChainGo中的提示词工程(Prompt Engineering)

发布日期:2025-03-11 05:06:44 浏览次数: 1560 来源:馬喽编程笔记
推荐语

掌握LangChainGo提示词工程,精准控制大语言模型输出。

核心内容:
1. 提示词工程的定义及其在LangChainGo中的重要性
2. 提示词基础策略及高级技巧详解
3. LangChainGo中基础提示词的实现与示例

杨芳贤
53A创始人/腾讯云(TVP)最具价值专家
在使用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 mainimport (    "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 mainimport (    "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应用的可靠性!

53AI,企业落地大模型首选服务商

产品:场景落地咨询+大模型应用平台+行业解决方案

承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

添加专属顾问

回到顶部

加载中...

扫码咨询