JinaChat 聊天
此笔记本介绍了如何开始使用 JinaChat 聊天模型。
from langchain_community.chat_models import JinaChat
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
API 参考:JinaChat | HumanMessage (人工消息) | 系统消息 | ChatPromptTemplate 模板 | HumanMessagePromptTemplate (简体中文) | SystemMessagePromptTemplate
chat = JinaChat(temperature=0)
messages = [
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(
content="Translate this sentence from English to French. I love programming."
),
]
chat(messages)
AIMessage(content="J'aime programmer.", additional_kwargs={}, example=False)
您可以通过使用MessagePromptTemplate.您可以构建一个ChatPromptTemplate从一个或多个MessagePromptTemplates.您可以使用ChatPromptTemplate的format_prompt-- 这将返回一个PromptValue,您可以将其转换为字符串或 Message 对象,具体取决于您是否要使用格式化的值作为 LLM 或聊天模型的输入。
为方便起见,有一个from_template方法。如果您要使用此模板,它将如下所示:
template = (
"You are a helpful assistant that translates {input_language} to {output_language}."
)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
chat_prompt = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
# get a chat completion from the formatted messages
chat(
chat_prompt.format_prompt(
input_language="English", output_language="French", text="I love programming."
).to_messages()
)
AIMessage(content="J'aime programmer.", additional_kwargs={}, example=False)