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,
)
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 来实现模板化。您可以从一个或多个 MessagePromptTemplates 构建一个 ChatPromptTemplate。您可以使用 ChatPromptTemplate 的 format_prompt —— 这会返回一个 PromptValue,您可以根据需要将其转换为字符串或 Message 对象,取决于您是将格式化后的值作为输入提供给语言模型还是聊天模型。
对于方便起见,在模板上暴露了一个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)