mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-09 19:35:20 +03:00
5.3 KiB
5.3 KiB
In [1]:
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
AIMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.schema import AIMessage, HumanMessage, SystemMessageIn [2]:
chat = ChatOpenAI(temperature=0)In [3]:
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)Out [3]:
AIMessage(content="J'adore la programmation.", additional_kwargs={}, example=False)In [4]:
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)In [5]:
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()
)Out [5]:
AIMessage(content="J'adore la programmation.", additional_kwargs={}, example=False)In [6]:
fine_tuned_model = ChatOpenAI(temperature=0, model_name="ft:gpt-3.5-turbo-0613:langchain::7qTVM5AR")
fine_tuned_model(messages)Out [6]:
AIMessage(content="J'adore la programmation.", additional_kwargs={}, example=False)