mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2026-09-23 17:08:58 -04:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99db6d8163 | ||
|
|
3a770be5de | ||
|
|
f8d8fe765f | ||
|
|
2c25f7f500 | ||
|
|
0beaed85aa | ||
|
|
69637722db |
+6
-4
@@ -4,11 +4,13 @@ CLIENT_TOKEN = BOT_TOKEN
|
|||||||
# Default model for new users
|
# Default model for new users
|
||||||
MODEL = DEFAULT_MODEL
|
MODEL = DEFAULT_MODEL
|
||||||
|
|
||||||
# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local
|
# ip/port address of the LLM server (ollama, llmman, ...), I use 172.18.0.3 for docker, 127.0.0.1 for local
|
||||||
OLLAMA_IP = IP_ADDRESS
|
# ollama listens on 11434, llmman (https://github.com/llmmanorg/llmman) on 17434
|
||||||
OLLAMA_PORT = PORT
|
# OLLAMA_IP / OLLAMA_PORT are still accepted as deprecated aliases
|
||||||
|
LLM_ENDPOINT = IP_ADDRESS
|
||||||
|
LLM_PORT = PORT
|
||||||
|
|
||||||
# ip address for discord bot container, I use 172.18.0.2, use different IP than ollama_ip
|
# ip address for discord bot container, I use 172.18.0.2, use different IP than llm_endpoint
|
||||||
DISCORD_IP = IP_ADDRESS
|
DISCORD_IP = IP_ADDRESS
|
||||||
|
|
||||||
# subnet address, ex. 172.18.0.0 as we use /16.
|
# subnet address, ex. 172.18.0.0 as we use /16.
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ jobs:
|
|||||||
- name: Set up Node Environment
|
- name: Set up Node Environment
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: lts/jod
|
node-version: lts/krypton
|
||||||
cache: npm
|
cache: npm
|
||||||
|
|
||||||
- name: Install Project Dependencies
|
- name: Install Project Dependencies
|
||||||
@@ -33,8 +33,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||||
|
|
||||||
# set -e ensures if nohup fails, this section fails
|
# set -e ensures if nohup fails, this section fails
|
||||||
@@ -45,7 +45,7 @@ jobs:
|
|||||||
|
|
||||||
Discord-Ollama-Container-Build: # test docker build and run
|
Discord-Ollama-Container-Build: # test docker build and run
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 2
|
timeout-minutes: 3
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -53,15 +53,15 @@ jobs:
|
|||||||
- name: Set up Node Environment
|
- name: Set up Node Environment
|
||||||
uses: actions/setup-node@v4
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: lts/jod
|
node-version: lts/krypton
|
||||||
cache: npm
|
cache: npm
|
||||||
|
|
||||||
- name: Create Environment Variables
|
- name: Create Environment Variables
|
||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||||
|
|
||||||
- name: Setup Docker Network and Images
|
- name: Setup Docker Network and Images
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.CLIENT }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.CLIENT }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||||
echo DISCORD_IP = ${{ secrets.DISCORD_IP }} >> .env
|
echo DISCORD_IP = ${{ secrets.DISCORD_IP }} >> .env
|
||||||
echo SUBNET_ADDRESS = ${{ secrets.SUBNET_ADDRESS }} >> .env
|
echo SUBNET_ADDRESS = ${{ secrets.SUBNET_ADDRESS }} >> .env
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
name: Tests (Fork PRs)
|
||||||
|
run-name: Unit Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
paths:
|
||||||
|
- '*'
|
||||||
|
- 'package*.json'
|
||||||
|
- 'src/**'
|
||||||
|
- 'tests/**'
|
||||||
|
- '!docs/**'
|
||||||
|
- '!imgs/**'
|
||||||
|
- '!.github/**'
|
||||||
|
- '.github/workflows/**'
|
||||||
|
- '!.gitignore'
|
||||||
|
- '!LICENSE'
|
||||||
|
- '!README'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Discord-Node-Test-Fork:
|
||||||
|
if: ${{ github.event.pull_request.head.repo.fork == true }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 'lts/jod'
|
||||||
|
cache: npm
|
||||||
|
|
||||||
|
- name: Install Project Dependencies
|
||||||
|
run: |
|
||||||
|
npm install
|
||||||
|
|
||||||
|
- name: Create Dummy Env
|
||||||
|
run: |
|
||||||
|
echo CLIENT_TOKEN=test-token >> .env
|
||||||
|
echo LLM_ENDPOINT=127.0.0.1 >> .env
|
||||||
|
echo LLM_PORT=11434 >> .env
|
||||||
|
echo MODEL=dummy-model >> .env
|
||||||
|
|
||||||
|
- name: Test Application
|
||||||
|
run: |
|
||||||
|
npm run tests
|
||||||
@@ -20,6 +20,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Discord-Node-Test:
|
Discord-Node-Test:
|
||||||
|
if: ${{ github.event.pull_request.head.repo.fork == false }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 2
|
timeout-minutes: 2
|
||||||
|
|
||||||
@@ -41,8 +42,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||||
|
|
||||||
- name: Test Application
|
- name: Test Application
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
# use node LTS image for version 22
|
# use node LTS image for version 24
|
||||||
FROM node:jod-alpine
|
FROM node:krypton-alpine
|
||||||
|
|
||||||
# set working directory inside container
|
# set working directory inside container
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|||||||
@@ -8,36 +8,15 @@
|
|||||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml"><img alt="Tests" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml/badge.svg" /></a>
|
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml"><img alt="Tests" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml/badge.svg" /></a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
## About/Goals
|
## About
|
||||||
Ollama is an AI model management tool that allows users to install and use custom large language models locally.
|
Discord Ollama is a Discord bot that lets users interact with locally hosted
|
||||||
The project aims to:
|
Ollama language models directly from Discord.
|
||||||
* [x] Create a Discord bot that will utilize Ollama and chat to chat with users!
|
|
||||||
* [x] User and Server Preferences
|
|
||||||
* [x] Message Persistance
|
|
||||||
* [x] Containerization with Docker
|
|
||||||
* [x] Slash Commands Compatible
|
|
||||||
* [x] Summary Command
|
|
||||||
* [ ] Model Info Command
|
|
||||||
* [ ] List Models Command
|
|
||||||
* [x] Pull Model Command
|
|
||||||
* [x] Switch Model Command
|
|
||||||
* [x] Delete Model Command
|
|
||||||
* [x] Create Thread Command
|
|
||||||
* [x] Create Private Thread Command
|
|
||||||
* [x] Message Stream Command
|
|
||||||
* [x] Change Message History Size Command
|
|
||||||
* [x] Clear Channel History Command (User Only)
|
|
||||||
* [x] Administrator Role Compatible
|
|
||||||
* [x] Generated Token Length Handling for >2000
|
|
||||||
* [x] Token Length Handling of any message size
|
|
||||||
* [x] Multi-User Chat Generation - This was built in from Ollama `v0.2.1+`
|
|
||||||
* [ ] Ollama Tool Support Implementation
|
|
||||||
* [ ] Enhanced Channel Context Awareness
|
|
||||||
* [ ] Improved User Replied Triggers
|
|
||||||
|
|
||||||
Further, Ollama provides the functionality to utilize custom models or provide context for the top-layer of any model available through the Ollama model library.
|
The bot supports model management, streamed responses, conversation history,
|
||||||
* [Customize a model](https://github.com/ollama/ollama#customize-a-model)
|
private threads, and configurable server controls. Because Ollama runs locally,
|
||||||
* [Modelfile Docs](https://github.com/ollama/ollama/blob/main/docs/modelfile.md)
|
you retain control over the models and data used by the bot.
|
||||||
|
|
||||||
|
See the [setup guides](./docs/setup-local.md) to get started.
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
These are guides to the features and capabilities of this app.
|
These are guides to the features and capabilities of this app.
|
||||||
@@ -48,6 +27,7 @@ These are guides to the features and capabilities of this app.
|
|||||||
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
||||||
* You will need a `.env` file in the root of the project directory with the bot's token. There is a `.env.sample` is provided for you as a reference for what environment variables.
|
* You will need a `.env` file in the root of the project directory with the bot's token. There is a `.env.sample` is provided for you as a reference for what environment variables.
|
||||||
* For example, `CLIENT_TOKEN = [Bot Token]`
|
* For example, `CLIENT_TOKEN = [Bot Token]`
|
||||||
|
* The LLM server is configured with `LLM_ENDPOINT` / `LLM_PORT` (`OLLAMA_IP` / `OLLAMA_PORT` still work as deprecated aliases).
|
||||||
* Please refer to the docs for bot setup.
|
* Please refer to the docs for bot setup.
|
||||||
* [Creating a Discord App](./docs/setup-discord-app.md)
|
* [Creating a Discord App](./docs/setup-discord-app.md)
|
||||||
* [Local Machine Setup](./docs/setup-local.md)
|
* [Local Machine Setup](./docs/setup-local.md)
|
||||||
@@ -61,6 +41,7 @@ These are guides to the features and capabilities of this app.
|
|||||||
* This project requires the use of npm version `10.9.0` or above.
|
* This project requires the use of npm version `10.9.0` or above.
|
||||||
* [Ollama](https://ollama.com/)
|
* [Ollama](https://ollama.com/)
|
||||||
* [Ollama Docker Image](https://hub.docker.com/r/ollama/ollama)
|
* [Ollama Docker Image](https://hub.docker.com/r/ollama/ollama)
|
||||||
|
* [llmman](https://github.com/llmmanorg/llmman) also serves the Ollama API (on port `17434`) and can be used in its place, see [Local Machine Setup](./docs/setup-local.md#using-with-llmman-alternative-to-ollama).
|
||||||
* [Discord.js Docs](https://discord.js.org/docs/packages/discord.js/main)
|
* [Discord.js Docs](https://discord.js.org/docs/packages/discord.js/main)
|
||||||
* [Setting up Docker (Ubuntu 20.04)](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
* [Setting up Docker (Ubuntu 20.04)](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
||||||
* [Setting up Nvidia Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
|
* [Setting up Nvidia Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
|
||||||
|
|||||||
+5
-5
@@ -7,11 +7,11 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: kevinthedang/discord-ollama:0.9.1
|
image: kevinthedang/discord-ollama:0.9.2
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
OLLAMA_IP: ${OLLAMA_IP}
|
LLM_ENDPOINT: ${LLM_ENDPOINT:-${OLLAMA_IP}} # OLLAMA_IP is a deprecated alias
|
||||||
OLLAMA_PORT: ${OLLAMA_PORT}
|
LLM_PORT: ${LLM_PORT:-${OLLAMA_PORT}} # OLLAMA_PORT is a deprecated alias
|
||||||
MODEL: ${MODEL}
|
MODEL: ${MODEL}
|
||||||
networks:
|
networks:
|
||||||
ollama-net:
|
ollama-net:
|
||||||
@@ -26,14 +26,14 @@ services:
|
|||||||
restart: always
|
restart: always
|
||||||
networks:
|
networks:
|
||||||
ollama-net:
|
ollama-net:
|
||||||
ipv4_address: ${OLLAMA_IP}
|
ipv4_address: ${LLM_ENDPOINT:-${OLLAMA_IP}}
|
||||||
runtime: nvidia # use Nvidia Container Toolkit for GPU support
|
runtime: nvidia # use Nvidia Container Toolkit for GPU support
|
||||||
devices:
|
devices:
|
||||||
- /dev/nvidia0
|
- /dev/nvidia0
|
||||||
volumes:
|
volumes:
|
||||||
- ollama:/root/.ollama
|
- ollama:/root/.ollama
|
||||||
ports:
|
ports:
|
||||||
- ${OLLAMA_PORT}:${OLLAMA_PORT}
|
- ${LLM_PORT:-${OLLAMA_PORT}}:${LLM_PORT:-${OLLAMA_PORT}}
|
||||||
|
|
||||||
# create a network that supports giving addresses withing a specific subnet
|
# create a network that supports giving addresses withing a specific subnet
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ sudo systemctl restart docker
|
|||||||
* [GitHub repository](https://github.com/NVIDIA/nvidia-container-toolkit?tab=readme-ov-file) for Nvidia Container Toolkit
|
* [GitHub repository](https://github.com/NVIDIA/nvidia-container-toolkit?tab=readme-ov-file) for Nvidia Container Toolkit
|
||||||
|
|
||||||
## To Run (with Docker and Docker Compose)
|
## To Run (with Docker and Docker Compose)
|
||||||
* With the inclusion of subnets in the `docker-compose.yml`, you will need to set the `SUBNET_ADDRESS`, `OLLAMA_IP`, `OLLAMA_PORT`, and `DISCORD_IP`. Here are some default values if you don't care:
|
* With the inclusion of subnets in the `docker-compose.yml`, you will need to set the `SUBNET_ADDRESS`, `LLM_ENDPOINT`, `LLM_PORT`, and `DISCORD_IP`. Here are some default values if you don't care:
|
||||||
* `SUBNET_ADDRESS = 172.18.0.0`
|
* `SUBNET_ADDRESS = 172.18.0.0`
|
||||||
* `OLLAMA_IP = 172.18.0.2`
|
* `LLM_ENDPOINT = 172.18.0.2`
|
||||||
* `OLLAMA_PORT = 11434`
|
* `LLM_PORT = 11434`
|
||||||
* `DISCORD_IP = 172.18.0.3`
|
* `DISCORD_IP = 172.18.0.3`
|
||||||
* Don't understand any of this? watch a Networking video to understand subnetting.
|
* Don't understand any of this? watch a Networking video to understand subnetting.
|
||||||
* You also need all environment variables shown in [`.env.sample`](../.env.sample)
|
* You also need all environment variables shown in [`.env.sample`](../.env.sample)
|
||||||
|
|||||||
+10
-3
@@ -14,11 +14,18 @@
|
|||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> You can now pull models directly from the Discord client using `/pull-model <model-name>` or `/switch-model <model-name>`. They must exist from your local model library or from the [Ollama Model Library](https://ollama.com/library)
|
> You can now pull models directly from the Discord client using `/pull-model <model-name>` or `/switch-model <model-name>`. They must exist from your local model library or from the [Ollama Model Library](https://ollama.com/library)
|
||||||
|
|
||||||
|
## Using with llmman (alternative to Ollama)
|
||||||
|
* [llmman](https://github.com/llmmanorg/llmman) is a local model runner that serves the Ollama API on port `17434`. The bot talks to it exactly as it would to Ollama, only the port differs.
|
||||||
|
* Install it with `curl -fsSL https://raw.githubusercontent.com/llmmanorg/llmman/main/install.sh | sh` (Linux/macOS) or `irm https://raw.githubusercontent.com/llmmanorg/llmman/main/install.ps1 | iex` (Windows).
|
||||||
|
* Start the server with `llmman serve` and pull a model with `llmman pull [model name]`, for example `llmman pull gemma4` or `llmman pull hf.co/unsloth/Qwen3.5-0.8B-GGUF`.
|
||||||
|
* Point the bot at it by setting `LLM_ENDPOINT = 127.0.0.1` and `LLM_PORT = 17434` in your `.env`, then set `MODEL` to a model you pulled. `/pull-model`, `/switch-model` and `/delete-model` work the same way.
|
||||||
|
|
||||||
## To Run Locally (without Docker)
|
## To Run Locally (without Docker)
|
||||||
* Run `npm install` to install the npm packages.
|
* Run `npm install` to install the npm packages.
|
||||||
* Ensure that your [.env](../.env.sample) file's `OLLAMA_IP` is `127.0.0.1` to work properly.
|
* Ensure that your [.env](../.env.sample) file's `LLM_ENDPOINT` is `127.0.0.1` to work properly.
|
||||||
* You only need your `CLIENT_TOKEN`, `OLLAMA_IP`, `OLLAMA_PORT`.
|
* You only need your `CLIENT_TOKEN`, `LLM_ENDPOINT`, `LLM_PORT`.
|
||||||
* The ollama ip and port should just use it's defaults by nature. If not, utilize `OLLAMA_IP = 127.0.0.1` and `OLLAMA_PORT = 11434`.
|
* The LLM server ip and port should just use it's defaults by nature. If not, utilize `LLM_ENDPOINT = 127.0.0.1` and `LLM_PORT = 11434`.
|
||||||
|
* The older `OLLAMA_IP` / `OLLAMA_PORT` names are still accepted as deprecated aliases.
|
||||||
* Now, you can run the bot by running `npm run client` which will build and run the decompiled typescript and run the setup for ollama.
|
* Now, you can run the bot by running `npm run client` which will build and run the decompiled typescript and run the setup for ollama.
|
||||||
* **IMPORTANT**: This must be ran in the wsl/Linux instance to work properly! Using Command Prompt/Powershell/Git Bash/etc. will not work on Windows (at least in my experience).
|
* **IMPORTANT**: This must be ran in the wsl/Linux instance to work properly! Using Command Prompt/Powershell/Git Bash/etc. will not work on Windows (at least in my experience).
|
||||||
* Refer to the [resources](../README.md#resources) on what node version to use.
|
* Refer to the [resources](../README.md#resources) on what node version to use.
|
||||||
|
|||||||
Generated
+1204
-1069
File diff suppressed because it is too large
Load Diff
+6
-3
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.9.1",
|
"version": "0.9.2",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
@@ -34,11 +34,14 @@
|
|||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"tsx": "^4.19.3",
|
"tsx": "^4.19.3",
|
||||||
"typescript": "^5.8.2",
|
"typescript": "^5.8.2",
|
||||||
"vitest": "^3.0.4"
|
"vitest": "^5.0.1"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
"npm": ">=10.9.0",
|
"npm": ">=10.9.0",
|
||||||
"node": ">=22.12.0"
|
"node": ">=24.0.0"
|
||||||
|
},
|
||||||
|
"allowScripts": {
|
||||||
|
"esbuild@0.25.12": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import { Client, GatewayIntentBits } from 'discord.js'
|
import { Client, GatewayIntentBits } from 'discord.js'
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
import { Queue } from './queues/queue.js'
|
import { Queue } from './components/index.js'
|
||||||
import { UserMessage, registerEvents } from './utils/index.js'
|
import { UserMessage, registerEvents } from './utils/index.js'
|
||||||
import Events from './events/index.js'
|
import Events from './events/index.js'
|
||||||
import Keys from './keys.js'
|
import Keys from './keys.js'
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Client, CommandInteraction, Message, MessageFlags, SendableChannels } from "discord.js";
|
import { Client, CommandInteraction, MessageFlags } from "discord.js";
|
||||||
import { accessChannelContext, normalMessage, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
|
import { accessChannelContext, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
|
||||||
import { ollama } from "../client.js"
|
import { ollama } from "../client.js"
|
||||||
|
|
||||||
export const Summary: SlashCommand = {
|
export const Summary: SlashCommand = {
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
/**
|
||||||
|
* @class Logger
|
||||||
|
* @description A class to handle logging messages
|
||||||
|
* @method log
|
||||||
|
*/
|
||||||
|
export class Logger {
|
||||||
|
private logPrefix: string = ''
|
||||||
|
private type: string = 'log'
|
||||||
|
|
||||||
|
private constructPrefix(component?: string, method?: string): string {
|
||||||
|
let prefix = this.type.toUpperCase()
|
||||||
|
|
||||||
|
if (component) {
|
||||||
|
prefix += ` [${component}`
|
||||||
|
if (method) prefix += `: ${method}`
|
||||||
|
prefix += ']'
|
||||||
|
}
|
||||||
|
|
||||||
|
return prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
public bind(component?: string, method?: string): CallableFunction {
|
||||||
|
let tempPrefix = this.constructPrefix(component, method)
|
||||||
|
|
||||||
|
if (tempPrefix !== this.logPrefix) this.logPrefix = tempPrefix
|
||||||
|
|
||||||
|
switch (this.type) {
|
||||||
|
case 'warn':
|
||||||
|
return console.warn.bind(console, this.logPrefix)
|
||||||
|
case 'error':
|
||||||
|
return console.error.bind(console, this.logPrefix)
|
||||||
|
case 'log':
|
||||||
|
default:
|
||||||
|
return console.log.bind(console, this.logPrefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public log(type: string, message: unknown, component?: string, method?: string): void {
|
||||||
|
if (type && type !== this.type) this.type = type
|
||||||
|
|
||||||
|
let log = this.bind(component, method)
|
||||||
|
|
||||||
|
log(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './queue.js'
|
||||||
|
export * from './binder.js'
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { TextChannel } from 'discord.js'
|
import { TextChannel } from 'discord.js'
|
||||||
import { event, Events, normalMessage, UserMessage, clean, addToChannelContext } from '../utils/index.js'
|
|
||||||
import {
|
import {
|
||||||
|
event, Events, normalMessage, UserMessage, clean, addToChannelContext,
|
||||||
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
|
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
|
||||||
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
|
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
|
||||||
} from '../utils/index.js'
|
} from '../utils/index.js'
|
||||||
|
|||||||
+2
-2
@@ -2,8 +2,8 @@ import { getEnvVar } from './utils/index.js'
|
|||||||
|
|
||||||
export const Keys = {
|
export const Keys = {
|
||||||
clientToken: getEnvVar('CLIENT_TOKEN'),
|
clientToken: getEnvVar('CLIENT_TOKEN'),
|
||||||
ipAddress: getEnvVar('OLLAMA_IP', '127.0.0.1'), // default ollama ip if none
|
ipAddress: getEnvVar('LLM_ENDPOINT', process.env.OLLAMA_IP ?? '127.0.0.1'), // OLLAMA_IP is a deprecated alias
|
||||||
portAddress: getEnvVar('OLLAMA_PORT', '11434'), // default ollama port if none
|
portAddress: getEnvVar('LLM_PORT', process.env.OLLAMA_PORT ?? '11434'), // OLLAMA_PORT is a deprecated alias
|
||||||
defaultModel: getEnvVar('MODEL', 'llama3.2')
|
defaultModel: getEnvVar('MODEL', 'llama3.2')
|
||||||
} as const // readonly keys
|
} as const // readonly keys
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ export function getEnvVar(name: string, fallback?: string): string {
|
|||||||
This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`)
|
This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`)
|
||||||
|
|
||||||
// validate IPv4 address found in environment variables
|
// validate IPv4 address found in environment variables
|
||||||
if ((name.endsWith("_IP") || name.endsWith("_ADDRESS")) && !ipValidate.test(value))
|
if ((name.endsWith("_IP") || name.endsWith("_ADDRESS") || name.endsWith("_ENDPOINT")) && !ipValidate.test(value))
|
||||||
throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`)
|
throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`)
|
||||||
|
|
||||||
// return env variable
|
// return env variable
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
import type { ClientEvents, Awaitable, Client } from 'discord.js'
|
import type { ClientEvents, Awaitable, Client } from 'discord.js'
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
import { Queue } from '../queues/queue.js'
|
import { Queue } from '../components/index.js'
|
||||||
|
|
||||||
// Export events through here to reduce amount of imports
|
// Export events through here to reduce amount of imports
|
||||||
export { Events } from 'discord.js'
|
export { Events } from 'discord.js'
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ export async function accessChannelContext(filename: string, callback: (config:
|
|||||||
* @param user the user's name
|
* @param user the user's name
|
||||||
* @param messages their messages
|
* @param messages their messages
|
||||||
*/
|
*/
|
||||||
export async function openChannelInfo(filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
export async function openChannelInfo(this: any, filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
||||||
const fullFileName = `data/${filename}-${user}.json`
|
const fullFileName = `data/${filename}-${user}.json`
|
||||||
if (fs.existsSync(fullFileName)) {
|
if (fs.existsSync(fullFileName)) {
|
||||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
||||||
@@ -143,7 +143,7 @@ export async function openChannelInfo(filename: string, channel: TextChannel | T
|
|||||||
|
|
||||||
// only creating it, no need to add anything
|
// only creating it, no need to add anything
|
||||||
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
||||||
console.log(`[Util: openChannelInfo] Created '${fullFileName}' in working directory`)
|
console.log(`[Util: ${this.name}] Created '${fullFileName}' in working directory`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import path from 'path'
|
|||||||
* @param value new value to assign
|
* @param value new value to assign
|
||||||
*/
|
*/
|
||||||
// add type of change (server, user)
|
// add type of change (server, user)
|
||||||
export function openConfig(filename: string, key: string, value: any) {
|
export function openConfig(this: any, filename: string, key: string, value: any) {
|
||||||
const fullFileName = `data/${filename}`
|
const fullFileName = `data/${filename}`
|
||||||
|
|
||||||
// check if the file exists, if not then make the config file
|
// check if the file exists, if not then make the config file
|
||||||
@@ -41,7 +41,7 @@ export function openConfig(filename: string, key: string, value: any) {
|
|||||||
fs.mkdirSync(directory, { recursive: true })
|
fs.mkdirSync(directory, { recursive: true })
|
||||||
|
|
||||||
fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2))
|
fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2))
|
||||||
console.log(`[Util: openConfig] Created '${filename}' in working directory`)
|
console.log(`[Util: ${this.name}] Created '${filename}' in working directory`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChatResponse } from "ollama"
|
import { ChatResponse, AbortableAsyncIterator } from "ollama"
|
||||||
import { ChatParams } from "../index.js"
|
import { ChatParams } from "../index.js"
|
||||||
import { AbortableAsyncIterator } from "ollama/src/utils.js"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to query the Ollama client for async generation
|
* Method to query the Ollama client for async generation
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { Message, SendableChannels } from 'discord.js'
|
import { Message, SendableChannels } from 'discord.js'
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
import { ChatResponse, Ollama, AbortableAsyncIterator } from 'ollama'
|
||||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
||||||
import { Queue } from '../queues/queue.js'
|
import { Queue } from '../components/index.js'
|
||||||
import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to send replies as normal text on discord like any other user
|
* Method to send replies as normal text on discord like any other user
|
||||||
@@ -11,6 +10,7 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
|||||||
* @param msgHist message history between user and model
|
* @param msgHist message history between user and model
|
||||||
*/
|
*/
|
||||||
export async function normalMessage(
|
export async function normalMessage(
|
||||||
|
this: any,
|
||||||
message: Message,
|
message: Message,
|
||||||
ollama: Ollama,
|
ollama: Ollama,
|
||||||
model: string,
|
model: string,
|
||||||
@@ -77,12 +77,11 @@ export async function normalMessage(
|
|||||||
sentMessage.edit(result)
|
sentMessage.edit(result)
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
console.log(`[Util: ${this.name}] Error creating message: ${error.message}`)
|
||||||
if (error.message.includes('fetch failed'))
|
if (error.message.includes('try pulling it first'))
|
||||||
error.message = 'Missing ollama service on machine'
|
sentMessage.edit(`**Response generation failed.**\n\nReason: You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`)
|
||||||
else if (error.message.includes('try pulling it first'))
|
else
|
||||||
error.message = `You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`
|
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
||||||
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,6 @@ describe('Events Existence', () => {
|
|||||||
// test specific events in the object
|
// test specific events in the object
|
||||||
it('references specific events', () => {
|
it('references specific events', () => {
|
||||||
const eventsString = events.map(e => e.key.toString()).join(', ')
|
const eventsString = events.map(e => e.key.toString()).join(', ')
|
||||||
expect(eventsString).toBe('ready, messageCreate, interactionCreate, threadDelete')
|
expect(eventsString).toBe('clientReady, messageCreate, interactionCreate, threadDelete')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -47,4 +47,28 @@ describe('Environment Setup', () => {
|
|||||||
it('throws an error if key is not found', () => {
|
it('throws an error if key is not found', () => {
|
||||||
expect(() => getEnvVar('NON_EXISTENT_KEY')).toThrowError()
|
expect(() => getEnvVar('NON_EXISTENT_KEY')).toThrowError()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// test that *_ENDPOINT keys are validated as IPv4 addresses like *_IP keys
|
||||||
|
it('validates *_ENDPOINT keys as IPv4', () => {
|
||||||
|
expect(getEnvVar('UNSET_TEST_ENDPOINT', '127.0.0.1')).toBe('127.0.0.1')
|
||||||
|
expect(() => getEnvVar('UNSET_TEST_ENDPOINT', 'not-an-ip')).toThrowError()
|
||||||
|
})
|
||||||
|
|
||||||
|
// test the deprecated alias lookup used in src/keys.ts
|
||||||
|
it('falls back to the deprecated OLLAMA_IP / OLLAMA_PORT aliases', () => {
|
||||||
|
const saved = { LLM_ENDPOINT: process.env.LLM_ENDPOINT, LLM_PORT: process.env.LLM_PORT }
|
||||||
|
delete process.env.LLM_ENDPOINT
|
||||||
|
delete process.env.LLM_PORT
|
||||||
|
process.env.OLLAMA_IP = '10.0.0.5'
|
||||||
|
process.env.OLLAMA_PORT = '17434'
|
||||||
|
try {
|
||||||
|
expect(getEnvVar('LLM_ENDPOINT', process.env.OLLAMA_IP ?? '127.0.0.1')).toBe('10.0.0.5')
|
||||||
|
expect(getEnvVar('LLM_PORT', process.env.OLLAMA_PORT ?? '11434')).toBe('17434')
|
||||||
|
} finally {
|
||||||
|
delete process.env.OLLAMA_IP
|
||||||
|
delete process.env.OLLAMA_PORT
|
||||||
|
if (saved.LLM_ENDPOINT !== undefined) process.env.LLM_ENDPOINT = saved.LLM_ENDPOINT
|
||||||
|
if (saved.LLM_PORT !== undefined) process.env.LLM_PORT = saved.LLM_PORT
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { Queue } from '../src/queues/queue.js'
|
import { Queue } from '../src/components/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue test suite, tests the Queue class
|
* Queue test suite, tests the Queue class
|
||||||
|
|||||||
Reference in New Issue
Block a user