ChatPromptTemplate和AI Message的用法

ChatPromptTemplate的用法

用法1:

python 复制代码
from langchain.chains import LLMChain
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.chains import LLMMathChain

prompt= ChatPromptTemplate.from_template("tell me the weather of {topic}")
str = prompt.format(topic="shenzhen")
print(str)

打印出:

bash 复制代码
Human: tell me the weather of shenzhen

最终和llm一起使用:

python 复制代码
import ChatGLM
from langchain.chains import LLMChain
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.chains import LLMMathChain


prompt = ChatPromptTemplate.from_template("who is {name}")
# str = prompt.format(name="Bill Gates")
# print(str)
llm = ChatGLM.ChatGLM_LLM()
output_parser = StrOutputParser()
chain05 = prompt| llm | output_parser
print(chain05.invoke({"name": "Bill Gates"}))

用法2:

python 复制代码
import ChatGLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
                ("system", "You are a helpful AI bot. Your name is {name}."),
                ("human", "Hello, how are you doing?"),
                ("ai", "I'm doing well, thanks!"),
                ("human", "{user_input}"),
            ])

llm = ChatGLM.ChatGLM_LLM()
output_parser = StrOutputParser()
chain05 = prompt| llm | output_parser
print(chain05.invoke({"name": "Bob","user_input": "What is your name"}))

也可以这样:

python 复制代码
import ChatGLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

llm = ChatGLM.ChatGLM_LLM()

prompt = ChatPromptTemplate.from_messages([
                ("system", "You are a helpful AI bot. Your name is {name}."),
                ("human", "Hello, how are you doing?"),
                ("ai", "I'm doing well, thanks!"),
                ("human", "{user_input}"),
            ])


# a = prompt.format_prompt({name="Bob"})

a = prompt.format_prompt(name="Bob",user_input="What is your name") 
print(a)
print(llm.invoke(a))

参考: https://python.langchain.com/docs/modules/model_io/prompts/quick_start

https://python.langchain.com/docs/modules/model_io/prompts/composition

相关推荐
沫璃染墨几秒前
从零入门计算机网络系列(三十二):Linux I/O 多路复用详解——poll 工作原理、TCP 实战与性能瓶颈
linux·运维·服务器·网络·c++·网络协议·网络安全
富贵儿9982 分钟前
NL2SQL 实践手记 09:答案正确以后,还能更快吗——一次可核对的 AI4DB 实验
数据库·python·sql·ai·自然语言处理·langchain
JAVA面经实录9172 分钟前
AI 辅助写单元测试
java·人工智能·单元测试·ai编程
科创致远6 分钟前
熟练工难留、新人上手慢,AI智能工位怎样沉淀工艺经验?
大数据·服务器·人工智能·制造·精益工程
IT枫斗者18 分钟前
浏览器跨域预检 OPTIONS:什么时候触发,为什么被接口拒绝
java·开发语言·数据库·mysql·fastapi
vivo互联网技术19 分钟前
从堆内存 91% 告警到高可用搜索:商城订单系统 Elasticsearch 重构之路
服务器·性能优化
SimonKing30 分钟前
可以自动部署项目的SSH客户端,部署时间可以摸鱼了
java·后端·程序员
水巷石子38 分钟前
从请求到逐字返回:用 Spring WebFlux + SSE 打通 Java 大模型的打字机流式输出
java·后端·spring
水巷石子41 分钟前
从一行调用到四种入口:Spring AI 1.x ChatClient 实战与 LangChain4j 选型对比
java·人工智能·spring·langchain