Compare commits
12 Commits
e12f518bd7
...
ba9aeaeb3e
Author | SHA1 | Date | |
---|---|---|---|
|
ba9aeaeb3e | ||
|
7b3e0c1db2 | ||
|
5f8e57d121 | ||
|
cc770e617d | ||
|
5a56251e20 | ||
|
6bb74c8020 | ||
|
56c7bfd26d | ||
|
5793b7b4ad | ||
|
cf6e47eebc | ||
|
f98caa23cc | ||
|
d3162bce32 | ||
|
ec7dbde761 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
.env
|
.env
|
||||||
|
node_modules/
|
||||||
|
package-lock.json
|
10
Dockerfile
Normal file
10
Dockerfile
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
FROM node:slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
RUN npm install --omit=dev
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
CMD node llamabot.js
|
19
README.md
19
README.md
@ -32,6 +32,9 @@ Define a generateResponse function that sends a request to the GPT-3 API to gene
|
|||||||
|
|
||||||
Call the generateResponse function within the messageCreate event listener function.
|
Call the generateResponse function within the messageCreate event listener function.
|
||||||
|
|
||||||
|
![demo](https://media.discordapp.net/attachments/562897071326101515/1095738407826767922/image.png?width=1038&height=660 "demo")
|
||||||
|
|
||||||
|
|
||||||
# Backend REQUIIRED
|
# Backend REQUIIRED
|
||||||
|
|
||||||
The HTTP Server from https://abetlen.github.io/llama-cpp-python/ is required to use this bot.
|
The HTTP Server from https://abetlen.github.io/llama-cpp-python/ is required to use this bot.
|
||||||
@ -48,7 +51,7 @@ python3 -m llama_cpp.server
|
|||||||
|
|
||||||
Navigate to http://localhost:8000/docs to see the OpenAPI documentation.
|
Navigate to http://localhost:8000/docs to see the OpenAPI documentation.
|
||||||
|
|
||||||
# Usage
|
# Static Usage
|
||||||
|
|
||||||
1) Use ```npm i ```
|
1) Use ```npm i ```
|
||||||
|
|
||||||
@ -60,5 +63,19 @@ Navigate to http://localhost:8000/docs to see the OpenAPI documentation.
|
|||||||
|
|
||||||
6) Run the bot ```node llamabot.js ```
|
6) Run the bot ```node llamabot.js ```
|
||||||
|
|
||||||
|
# Docker Compose
|
||||||
|
This will automatically configure the API for you as well as the bot in two seperate containers within a stack.
|
||||||
|
|
||||||
|
1. `git clone https://git.ssh.surf/snxraven/llama-cpp-python-djs-bot.git`
|
||||||
|
|
||||||
|
|
||||||
|
2. `cp default.env .env`
|
||||||
|
|
||||||
|
3. Set DATA_DIR in .env to the exact location of your model files.
|
||||||
|
|
||||||
|
4. Edit docker-compose.yaml MODEL to ensure the correct model bin is set
|
||||||
|
|
||||||
|
5. `docker compose up -d`
|
||||||
|
|
||||||
|
|
||||||
Want to make this better? Issue a pull request!
|
Want to make this better? Issue a pull request!
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const emptyResponses = [
|
export const emptyResponses = [
|
||||||
"Oh boy, this is a tough one! Unfortunately, I don't have much insight to share on this topic.",
|
"Oh boy, this is a tough one! Unfortunately, I don't have much insight to share on this topic.",
|
||||||
"Hmm, I'm scratching my head on this one. Sorry to say that I don't have much to offer.",
|
"Hmm, I'm scratching my head on this one. Sorry to say that I don't have much to offer.",
|
||||||
"Wish I had a witty remark, but alas, I don't have much to contribute to this discussion.",
|
"Wish I had a witty remark, but alas, I don't have much to contribute to this discussion.",
|
||||||
@ -28,5 +28,3 @@ const emptyResponses = [
|
|||||||
"I'm afraid I don't have much experience with this, so I can't be of much help.",
|
"I'm afraid I don't have much experience with this, so I can't be of much help.",
|
||||||
"I wish I had some grand insight to share, but alas, I don't have much to offer in that regard."
|
"I wish I had some grand insight to share, but alas, I don't have much to offer in that regard."
|
||||||
];
|
];
|
||||||
|
|
||||||
module.exports = emptyResponses;
|
|
@ -1,4 +1,4 @@
|
|||||||
const errorMessages = [
|
export const errorMessages = [
|
||||||
"Uh oh, looks like something went awry! Try !reset to start fresh.",
|
"Uh oh, looks like something went awry! Try !reset to start fresh.",
|
||||||
"Oops, we hit a bump in the road! Give !reset a try to start anew.",
|
"Oops, we hit a bump in the road! Give !reset a try to start anew.",
|
||||||
"We've encountered an error, but !reset can help us out! Give it a go.",
|
"We've encountered an error, but !reset can help us out! Give it a go.",
|
||||||
@ -21,7 +21,7 @@ const errorMessages = [
|
|||||||
"Oopsie daisy! Give !reset a try and we'll start over."
|
"Oopsie daisy! Give !reset a try and we'll start over."
|
||||||
];
|
];
|
||||||
|
|
||||||
const busyResponses = [
|
export const busyResponses = [
|
||||||
"Sorry about that! Looks like I'm tied up at the moment. Please try again later.",
|
"Sorry about that! Looks like I'm tied up at the moment. Please try again later.",
|
||||||
"Oops, I'm currently busy with something else. Please try again later.",
|
"Oops, I'm currently busy with something else. Please try again later.",
|
||||||
"Looks like I'm already working on something. Can you try again later?",
|
"Looks like I'm already working on something. Can you try again later?",
|
||||||
@ -43,5 +43,3 @@ const errorMessages = [
|
|||||||
"Looks like I'm currently engaged with something else. Please try again later.",
|
"Looks like I'm currently engaged with something else. Please try again later.",
|
||||||
"I'm currently unavailable. Can you try again later?"
|
"I'm currently unavailable. Can you try again later?"
|
||||||
];
|
];
|
||||||
|
|
||||||
module.exports = { errorMessages, busyResponses};
|
|
@ -1,4 +1,4 @@
|
|||||||
const resetResponses = [
|
export const resetResponses = [
|
||||||
"Whoops, let's start fresh! What can I assist you with now?",
|
"Whoops, let's start fresh! What can I assist you with now?",
|
||||||
"Looks like we need a fresh start! What do you need help with?",
|
"Looks like we need a fresh start! What do you need help with?",
|
||||||
"To avoid any gremlins in the system, let's reset! How can I assist you now?",
|
"To avoid any gremlins in the system, let's reset! How can I assist you now?",
|
||||||
@ -21,7 +21,7 @@ const resetResponses = [
|
|||||||
"Let's hit the restart button to make sure we're on the right track. What can I help you with now?"
|
"Let's hit the restart button to make sure we're on the right track. What can I help you with now?"
|
||||||
];
|
];
|
||||||
|
|
||||||
const userResetMessages = [
|
export const userResetMessages = [
|
||||||
"All good, we're starting fresh! How can I assist you?",
|
"All good, we're starting fresh! How can I assist you?",
|
||||||
"Got it, let's start over! How can I help you today?",
|
"Got it, let's start over! How can I help you today?",
|
||||||
"Alright, starting anew! What can I help you with?",
|
"Alright, starting anew! What can I help you with?",
|
||||||
@ -43,5 +43,3 @@ const resetResponses = [
|
|||||||
"Sure thing, we'll start over! What can I help you with today?",
|
"Sure thing, we'll start over! What can I help you with today?",
|
||||||
"Conversation reset, confirmed! What do you need help with?"
|
"Conversation reset, confirmed! What do you need help with?"
|
||||||
];
|
];
|
||||||
|
|
||||||
module.exports = {resetResponses, userResetMessages};
|
|
@ -1,4 +1,6 @@
|
|||||||
THE_TOKEN = "DISCORD_TOKEN_HERE"
|
THE_TOKEN = "DISCORD_TOKEN_HERE"
|
||||||
CHANNEL_IDS = 1094494101631680653,1094628334727614605
|
CHANNEL_IDS = 1094494101631680653,1094628334727614605
|
||||||
|
# ROOT_IP is only used when running the bot without docker compose
|
||||||
ROOT_IP = 192.168.0.15
|
ROOT_IP = 192.168.0.15
|
||||||
ROOT_PORT = 8000
|
ROOT_PORT = 8000
|
||||||
|
DATA_DIR = /home/USERNAME/weights
|
||||||
|
27
docker-compose.yml
Normal file
27
docker-compose.yml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
version: '3.9'
|
||||||
|
|
||||||
|
services:
|
||||||
|
llama-python-server:
|
||||||
|
container_name: llama-python-server
|
||||||
|
restart: unless-stopped
|
||||||
|
build:
|
||||||
|
context: ./server
|
||||||
|
env_file: .env
|
||||||
|
volumes:
|
||||||
|
- ${DATA_DIR}/weights:/usr/src/app/models
|
||||||
|
environment:
|
||||||
|
- HOST=llama-python-server
|
||||||
|
- MODEL=./models/30B.bin
|
||||||
|
llama-python-djs-bot:
|
||||||
|
container_name: llama-python-djs-bot
|
||||||
|
restart: unless-stopped
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
depends_on:
|
||||||
|
- llama-python-server
|
||||||
|
environment:
|
||||||
|
- THE_TOKEN
|
||||||
|
- CHANNEL_IDS
|
||||||
|
- ROOT_IP=llama-python-server
|
||||||
|
- ROOT_PORT=8000
|
||||||
|
- CACHE=1
|
37
llamabot.js
37
llamabot.js
@ -1,23 +1,16 @@
|
|||||||
const Discord = require('discord.js');
|
import "dotenv/config.js";
|
||||||
const fetch = require('node-fetch');
|
import fetch from 'node-fetch';
|
||||||
const emptyResponses = require('./assets/emptyMessages.js');
|
import { emptyResponses } from './assets/emptyMessages.js';
|
||||||
const { resetResponses, userResetMessages } = require('./assets/resetMessages.js');
|
import { resetResponses, userResetMessages } from './assets/resetMessages.js';
|
||||||
const { errorMessages, busyResponses } = require('./assets/errorMessages.js');
|
import { errorMessages, busyResponses } from './assets/errorMessages.js';
|
||||||
|
|
||||||
require('dotenv').config()
|
import { Client, GatewayIntentBits, ActivityType, Partials } from 'discord.js';
|
||||||
|
|
||||||
const {
|
|
||||||
Client,
|
|
||||||
GatewayIntentBits,
|
|
||||||
ActivityType,
|
|
||||||
Partials
|
|
||||||
} = require('discord.js');
|
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [
|
intents: [
|
||||||
GatewayIntentBits.DirectMessages,
|
GatewayIntentBits.DirectMessages,
|
||||||
GatewayIntentBits.Guilds,
|
GatewayIntentBits.Guilds,
|
||||||
GatewayIntentBits.GuildBans,
|
GatewayIntentBits.GuildModeration,
|
||||||
GatewayIntentBits.GuildMessages,
|
GatewayIntentBits.GuildMessages,
|
||||||
GatewayIntentBits.MessageContent,
|
GatewayIntentBits.MessageContent,
|
||||||
],
|
],
|
||||||
@ -111,15 +104,15 @@ client.on('messageCreate', async (message) => {
|
|||||||
if (conversation.messages.length === 0) {
|
if (conversation.messages.length === 0) {
|
||||||
conversation.messages.push({
|
conversation.messages.push({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `Your name is rAi, you code, write and provide any information without any mistakes.`
|
content: ` Assistant name: ChatAI. You code, write and provide any information without any mistakes.`
|
||||||
});
|
});
|
||||||
conversation.messages.push({
|
conversation.messages.push({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: `My name is ${message.author.username}.`
|
content: ` User name: ${message.author.username}.`
|
||||||
});
|
});
|
||||||
conversation.messages.push({
|
conversation.messages.push({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: `Hello, ${message.author.username}, how may I help you?`
|
content: ` Hello, ${message.author.username}, how may I help you?`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +125,7 @@ client.on('messageCreate', async (message) => {
|
|||||||
// Append user message to conversation history
|
// Append user message to conversation history
|
||||||
conversation.messages.push({
|
conversation.messages.push({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: message.cleanContent
|
content: ` ${message.cleanContent}`
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -197,12 +190,10 @@ async function generateResponse(conversation) {
|
|||||||
console.log(JSON.stringify(responseData))
|
console.log(JSON.stringify(responseData))
|
||||||
const choice = responseData.choices[0];
|
const choice = responseData.choices[0];
|
||||||
|
|
||||||
// Remove "user None:" and any text after it from the response
|
const responseText = choice.message.content
|
||||||
const responseText = choice.message.content.trim();
|
|
||||||
const startIndex = responseText.indexOf('user None:');
|
return responseText;
|
||||||
const sanitizedResponse = startIndex === -1 ? responseText : responseText.substring(0, startIndex);
|
|
||||||
|
|
||||||
return sanitizedResponse;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} finally {
|
} finally {
|
||||||
|
17
package.json
Normal file
17
package.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "llama-cpp-python-djs-bot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "llamabot.js",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"discord.js": "^14.9.0",
|
||||||
|
"dotenv": "^16.0.3",
|
||||||
|
"node-fetch": "^3.3.1"
|
||||||
|
}
|
||||||
|
}
|
11
server/Dockerfile
Normal file
11
server/Dockerfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FROM python:bullseye
|
||||||
|
|
||||||
|
RUN apt-get update; \
|
||||||
|
apt-get install -y --no-install-recommends \
|
||||||
|
build-essential
|
||||||
|
|
||||||
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir llama-cpp-python[server]
|
||||||
|
|
||||||
|
CMD python3 -m llama_cpp.server
|
Loading…
Reference in New Issue
Block a user