Slack
此笔记本展示了如何使用Slack聊天加载器。该类有助于将导出的Slack对话映射为LangChain聊天消息。
该过程分为三个步骤:
- 导出所需对话线程,请按照此处说明操作。
- 创建
SlackChatLoader,并将文件路径指向 JSON 文件或包含 JSON 文件的目录 - 调用
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 参考:Slack聊天加载器
loader = SlackChatLoader(
path="slack_dump.zip",
)
3. 加载消息
load()(或 lazy_load)方法返回一个“聊天会话”列表,目前每个会话仅包含已加载对话中的消息列表。
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 参考: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]