LangChain 58 深入理解LangChain 表达式语言21 Memory消息历史 LangChain Expression Language (LCEL)

LangChain系列文章

  1. LangChain 36 深入理解LangChain 表达式语言优势一 LangChain Expression Language (LCEL)
  2. LangChain 37 深入理解LangChain 表达式语言二 实现prompt+model+output parser LangChain Expression Language (LCEL)
  3. LangChain 38 深入理解LangChain 表达式语言三 实现RAG检索增强生成 LangChain Expression Language (LCEL)
  4. LangChain 39 深入理解LangChain 表达式语言四 为什么要用LCEL LangChain Expression Language (LCEL)
  5. LangChain 40 实战Langchain访问OpenAI ChatGPT API Account deactivated的另类方法,访问跳板机API
  6. LangChain 41 深入理解LangChain 表达式语言五 为什么要用LCEL调用大模型LLM LangChain Expression Language (LCEL)
  7. LangChain 42 深入理解LangChain 表达式语言六 Runtime调用不同大模型LLM LangChain Expression Language (LCEL)
  8. LangChain 43 深入理解LangChain 表达式语言七 日志和Fallbacks异常备选方案 LangChain Expression Language (LCEL)
  9. LangChain 44 深入理解LangChain 表达式语言八 Runnable接口输入输出模式 LangChain Expression Language (LCEL)
  10. LangChain 45 深入理解LangChain 表达式语言九 Runnable 调用、流输出、批量调用、异步处理 LangChain Expression Language (LCEL)
  11. LangChain 46 深入理解LangChain 表达式语言十 Runnable 调用中间状态调试日志 LangChain Expression Language (LCEL)
  12. LangChain 47 深入理解LangChain 表达式语言十一 Runnable 并行处理 LangChain Expression Language (LCEL)
  13. LangChain 48 终极解决 实战Langchain访问OpenAI ChatGPT API Account deactivated的另类方法,访问跳板机API
  14. LangChain 49 深入理解LangChain 表达式语言十二 Runnable 透传数据保持输入不变 LangChain Expression Language (LCEL)
  15. LangChain 50 深入理解LangChain 表达式语言十三 自定义pipeline函数 LangChain Expression Language (LCEL)
  16. LangChain 51 深入理解LangChain 表达式语言十四 自动修复配置RunnableConfig LangChain Expression Language (LCEL)
  17. LangChain 52 深入理解LangChain 表达式语言十五 Bind runtime args绑定运行时参数 LangChain Expression Language (LCEL)
  18. LangChain 53 深入理解LangChain 表达式语言十六 Dynamically route动态路由 LangChain Expression Language (LCEL)
  19. LangChain 54 深入理解LangChain 表达式语言十七 Chains Route动态路由 LangChain Expression Language (LCEL)
  20. LangChain 55 深入理解LangChain 表达式语言十八 function Route自定义动态路由 LangChain Expression Language (LCEL)
  21. LangChain 56 深入理解LangChain 表达式语言十九 config运行时选择大模型LLM LangChain Expression Language (LCEL)
  22. LangChain 57 深入理解LangChain 表达式语言二十 LLM Fallbacks速率限制备份大模型 LangChain Expression Language (LCEL)

在这里插入图片描述

1. 添加消息历史记录 memory

RunnableWithMessageHistory让我们可以向某些类型的链中添加消息历史记录。

具体来说,它可用于接受以下之一作为输入的任何可运行对象

  • 一系列BaseMessage
  • 一个带有以一系列BaseMessage为值的键的字典
  • 一个带有以字符串或一系列BaseMessage为最新消息值的键和一个带有历史消息值的单独键的字典

并且以以下之一作为输出返回

  • 可以被视为AIMessage内容的字符串
  • 一系列BaseMessage
  • 一个带有包含一系列BaseMessage的键的字典

让我们看一些示例来看看它是如何工作的。

2. 环境准备

我们将使用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"

3. 示例:字典输入Dict input,消息输出 message output

让我们创建一个简单的链,它以字典作为输入,并返回一个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()

3.1 添加消息历史

要将消息历史添加到我们的原始链中,我们将其包装在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",
)

3.2 调用配置config

每当我们使用消息历史记录调用我们的链时,我们需要包含一个包含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')

4. 完整代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值