使用LangChain构建问答聊天机器人案例实战(三)

使用LangChain构建问答聊天机器人案例实战

LangChain开发全流程剖析
接下来,我们再回到“get_prompt()”方法。在这个方法中,有系统提示词(system prompts)和用户提示词(user prompts),这是从相应的文件中读取的,从“system.prompt”文件中读取系统提示词(system_template),从“user.prompt”文件中读取用户提示词(human_template)。在第7行中,我们定义了两个输入变量:“query”和“df_info”。然后,将这些提示词信息和输入变量封装在一个“ChatPromptTemplate”对象中,会调用“SystemMessagePromptTemplate.from_template()”方法和“HumanMessagePromptTemplate.from_template()”方法,并返回该对象。
其中,from_template方法是BaseStringMessagePromptTemplate的一个类方法,这个方法接受一个字符串模板“template”和一个字符串“template_format”,还有其他的关键字参数。这个方法返回一个“MessagePromptTemplateT”类型的对象。我们使用“PromptTemplate.from_template()”方法从字符串模板中创建一个PromptTemplate对象。然后,使用这个对象作为参数来创建一个“MessagePromptTemplateT”类型的对象。这个对象将包含从模板中解析出来的内容,这个源码本身非常简单。Gavin大咖微信:NLP_Matrix_Space
chat.py的from_template方法的代码实现:

 

猜你喜欢

转载自blog.csdn.net/duan_zhihua/article/details/132003405