0


langchain学习5

langchain学习5

文章目录

原文地址:examples/04-langchain-chat.ipynb at master · pinecone-io/examples (github.com)

langchain-chat

设置apikey

from getpass import getpass

# enter your api key
OPENAI_API_KEY = getpass("OpenAI API key: ")

初始化

ChatOpenAI

对象。我们将

temperature = 0

设置为最小化随机性并使输出repeatable.

from langchain.chat_models import ChatOpenAI

chat = ChatOpenAI(
    openai_api_key=OPENAI_API_KEY,
    temperature=0,
    model='gpt-3.5-turbo')

与Chat-GPT模型gpt-3.5-turbo的聊天通常结构如下:

System: You are a helpful assistant.

User: Hi AI, how are you today?

Assistant: I'm great thank you. How can I help you?

User: I'd like to understand string theory.

Assistant:

The final

"Assistant:"

without a response is what would prompt the model to continue the comversation. In the official OpenAI

ChatCompletion

endpoint these would be passed to the model in a format like:

[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"Hi AI, how are you today?"},{"role":"assistant","content":"I'm great thank you. How can I help you?"}{"role":"user","content":"I'd like to understand string theory."}]

在LangChain中,会是有些不同的格式。我们使用三个消息对象( message objects),如下所示:

from langchain.schema import(
    SystemMessage,
    HumanMessage,
    AIMessage
)

messages =[
    SystemMessage(content="You are a helpful assistant."),
    HumanMessage(content="Hi AI, how are you today?"),
    AIMessage(content="I'm great thank you. How can I help you?"),
    HumanMessage(content="I'd like to understand string theory.")]

格式非常相似,我们只是将

"user"

的角色换成了

HumanMessage

"assistant"

的角色换成了

AIMessage

我们通过将这些消息传递给

ChatOpenAI

对象来生成AI的下一个响应。

res = chat(messages)
res
AIMessage(content='Sure, I can help you with that. String theory is a theoretical framework in physics that attempts to reconcile quantum mechanics and general relativity. It proposes that the fundamental building blocks of the universe are not particles, but rather tiny, one-dimensional "strings" that vibrate at different frequencies. These strings are incredibly small, with a length scale of around 10^-35 meters.\n\nThe theory suggests that there are many different possible configurations of these strings, each corresponding to a different particle. For example, an electron might be a string vibrating in one way, while a photon might be a string vibrating in a different way.\n\nOne of the key features of string theory is that it requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. In fact, the theory requires a total of 10 or 11 dimensions, depending on the specific version of the theory.\n\nString theory is still a highly speculative area of physics, and there is currently no experimental evidence to support it. However, it is an active area of research, and many physicists believe that it has the potential to provide a unified description of all the fundamental forces of nature.', additional_kwargs={})

In response we get another AI message object. We can print it more clearly like so:

print(res.content)
Sure, I can help you with that. String theory is a theoretical framework in physics that attempts to reconcile quantum mechanics and general relativity. It proposes that the fundamental building blocks of the universe are not particles, but rather tiny, one-dimensional "strings" that vibrate at different frequencies. These strings are incredibly small, with a length scale of around 10^-35 meters.

The theory suggests that there are many different possible configurations of these strings, each corresponding to a different particle. For example, an electron might be a string vibrating in one way, while a photon might be a string vibrating in a different way.

One of the key features of string theory is that it requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. In fact, the theory requires a total of 10 or 11 dimensions, depending on the specific version of the theory.

String theory is still a highly speculative area of physics, and there is currently no experimental evidence to support it. However, it is an active area of research, and many physicists believe that it has the potential to provide a unified description of all the fundamental forces of nature.

因为

res

也是

AIMessage

对象,我们可以将其附加到消息中,添加另一个

HumanMessage

,并在对话中生成下一个response。

# add latest AI response to messages
messages.append(res)# now create a new user prompt
prompt = HumanMessage(
    content="Why do physicists believe it can produce a 'unified theory'?")# add to messages
messages.append(prompt)# send to chat-gpt
res = chat(messages)print(res.content)
Physicists believe that string theory has the potential to produce a unified theory because it provides a framework for describing all the fundamental particles and forces of nature in a single, coherent framework.

In the standard model of particle physics, there are four fundamental forces: gravity, electromagnetism, the strong nuclear force, and the weak nuclear force. These forces are described by different mathematical equations and are not easily reconciled with each other.

String theory, on the other hand, proposes that all of these forces arise from the same underlying physical principles. In particular, the theory suggests that the different particles and forces are all manifestations of the same underlying strings vibrating in different ways.

