Skip to main content

Slack

This notebook shows how to use the Slack chat loader. This class helps map exported slack conversations to LangChain chat messages.

The process has three steps:

  1. Export the desired conversation thread by following the instructions here.
  2. Create the SlackChatLoader with the file path pointed to the json file or directory of JSON files
  3. Call loader.load() (or loader.lazy_load()) to perform the conversion. Optionally use merge_chat_runs to combine message from the same sender in sequence, and/or map_ai_messages to convert messages from the specified sender to the "AIMessage" class.

1. Create message dump

Currently (2023/08/23) this loader best supports a zip directory of files in the format generated by exporting your a direct message conversation from Slack. Follow up-to-date instructions from slack on how to do so.

We have an example in the LangChain repo.

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. Create the Chat Loader

Provide the loader with the file path to the zip directory. You can optionally specify the user id that maps to an ai message as well an configure whether to merge message runs.

from langchain_community.chat_loaders.slack import SlackChatLoader

API Reference:

loader = SlackChatLoader(
path="slack_dump.zip",
)

3. Load messages

The load() (or lazy_load) methods return a list of "ChatSessions" that currently just contain a list of messages per loaded conversation.

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")
)

Next Steps

You can then use these messages how you see fit, such as fine-tuning a model, few-shot example selection, or directly make predictions for the next message.

from langchain_openai import ChatOpenAI

llm = ChatOpenAI()

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

API Reference:

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]

Was this page helpful?


You can leave detailed feedback on GitHub.