File size: 1,031 Bytes
d02575f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from langchain.chat_models import ChatOpenAI
from langchain import LLMChain
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate
)

chat = ChatOpenAI(temperature=0)

template = "你是一名翻译助手。把{input_language} 翻译为 {output_language}。"
system_message_prompt = SystemMessagePromptTemplate.from_template(template)

human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

# 这里是使用chat请求,返回BaseMessage。
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
result = chat(chat_prompt.format_prompt(input_language="中文", output_language="英语", text="我想请假").to_messages())
print(result.content)

# 这里是使用chain请求,返回str, 带有聊天模型的chain。
chain = LLMChain(llm=chat, prompt=chat_prompt)
result = chain.run(input_language="中文", output_language="英语", text="我想请假")
print(result)