langchain(三)做一个简单的运用:聊天模型

1,857 阅读2分钟

引言:通过之前的知识再配合今天学的一些东西就能做一个之前看起来很高大上的应用了。

今天我们就用之前学过的知识构建一个简单的语言模型应用程序:聊天模型。

(通过这个例子,你能很清晰的理解调用api的处理问题的过程。)

你可以通过将一个或多个消息传递给聊天模型来获取聊天完成。响应将是一个消息。LangChain 目前支持的消息类型有 AIMessage(ai生成的)、HumanMessage(人给出的)、SystemMessage(系统的)和 ChatMessage(ChatMessage 接受一个任意的角色参数)。大多数情况下,你会只处理 HumanMessage、AIMessage 和 SystemMessage。


from langchain.chat_models import ChatOpenAI
# 调用了langchain的一个现成的包
from langchain.schema import (

    AIMessage,

    HumanMessage,

    SystemMessage

)

chat = ChatOpenAI(temperature=0)

你可以通过传递一个单一的消息来获得完成。

#设置HumanMessage的参数,其实就相当于对话中你的提问
chat([HumanMessage(content="Translate this sentence from English to French. I love programming.")])

# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

你也可以为 OpenAI 的 gpt-3.5-turbo 和 gpt-4 模型传递多个消息。


messages = [        #相当于我们提问前让chatgpt进行角色扮演    SystemMessage(content="You are a helpful assistant that translates English to French."),    HumanMessage(content="I love programming.")]

chat(messages)

# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

你还可以进一步使用 generate 来生成多组消息的完成。这将返回一个 LLMResult 带有一个额外的消息参数:


batch_messages = [    [        SystemMessage(content="You are a helpful assistant that translates English to French."),        HumanMessage(content="I love programming.")    ],

    [        SystemMessage(content="You are a helpful assistant that translates English to French."),        HumanMessage(content="I love artificial intelligence.")    ],

]

result = chat.generate(batch_messages)

result

# -> LLMResult(generations=[[ChatGeneration(text="J'aime programmer.", generation_info=None, message=AIMessage(content="J'aime programmer.", additional_kwargs={}))], [ChatGeneration(text="J'aime l'intelligence artificielle.", generation_info=None, message=AIMessage(content="J'aime l'intelligence artificielle.", additional_kwargs={}))]], llm_output={'token_usage': {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}})

从这个 LLMResult 中,你可以产看诸如 token 使用情况之类的内容(我们开发的时候预测token使用量是一个很重要的点):


result.llm_output['token_usage']

# -> {'prompt_tokens': 57, 'completion_tokens': 20, 'total_tokens': 77}

当然,实际开发中,我们不可能把“输入的话”(HumanMessage)写死,所以接着往下看。

接下来我们要介绍的就是“模板”,如果你是chatgpt的prompt高手,那么你很容易就理解。

template = "You are a helpful assistant that translates {input_language} to {output_language}."

其实就是在一句话里面挖空,通过{ }的方式,在后面你再给{ }里面的内容填充值。

代码实现:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

chat = ChatOpenAI(temperature=0)

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])

# 赋值
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())
# -> AIMessage(content="J'aime programmer.", additional_kwargs={})

之后就可以用input语句(python基础知识),获取用户输入的问题,然后输出

以上就是构建一个简单的语言模型应用程序中聊天模型的简单介绍。

明天继续连载。