Furthermore, string theory requires the existence of extra dimensions beyond the three spatial dimensions we are familiar with. These extra dimensions could potentially provide a way to unify the different forces of nature by showing how they arise from a single, higher-dimensional framework.

While there is currently no experimental evidence to support string theory, many physicists believe that it is a promising avenue for developing a unified theory of physics.

New Prompt Templates

Alongside what we’ve seen so far there are also three new prompt templates that we can use. Those are the

SystemMessagePromptTemplate

,

AIMessagePromptTemplate

, and

HumanMessagePromptTemplate

.

这些是Langchain的prompt templates的扩展,它们返回的“prompt”分别为

SystemMessage

AIMessage

HumanMessage

对象。

目前,这些对象的使用情况并不是很多。然而,如果我们想要在消息中添加一些内容,这将会很有帮助。例如,假设我们希望我们的人工智能回复始终不超过50个字符。

Using the current OpenAI

gpt-3.5-turbo-0301

model, we might run into issues if passing this instruction in the first system message only.

chat = ChatOpenAI(
    openai_api_key=OPENAI_API_KEY,
    temperature=0,
    model='gpt-3.5-turbo-0301')# setup first system message
messages =[
    SystemMessage(content=('You are a helpful assistant. You keep responses to no more than ''100 characters long (including whitespace), and sign off every ''message with a random name like "Robot McRobot" or "Bot Rob".')),
    HumanMessage(content="Hi AI, how are you? What is quantum physics?")]

Now make our first completion.

res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 154
I'm doing well, thank you! Quantum physics is the study of the behavior of matter and energy at a very small scale, such as atoms and subatomic particles.

看起来我们的AI助手在遵循我们的instructions方面并不是很擅长。如果我们通过

HumanMessagePromptTemplate

将这些instructions添加到

HumanMessage

中会怎样呢?

from langchain.prompts.chat import HumanMessagePromptTemplate, ChatPromptTemplate

human_template = HumanMessagePromptTemplate.from_template('{input} Can you keep the response to no more than 100 characters '+'(including whitespace), and sign off with a random name like "Robot '+'McRobot" or "Bot Rob".')# create the human message
chat_prompt = ChatPromptTemplate.from_messages([human_template])# format with some input
chat_prompt_value = chat_prompt.format_prompt(input="Hi AI, how are you? What is quantum physics?")
chat_prompt_value
ChatPromptValue(messages=[HumanMessage(content='Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like "Robot McRobot" or "Bot Rob".', additional_kwargs={})])

请注意,要像典型的prompt templates一样使用

HumanMessagePromptTemplate

并使用

.format_prompt

方法,我们需要将其通过

ChatPromptTemplate

对象传递。这对于所有新的chat-based prompt templates.都是如此。

使用此方法,我们返回一个

ChatPromptValue

对象。可以将其格式化为列表或字符串,如下所示:

chat_prompt_value.to_messages()
[HumanMessage(content='Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like "Robot McRobot" or "Bot Rob".', additional_kwargs={})]
chat_prompt_value.to_string()
'Human: Hi AI, how are you? What is quantum physics? Can you keep the response to no more than 100 characters (including whitespace), and sign off with a random name like "Robot McRobot" or "Bot Rob".'

Let’s see if this new approach works.

messages =[
    SystemMessage(content=('You are a helpful assistant. You keep responses to no more than ''100 characters long (including whitespace), and sign off every ''message with a random name like "Robot McRobot" or "Bot Rob".')),
    chat_prompt.format_prompt(input="Hi AI, how are you? What is quantum physics?").to_messages()[0]]

res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 99
I'm good! Quantum physics studies the behavior of matter and energy at a very small scale. -Bot Rob

This time we get pretty close, we’re slightly over the character limit (by

8

characters), and we got a sign off with

- Bot Rob

.

We can also use the prompt templates approach for building an initial system message with a few examples for the chatbot to follow — few-shot training via examples. Let’s see what that looks like.

我们还可以使用prompt templates的方法来构建一个初始的系统消息,并提供一些示例for the chatbot to follow, — few-shot training via examples.。让我们看看它是什么样子的。

from langchain.prompts.chat import(
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate
)

