我对如何组合多个消息并将其发送到大型语言模型(例如
ChatOpenAI
)感到困惑。
from langchain_core.prompts import ChatPromptTemplate
template = 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}"),
])
messages = template.format_messages(
name="Bob",
user_input="What is your name?"
)
messages
[SystemMessage(content='You are a helpful AI bot. Your name is Bob.'),
HumanMessage(content='Hello, how are you doing?'),
AIMessage(content="I'm doing well, thanks!"),
HumanMessage(content='What is your name?')]
是否生成如下所示的文本:
System:
Human:
Assistant:
Human:
...
如何打印发送给 llm 的最终文本?
您可以使用
format
方法将聊天模板格式化为字符串,以下是更新后的代码:
prompt = template.format(
name="Bob",
user_input="What is your name?"
)
prompt
输出:
System: You are a helpful AI bot. Your name is Bob.
Human: Hello, how are you doing?
AI: I'm doing well, thanks!
Human: What is your name?