赞
踩
LangChain系列文章
RunnableWithMessageHistory
让我们可以向某些类型的链中添加消息历史记录。
具体来说,它可用于接受以下之一作为输入的任何可运行对象
BaseMessage
BaseMessage
为值的键的字典BaseMessage
为最新消息值的键和一个带有历史消息值的单独键的字典并且以以下之一作为输出返回
AIMessage
内容的字符串BaseMessage
BaseMessage
的键的字典让我们看一些示例来看看它是如何工作的。
我们将使用Redis来存储我们的聊天消息记录和ChatOpenAI模型,所以我们需要安装以下依赖项:
pip install -U langchain redis
发现Docker Desktop version 4.26.1 需要设置 > Advanced > CLI tools 选择 System Docker CLI tools are install under /usr/local/bin
. 才能在terminal中用docker
命令
如果我们没有现有的Redis部署来连接,可以启动一个本地的Redis Stack服务器:
docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
REDIS_URL = "redis://localhost:6379/0"
让我们创建一个简单的链,它以字典作为输入,并返回一个BaseMessage
。
在这种情况下,输入中的“question
”键代表我们的输入消息,“history
”键是我们历史消息将被注入的地方。
prompt = ChatPromptTemplate.from_messages(
[
("system", "You're an assistant who's good at {ability}"),
MessagesPlaceholder(variable_name="history"),
("human", "{question}"),
]
)
chain = prompt | ChatOpenAI()
要将消息历史添加到我们的原始链中,我们将其包装在RunnableWithMessageHistory
类中。
至关重要的是,我们还需要定义一个方法,该方法接受一个session_id
字符串,并基于它返回一个BaseChatMessageHistory
。对于相同的输入,该方法应返回等效的输出。
在这种情况下,我们还需要指定input_messages_key
(要视为最新输入消息的键)和history_messages_key
(要将历史消息添加到的键)。
chain_with_history = RunnableWithMessageHistory(
chain,
lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL),
input_messages_key="question",
history_messages_key="history",
)
每当我们使用消息历史记录调用我们的链时,我们需要包含一个包含session_id的配置。
config={"configurable": {"session_id": "<SESSION_ID>"}}
在相同的配置下,我们的链应该从相同的聊天消息记录中拉取。
chain_with_history.invoke(
{"ability": "math", "question": "What does cosine mean?"},
config={"configurable": {"session_id": "foobar"}},
)
输出
AIMessage(content=' Cosine is one of the basic trigonometric functions in mathematics. It is defined as the ratio of the adjacent side to the hypotenuse in a right triangle.\n\nSome key properties and facts about cosine:\n\n- It is denoted by cos(θ), where θ is the angle in a right triangle. \n\n- The cosine of an acute angle is always positive. For angles greater than 90 degrees, cosine can be negative.\n\n- Cosine is one of the three main trig functions along with sine and tangent.\n\n- The cosine of 0 degrees is 1. As the angle increases towards 90 degrees, the cosine value decreases towards 0.\n\n- The range of values for cosine is -1 to 1.\n\n- The cosine function maps angles in a circle to the x-coordinate on the unit circle.\n\n- Cosine is used to find adjacent side lengths in right triangles, and has many other applications in mathematics, physics, engineering and more.\n\n- Key cosine identities include: cos(A+B) = cosAcosB − sinAsinB and cos(2A) = cos^2(A) − sin^2(A)\n\nSo in summary, cosine is a fundamental trig')
这里的its 就表示history的cosine
chain_with_history.invoke(
{"ability": "math", "question": "What's its inverse"},
config={"configurable": {"session_id": "foobar"}},
)
输出
AIMessage(content=' The inverse of the cosine function is called the arccosine or inverse cosine, often denoted as cos-1(x) or arccos(x).\n\nThe key properties and facts about arccosine:\n\n- It is defined as the angle θ between 0 and π radians whose cosine is x. So arccos(x) = θ such that cos(θ) = x.\n\n- The range of arccosine is 0 to π radians (0 to 180 degrees).\n\n- The domain of arccosine is -1 to 1. \n\n- arccos(cos(θ)) = θ for values of θ from 0 to π radians.\n\n- arccos(x) is the angle in a right triangle whose adjacent side is x and hypotenuse is 1.\n\n- arccos(0) = 90 degrees. As x increases from 0 to 1, arccos(x) decreases from 90 to 0 degrees.\n\n- arccos(1) = 0 degrees. arccos(-1) = 180 degrees.\n\n- The graph of y = arccos(x) is part of the unit circle, restricted to x')
from langchain.prompts import PromptTemplate from langchain_community.chat_models import ChatOpenAI from langchain_core.runnables import ConfigurableField # We add in a string output parser here so the outputs between the two are the same type from langchain_core.output_parsers import StrOutputParser from langchain.prompts import ChatPromptTemplate # Now lets create a chain with the normal OpenAI model from langchain_community.llms import OpenAI from typing import Optional from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder from langchain_community.chat_message_histories import RedisChatMessageHistory from langchain_community.chat_models import ChatAnthropic from langchain_core.chat_history import BaseChatMessageHistory from langchain_core.runnables.history import RunnableWithMessageHistory from dotenv import load_dotenv # 导入从 .env 文件加载环境变量的函数 load_dotenv() # 调用函数实际加载环境变量 from langchain.globals import set_debug # 导入在 langchain 中设置调试模式的函数 set_debug(True) # 启用 langchain 的调试模式 prompt = ChatPromptTemplate.from_messages( [ ("system", "You're an assistant who's good at {ability}"), MessagesPlaceholder(variable_name="history"), ("human", "{question}"), ] ) chain = prompt | ChatOpenAI() REDIS_URL = "redis://localhost:6379/0" chain_with_history = RunnableWithMessageHistory( chain, lambda session_id: RedisChatMessageHistory(session_id, url=REDIS_URL), input_messages_key="question", history_messages_key="history", ) # config={"configurable": {"session_id": "<SESSION_ID>"}} cosine_response = chain_with_history.invoke( {"ability": "math", "question": "What does cosine mean?"}, config={"configurable": {"session_id": "foobar"}}, ) print('cosine_response >> ', cosine_response) inverse_response = chain_with_history.invoke( {"ability": "math", "question": "What's its inverse"}, config={"configurable": {"session_id": "foobar"}}, ) print('inverse_response >> ', inverse_response)
debug 输出如下
"id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } } ] } [chain/end] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history > 3:chain:RunnableParallel<history>] [12ms] Exiting Chain run with output: [outputs] [chain/end] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history] [27ms] Exiting Chain run with output: { "ability": "math", "question": "What does cosine mean?", "history": [ { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } } ] } [chain/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence] Entering Chain run with input: [inputs] [chain/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 6:prompt:ChatPromptTemplate] Entering Prompt run with input: [inputs] [chain/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 6:prompt:ChatPromptTemplate] [16ms] Exiting Prompt run with output: { "lc": 1, "type": "constructor", "id": [ "langchain", "prompts", "chat", "ChatPromptValue" ], "kwargs": { "messages": [ { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "SystemMessage" ], "kwargs": { "content": "You're an assistant who's good at math", "additional_kwargs": {} } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {} } } ] } } [llm/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 7:llm:ChatOpenAI] Entering LLM run with input: { "prompts": [ "System: You're an assistant who's good at math\nHuman: What does cosine mean?\nAI: In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.\nHuman: What does cosine mean?\nAI: I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.\nHuman: What's its inverse\nAI: The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.\nHuman: What does cosine mean?" ] } [llm/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 7:llm:ChatOpenAI] [8.96s] Exiting LLM run with output: { "generations": [ [ { "text": "I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.", "generation_info": { "finish_reason": "stop", "logprobs": null }, "type": "ChatGeneration", "message": { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.", "additional_kwargs": {} } } } ] ], "llm_output": { "token_usage": { "completion_tokens": 155, "prompt_tokens": 433, "total_tokens": 588 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null }, "run": null } [chain/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence] [9.02s] Exiting Chain run with output: [outputs] [chain/end] [1:chain:RunnableWithMessageHistory] [9.07s] Exiting Chain run with output: [outputs] cosine_response >> content='I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.' [chain/start] [1:chain:RunnableWithMessageHistory] Entering Chain run with input: { "ability": "math", "question": "What's its inverse" } [chain/start] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history] Entering Chain run with input: { "ability": "math", "question": "What's its inverse" } [chain/start] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history > 3:chain:RunnableParallel<history>] Entering Chain run with input: { "ability": "math", "question": "What's its inverse" } [chain/start] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history > 3:chain:RunnableParallel<history> > 4:chain:load_history] Entering Chain run with input: { "ability": "math", "question": "What's its inverse" } [chain/end] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history > 3:chain:RunnableParallel<history> > 4:chain:load_history] [3ms] Exiting Chain run with output: { "output": [ { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.", "additional_kwargs": {}, "type": "ai", "example": false } } ] } [chain/end] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history > 3:chain:RunnableParallel<history>] [9ms] Exiting Chain run with output: [outputs] [chain/end] [1:chain:RunnableWithMessageHistory > 2:chain:insert_history] [12ms] Exiting Chain run with output: { "ability": "math", "question": "What's its inverse", "history": [ { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.", "additional_kwargs": {}, "type": "ai", "example": false } } ] } [chain/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence] Entering Chain run with input: [inputs] [chain/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 6:prompt:ChatPromptTemplate] Entering Prompt run with input: [inputs] [chain/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 6:prompt:ChatPromptTemplate] [3ms] Exiting Prompt run with output: { "lc": 1, "type": "constructor", "id": [ "langchain", "prompts", "chat", "ChatPromptValue" ], "kwargs": { "messages": [ { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "SystemMessage" ], "kwargs": { "content": "You're an assistant who's good at math", "additional_kwargs": {} } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What does cosine mean?", "additional_kwargs": {}, "type": "human", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.", "additional_kwargs": {}, "type": "ai", "example": false } }, { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "HumanMessage" ], "kwargs": { "content": "What's its inverse", "additional_kwargs": {} } } ] } } [llm/start] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 7:llm:ChatOpenAI] Entering LLM run with input: { "prompts": [ "System: You're an assistant who's good at math\nHuman: What does cosine mean?\nAI: In mathematics, cosine (abbreviated as cos) is a trigonometric function that represents the ratio of the length of the adjacent side to the length of the hypotenuse of a right triangle. It is defined for all real numbers and is commonly used in geometry, physics, and engineering to solve problems involving angles and distances. The cosine function oscillates between -1 and 1, with its maximum value of 1 occurring at 0 degrees (or 360 degrees) and the minimum value of -1 occurring at 180 degrees.\nHuman: What does cosine mean?\nAI: I apologize for any confusion. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the hypotenuse. It is defined as the ratio of the adjacent side to the hypotenuse. In more general terms, cosine is a mathematical function that maps angles to real numbers. The cosine function has many applications in various fields, including physics, engineering, and signal processing. It is commonly used to analyze periodic phenomena and to solve problems involving angles and distances.\nHuman: What's its inverse\nAI: The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.\nHuman: What does cosine mean?\nAI: I apologize for the confusion earlier. In mathematics, cosine (abbreviated as cos) is a trigonometric function that relates the angle of a right triangle to the ratio of the length of the adjacent side to the length of the hypotenuse. It is defined as the ratio of the length of the adjacent side to the length of the hypotenuse. In simpler terms, cosine represents the horizontal component of a unit vector in a given direction. The cosine function has values that range between -1 and 1, where -1 represents a negative direction, 0 represents perpendicularity, and 1 represents a positive direction. Cosine is widely used in various fields of science, engineering, and mathematics to analyze and solve problems involving angles, distances, and periodic phenomena.\nHuman: What's its inverse" ] } [llm/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence > 7:llm:ChatOpenAI] [4.45s] Exiting LLM run with output: { "generations": [ [ { "text": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "generation_info": { "finish_reason": "stop", "logprobs": null }, "type": "ChatGeneration", "message": { "lc": 1, "type": "constructor", "id": [ "langchain", "schema", "messages", "AIMessage" ], "kwargs": { "content": "The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.", "additional_kwargs": {} } } } ] ], "llm_output": { "token_usage": { "completion_tokens": 146, "prompt_tokens": 600, "total_tokens": 746 }, "model_name": "gpt-3.5-turbo", "system_fingerprint": null }, "run": null } [chain/end] [1:chain:RunnableWithMessageHistory > 5:chain:RunnableSequence] [4.46s] Exiting Chain run with output: [outputs] [chain/end] [1:chain:RunnableWithMessageHistory] [4.49s] Exiting Chain run with output: [outputs] inverse_response >> content='The inverse of the cosine function is called the arccosine function (abbreviated as arccos or acos). It is denoted as acos(x) or arccos(x), where x is a real number between -1 and 1. The arccosine function returns the angle whose cosine is equal to the given value. In other words, if y = arccos(x), then cos(y) = x. The output of the arccosine function is always an angle in the range of 0 to 180 degrees (or 0 to π radians), inclusive. It is important to note that the arccosine function is the inverse of the cosine function only within this restricted range.'
https://github.com/zgpeace/pets-name-langchain/tree/develop
https://python.langchain.com/docs/expression_language/how_to/message_history
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。