system_template = SystemMessagePromptTemplate.from_template('You are a helpful assistant. You keep responses to no more than ''{character_limit} characters long (including whitespace), and sign ''off every message with "- {sign_off}')
human_template = HumanMessagePromptTemplate.from_template("{input}")
ai_template = AIMessagePromptTemplate.from_template("{response} - {sign_off}")# create the list of messages
chat_prompt = ChatPromptTemplate.from_messages([
    system_template,
    human_template,
    ai_template
])# format with required inputs
chat_prompt_value = chat_prompt.format_prompt(
    character_limit="50", sign_off="Robot McRobot",input="Hi AI, how are you? What is quantum physics?",
    response="Good! It's physics of small things")
chat_prompt_value
ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with "- Robot McRobot', additional_kwargs={}), HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}), AIMessage(content="Good! It's physics of small things - Robot McRobot", additional_kwargs={})])

We extract these as messages and feed them into the chat model alongside our next query, which we’ll feed in as usual (without the template).

我们将这些作为消息提取出来,并与我们的下一个query一起输入到聊天模型中,我们会像往常一样输入(不使用模板)。

messages = chat_prompt_value.to_messages()

messages.append(
    HumanMessage(content="How small?"))

res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 41
Atoms, electrons, photons - Robot McRobot

Perfect, we seem to get a good response there, let’s try a couple more.

# add last response
messages.append(res)# make new query
messages.append(
    HumanMessage(content="Okay cool, so it is like 'partical physics'?"))

res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 54
Yes, it's a branch of particle physics - Robot McRobot

我们在这里稍微超过了一点。我们可以像这样重新开始使用以前的

HumanMessagePromptTemplate

from langchain import PromptTemplate

# this is a faster way of building the prompt via a PromptTemplate
human_template = HumanMessagePromptTemplate.from_template('{input} Answer in less than {character_limit} characters (including whitespace).')# create the human message
human_prompt = ChatPromptTemplate.from_messages([human_template])# format with some input
human_prompt_value = human_prompt.format_prompt(input="Okay cool, so it is like 'partical physics'?",
    character_limit="50")
human_prompt_value
ChatPromptValue(messages=[HumanMessage(content="Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).", additional_kwargs={})])
# drop the last message about partical physics so we can rewrite
messages.pop(-1)
HumanMessage(content="Okay cool, so it is like 'partical physics'?", additional_kwargs={})
messages.extend(human_prompt_value.to_messages())
messages
[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with "- Robot McRobot', additional_kwargs={}),
 HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}),
 AIMessage(content="Good! It's physics of small things - Robot McRobot", additional_kwargs={}),
 HumanMessage(content='How small?', additional_kwargs={}),
 AIMessage(content='Atoms, electrons, photons - Robot McRobot', additional_kwargs={}),
 HumanMessage(content="Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).", additional_kwargs={})

Now process:

res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 28
Yes, similar - Robot McRobot

There we go, a good answer again!

Now, it’s arguable as to whether all of the above is better than simple f-strings like:

_input ="Okay cool, so it is like 'partical physics'?"
character_limit =50

human_message = HumanMessage(content=(f"{_input} Answer in less than {character_limit} characters ""(including whitespace)."))

human_message
HumanMessage(content="Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).", additional_kwargs={})

In this example, the above is far simpler. So we wouldn’t necessarily recommend using prompt templates over f-strings in all scenarios. But, if you do find yourself in a scenario where they become more useful — you now know how to use them.

最后,让我们看看如何使用f-string格式化消息

human_message

完成剩余的完成过程:

# drop the last message about partical physics so we can rewrite
messages.pop(-1)# add f-string formatted message
messages.append(human_message)
messages
[SystemMessage(content='You are a helpful assistant. You keep responses to no more than 50 characters long (including whitespace), and sign off every message with "- Robot McRobot', additional_kwargs={}),
 HumanMessage(content='Hi AI, how are you? What is quantum physics?', additional_kwargs={}),
 AIMessage(content="Good! It's physics of small things - Robot McRobot", additional_kwargs={}),
 HumanMessage(content='How small?', additional_kwargs={}),
 AIMessage(content='Atoms, electrons, photons - Robot McRobot', additional_kwargs={}),
 HumanMessage(content="Okay cool, so it is like 'partical physics'? Answer in less than 50 characters (including whitespace).", additional_kwargs={})]
res = chat(messages)print(f"Length: {len(res.content)}\n{res.content}")
Length: 28
Yes, similar - Robot McRobot

That’s it for this example exploring LangChain’s new features for chat.

标签: 人工智能

本文转载自: https://blog.csdn.net/qq_52431436/article/details/129850636
版权归原作者 临风而眠 所有, 如有侵权,请联系我们删除。

“langchain学习5”的评论:

还没有评论