Skip to main content
Open In ColabOpen on GitHub

Slack

本笔记本展示了如何使用Slack聊天加载器。该类帮助将导出的Slack对话映射到LangChain聊天消息。

该过程有三个步骤:

  1. 按照这里的说明导出所需的对话线程。
  2. 创建SlackChatLoader,文件路径指向json文件或JSON文件的目录
  3. 调用 loader.load()(或 loader.lazy_load())以执行转换。可选使用 merge_chat_runs 来合并来自同一发送者的连续消息,和/或使用 map_ai_messages 将来自指定发送者的消息转换为 "AIMessage" 类。

1. 创建消息转储

目前(2023/08/23),此加载器最佳支持从Slack导出直接消息对话生成的zip文件目录格式。请遵循Slack的最新说明进行操作。

我们在LangChain仓库中有一个示例。

import requests

permalink = "https://raw.githubusercontent.com/langchain-ai/langchain/342087bdfa3ac31d622385d0f2d09cf5e06c8db3/libs/langchain/tests/integration_tests/examples/slack_export.zip"
response = requests.get(permalink)
with open("slack_dump.zip", "wb") as f:
f.write(response.content)

2. 创建聊天加载器

为加载器提供zip目录的文件路径。您还可以选择指定映射到AI消息的用户ID,并配置是否合并消息运行。

from langchain_community.chat_loaders.slack import SlackChatLoader
API Reference:SlackChatLoader
loader = SlackChatLoader(
path="slack_dump.zip",
)

3. 加载消息

load()(或lazy_load)方法返回一个“ChatSessions”列表,目前仅包含每个加载对话的消息列表。

from typing import List

from langchain_community.chat_loaders.utils import (
map_ai_messages,
merge_chat_runs,
)
from langchain_core.chat_sessions import ChatSession

raw_messages = loader.lazy_load()
# Merge consecutive messages from the same sender into a single message
merged_messages = merge_chat_runs(raw_messages)
# Convert messages from "U0500003428" to AI messages
messages: List[ChatSession] = list(
map_ai_messages(merged_messages, sender="U0500003428")
)

下一步

然后,您可以根据需要利用这些消息,例如微调模型、选择少量示例,或直接预测下一条消息。

from langchain_openai import ChatOpenAI

llm = ChatOpenAI()

for chunk in llm.stream(messages[1]["messages"]):
print(chunk.content, end="", flush=True)
API Reference:ChatOpenAI
Hi, 

I hope you're doing well. I wanted to reach out and ask if you'd be available to meet up for coffee sometime next week. I'd love to catch up and hear about what's been going on in your life. Let me know if you're interested and we can find a time that works for both of us.

Looking forward to hearing from you!

Best, [Your Name]

这个页面有帮助吗?