06. 使用模板进行交互

本文代码

提示模板

创建LangChain模板,根据实际需求动态选择不同的输入

使用模板, 我们让模型将 文本 从 某个语言 转换为 另一个语言, 使用模板我们只需要对 文本,转换前语言 和 被转后语言做指定修改就可以了, 比如下方我希望从英文转换为中文 如果转换完以后还需要将英文转换成其他语言,只需要修改 outputLang 的值就可以了, 这样我们就不用单独为他重新写一次对话信息

go 复制代码
package main

import (
    "context"
    "fmt"
    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/prompts"
    "log"
    "study_langchain/pkg/mllm"
)

func main() {
    client, err := mllm.NewLLM("qwen2.5:3b", "http://127.0.0.1:11434")
    if err != nil {
       log.Fatalf("llm初始化失败:%s", err.Error())
    }
    
    // 使用模板进行对话
    template := prompts.NewChatPromptTemplate([]prompts.MessageFormatter{
       prompts.NewSystemMessagePromptTemplate("你是一个翻译人员,只翻译文本,不进行解释", nil),
       prompts.NewHumanMessagePromptTemplate("将此文本从{{.inputLang}}转换为{{.outputLang}}:\n{{.input}}", []string{"inputLang", "outputLang", "input"}),
    })
    
    // 对模板中的值进行替换
    value, err := template.FormatPrompt(map[string]any{
       "inputLang":  "English",
       "outputLang": "Chinese",
       "input":      "I love programming",
    })
    if err != nil {
       log.Fatal(err.Error())
    }

    msg := make([]llms.MessageContent, 0, len(value.Messages()))
    for _, v := range value.Messages() {
       msg = append(msg, llms.MessageContent{Role: v.GetType(), Parts: []llms.ContentPart{llms.TextPart(v.GetContent())}})
    }

    res, err := client.LLM.GenerateContent(context.Background(), msg)
    if err != nil {
       log.Fatal(err.Error())
    }
    fmt.Println(res.Choices[0].Content)
}

案例模板和输出解析

FewShotPrompt:少样本提示模板, 通过示例的展示来教模型如何回答问题

我们想要模型返回的数据能够解析成想要的数据结构,我们可以给模型提供一些案例,让他通过案例学习后回答给我们对应的结构。如下展示

你是一个翻译人员,只翻译文本,不对文本进行解释。

例:

将此文本从English转换为Chinese:

I love programming

```json

{"text":"我爱编程"}

```

这样,模型返回的数据我们就可以通过langChain提供的解析器进行解析

下面就让我们来尝试一下

go 复制代码
package main

import (
    "context"
    "fmt"
    "github.com/tmc/langchaingo/outputparser"
    "github.com/tmc/langchaingo/prompts"
    "log"
    "study_langchain/pkg/mllm"
)

type Trans struct {
    Text string `json:"text" describe:"翻译后文本"`
}

func main() {
    examplePrompt := prompts.NewPromptTemplate("例:\n将此文本从{{.inputLang}}转换为{{.outputLang}}:\n{{.input}}\n```json\n {"text":"{{.output}}"} \n```", []string{"inputLang", "outputLang", "input", "output"})
    examples := []map[string]string{{"inputLang": "English", "outputLang": "Chinese", "input": "I love programming", "output": "我爱编程"}}

    p, err := prompts.NewFewShotPrompt(examplePrompt, examples, nil,
       "你是一个翻译人员,只翻译文本,不对文本进行解释。", "请开始你的回答: 将此文本从{{.inputLang}}转换为{{.outputLang}}: {{.question}}",
       []string{"question", "inputLang", "outputLang"}, map[string]interface{}{"type": func() string { return "json" }},
       "\n", prompts.TemplateFormatGoTemplate, true)
    if err != nil {
       log.Fatal(err)
    }

    msg, err := p.Format(map[string]any{"inputLang": "English", "outputLang": "Chinese", "question": "What a nice day today"})
    if err != nil {
       log.Fatal(err)
    }

    client, err := mllm.NewLLM("qwen2.5:3b", "http://127.0.0.1:11434")
    if err != nil {
       log.Fatalf("llm初始化失败:%s", err.Error())
    }

    res, err := client.LLM.Call(context.Background(), msg)
    if err != nil {
       log.Fatal(err.Error())
    }

    // 对返回结果进行解析
    output, err := outputparser.NewDefined(Trans{})
    if err != nil {
       log.Fatal(err.Error())
    }
    trans, err := output.Parse(res)
    if err != nil {
       log.Fatal(err.Error())
    }
    fmt.Println(trans)
}
相关推荐
Anthony_492612 小时前
逻辑清晰地梳理Golang Context
后端·go
Dobby_052 天前
【Go】C++ 转 Go 第(二)天:变量、常量、函数与init函数
vscode·golang·go
光头闪亮亮2 天前
Golang使用gofpdf库和barcode库创建PDF原材料二维码标签【GBK中文或UTF8】及预览和打印
go
光头闪亮亮2 天前
go-fitz库-PDF文件所有页转换到HTML及从HTML中提取图片的示例教程
go
用户855651414462 天前
环信http请求失败排查
go
_码力全开_3 天前
P1005 [NOIP 2007 提高组] 矩阵取数游戏
java·c语言·c++·python·算法·矩阵·go
程序员爱钓鱼3 天前
Python编程实战 · 基础入门篇 | Python程序的运行方式
后端·go
光头闪亮亮4 天前
gozxing库-对图片中多个二维码进行识别的完整示例教程
go
召摇4 天前
在浏览器中无缝运行Go工具:WebAssembly实战指南
后端·面试·go
王中阳Go5 天前
我发现不管是Java还是Golang,懂AI之后,是真吃香!
后端·go·ai编程