Link Search Menu Expand Document Documentation Menu

创建或更新消息

2.12 版本引入

使用此 API 在对话式搜索的会话记忆中创建或更新消息。会话记忆存储当前会话的对话历史记录。消息代表对话中的一个问答对。

消息创建后,您需要将其 message_id 提供给其他 API。

POST 方法用于创建新消息。PUT 方法用于更新现有消息。

您只能更新消息的 additional_info 字段。

当安全插件启用时,所有内存都以私有安全模式存在。只有创建内存的用户才能与该内存及其消息进行交互。

端点

POST /_plugins/_ml/memory/<memory_id>/messages
PUT /_plugins/_ml/memory/message/<message_id>

路径参数

下表列出了可用的路径参数。

参数 数据类型 描述
memory_id 字符串 要添加消息的记忆 ID。POST 方法必需。
message_id 字符串 要更新的消息 ID。PUT 方法必需。

请求正文字段

下表列出了可用的请求字段。

字段 数据类型 必需/可选 可更新 描述
input 字符串 可选 消息中的问题(人类输入)。
prompt_template 字符串 可选 用于该消息的提示模板。该模板可能包含发送到大型语言模型的指令或示例。
response 字符串 可选 问题的答案(生成式 AI 输出)。
origin 字符串 可选 生成响应的 AI 或其他系统的名称。
additional_info 对象 可选 发送到 origin 的任何其他信息。

要成功创建或更新消息,您必须提供至少一个上述字段。提供的字段不能为 null 或空。

请求示例:创建消息

POST /_plugins/_ml/memory/SXA2cY0BfUsSoeNTz-8m/messages
{
    "input": "How do I make an interaction?",
    "prompt_template": "Hello OpenAI, can you answer this question?",
    "response": "Hello, this is OpenAI. Here is the answer to your question.",
    "origin": "MyFirstOpenAIWrapper",
    "additional_info": {
      "suggestion": "api.openai.com"
    }
}

示例响应

{
  "memory_id": "WnA3cY0BfUsSoeNTI-_J"
}

请求示例:向 additional_info 添加字段

PUT /_plugins/_ml/memory/message/WnA3cY0BfUsSoeNTI-_J
{
  "additional_info": {
    "feedback": "positive"
  }
}

示例响应

{
  "_index": ".plugins-ml-memory-message",
  "_id": "WnA3cY0BfUsSoeNTI-_J",
  "_version": 2,
  "result": "updated",
  "forced_refresh": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 45,
  "_primary_term": 1
}

更新后的消息包含一个附加的 feedback 字段

{
  "memory_id": "SXA2cY0BfUsSoeNTz-8m",
  "message_id": "WnA3cY0BfUsSoeNTI-_J",
  "create_time": "2024-02-03T23:04:15.554370024Z",
  "input": "How do I make an interaction?",
  "prompt_template": "Hello OpenAI, can you answer this question?",
  "response": "Hello, this is OpenAI. Here is the answer to your question.",
  "origin": "MyFirstOpenAIWrapper",
  "additional_info": {
    "feedback": "positive",
    "suggestion": "api.openai.com"
  }
}

请求示例:更改 additional_info 中的字段

PUT /_plugins/_ml/memory/message/WnA3cY0BfUsSoeNTI-_J
{
  "additional_info": {
    "feedback": "negative"
  }
}

示例响应

{
  "_index": ".plugins-ml-memory-message",
  "_id": "WnA3cY0BfUsSoeNTI-_J",
  "_version": 3,
  "result": "updated",
  "forced_refresh": true,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 46,
  "_primary_term": 1
}

更新后的消息包含已更新的 feedback 字段

{
  "memory_id": "SXA2cY0BfUsSoeNTz-8m",
  "message_id": "WnA3cY0BfUsSoeNTI-_J",
  "create_time": "2024-02-03T23:04:15.554370024Z",
  "input": "How do I make an interaction?",
  "prompt_template": "Hello OpenAI, can you answer this question?",
  "response": "Hello, this is OpenAI. Here is the answer to your question.",
  "origin": "MyFirstOpenAIWrapper",
  "additional_info": {
    "feedback": "negative",
    "suggestion": "api.openai.com"
  }
}