8000 GitHub - mininukefromfallout2/discord-chatbot-ollama: Simple Discord chatbot using ollama to generate responses
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mininukefromfallout2/discord-chatbot-ollama

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Chatbot using Ollama API

This is a Discord bot that integrates with the Ollama API to generate responses based on a custom set of instructions. The bot can be activated to respond to all messages in a specific channel, and it can be deactivated when necessary. The bot uses the aiohttp library for asynchronous HTTP requests and supports custom instructions for more dynamic responses. It also logs commands with a timezone specified in .env. If the response is longer than 2000 characters the response will be in a .md file.

⚠️ If you use Windows the bot might not be able to log commands or read the instructions.txt file. I suggest using Windows Subsystem for Linux to run the bot if you're using Windows.

Requirements

This project requires the following python dependencies:

  • discord.py: For building the bot.
  • aiohttp: For making asynchronous HTTP requests.
  • python-dotenv: To manage environment variables.
  • pytz: For time logging in the command log.

You can install these dependencies via requirements.txt.

You also need git to clone the repository.

Installation

# 1. Clone the repository to your local machine:
git clone https://github.com/mininukefromfallout2/discord-ollama-chatbot
cd discord-ollama-chatbot

# 2. Create a virtual environment (recommended but not required):
python3 -m venv venv

# 3. Activate the virtual environment:
source venv/bin/activate  # On macOS/Linux
venv\Scripts\activate  # On Windows

# 4. Install the required dependencies:
pip3 install -r requirements.txt

Setting Up the .env File

Create a .env file in the root of your project directory to store sensitive environment variables.

BOT_TOKEN=YOUR_TOKEN_HERE
TIMEZONE=America/New_York
OLLAMA_MODEL=phi3:mini
OLLAMA_ERROR_MESSAGE=I couldn't process that.
  • BOT_TOKEN: Your Discord bot token (can be obtained from the Discord Developer Portal).
  • TIMEZONE: Timezone to log commands in. List of timezones you can use are in timezones.txt (these were generated by pytz.all_timezones).
  • OLLAMA_MODEL: The model used by Ollama (e.g., phi3:mini).
  • OLLAMA_ERROR_MESSAGE: Customizable error message shown if Ollama fails to generate a response.

Setting Up Ollama Model

macOS/Linux

  1. Install Ollama:

    • Download and install Ollama from ollama.com.

    • After installation, open a terminal and run:

      ollama run phi3:mini
  2. Test API:

    • Open your browser and visit http://localhost:11434/api/generate to ensure the API is running.
  3. Stopping ollama:

    • In the same terminal run
      ollama stop phi3:mini

Windows

  1. Install Ollama:

    • Download and install Ollama from ollama.com.

    • Run Ollama with the following command:

      ollama run phi3:mini
  2. Test API:

    • Open your browser and visit http://localhost:11434/api/generate to ensure the API is running.
  3. Stopping ollama:

    • Run
      ollama stop phi3:mini

Running the Bot

Once everything is set up, you can run the bot using Python.

# 1. Ensure the virtual environment is activated:
source venv/bin/activate  # On macOS/Linux
venv\Scripts\activate  # On Windows

# 2. Run Ollama:
ollama run phi3:mini 

# 3. Run the bot:
python3 chatbot.py

The bot will log into Discord using the token provided in the .env file. You should see a message in your terminal indicating that the bot has successfully logged in.

Bot Commands

/activate

Activates the bot to respond to all messages in the channel where the command is used.

/activate

/deactivate

Deactivates the bot from responding to all messages in the channel where the command is used.

/deactivate

/reload_instructions

Reloads the instructions from instructions.txt. If the file doesn't exist, the default instructions will be used.

/reload_instructions

Message Event

When the bot is active in a channel, it will respond to all messages in that channel. The bot uses the instructions from the instructions.txt file (or the default instructions) to formulate a response using the Ollama API.

Compiling aiohttp on Linux, macOS, and Windows

Linux

# Install dependencies for building aiohttp
sudo apt update
sudo apt install -y python3-dev build-essential

# Install aiohttp
pip3 install aiohttp

macOS

# Install dependencies for building aiohttp
brew install python3

# Install aiohttp
pip3 install aiohttp

Windows

  1. Install Visual C++ Build Tools:

  2. Install aiohttp using pip:

    pip install aiohttp

Installing Python on Linux, macOS, and Windows

Linux

  1. On Ubuntu/Debian-based systems, you can install Python with the following command:

    sudo apt update
    sudo apt install python3 python3-pip
  2. On other Linux distributions, use your package manager to install Python (e.g., yum, dnf, etc.).

macOS

  1. Install Python 3 using Homebrew:

    brew install python3
  2. Verify the installation:

    python3 --version

Windows

  1. Download Python from python.org.

  2. Run the installer and ensure the "Add Python to PATH" checkbox is checked during installation.

  3. Verify the installation:

    python3 --version

Getting the Bot Token

To obtain a bot token, follow these steps:

  1. Go to the Discord Developer Portal.
  2. Create a new application.
  3. Under the "Bot" tab, click "Reset Token" (while youre in the bot tab scroll down and enable Message Content Intent and Server Members Intent).
  4. Copy the bot token and paste it into the .env file under BOT_TOKEN.

Invite Bot to Server

To invite the bot to your server:

  1. Go to the "OAuth2" tab in the Discord Developer Portal.
  2. Under "OAuth2 URL Generator", select "bot" as a scope and choose the necessary permissions which will be said in the next section. Also make sure to enable the message content intent in the Privileged Gateway Intents section
  3. Copy the generated URL and paste it into your browser to invite the bot to your server.

Permissions

  • Read Message History
  • Send Messages
  • View Channels
  • Attach Files

If you have any problems please create an issue and I'll get to it as soon as I can

About

Simple Discord chatbot using ollama to generate responses

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0