Compare commits

...

3 Commits

Author SHA1 Message Date
Kevin Dang
78921ee571 added persistence in chat endpoint 2024-01-23 22:24:26 -08:00
Kevin Dang
f8956b0b50 bot can edit message response 2024-01-23 21:26:39 -08:00
Kevin Dang
70103c1f5a readme ollama setup 2024-01-22 23:24:32 -08:00
6 changed files with 79 additions and 49 deletions

View File

@@ -1,9 +1,23 @@
# Discord Ollama Integration [![License: CC BY-NC 4.0](https://img.shields.io/badge/License-CC_BY--NC_4.0-darkgreen.svg)](https://creativecommons.org/licenses/by-nc/4.0/) [![Release Badge](https://img.shields.io/github/v/release/kevinthedang/discord-ollama?logo=github)](https://github.com/kevinthedang/discord-ollama/releases/latest) # Discord Ollama Integration [![License: CC BY-NC 4.0](https://img.shields.io/badge/License-CC_BY--NC_4.0-darkgreen.svg)](https://creativecommons.org/licenses/by-nc/4.0/) [![Release Badge](https://img.shields.io/github/v/release/kevinthedang/discord-ollama?logo=github)](https://github.com/kevinthedang/discord-ollama/releases/latest)
Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord! Ollama is an AI model management tool that allows users to install and use custom large language models locally. The goal is to create a discord bot that will utilize Ollama and chat with it on a Discord!
## Ollama Setup
* Go to Ollama's [Linux download page](https://ollama.ai/download/linux) and run the simple curl command they provide. The command should be `curl https://ollama.ai/install.sh | sh`.
* Now the the following commands in separate terminals to test out how it works!
* In terminal 1 -> `ollama serve` to setup ollama
* In terminal 2 -> `ollama run [model name]`, for example `ollama run llama2`
* The models can vary as you can create your own model. You can also view ollama's [library](https://ollama.ai/library) of models.
* This can also be done in [wsl](https://learn.microsoft.com/en-us/windows/wsl/install) for Windows machines.
* You can now interact with the model you just ran (it might take a second to startup).
* Response time varies with processing power!
## To Run ## To Run
* 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 can run the bot by running `npm run start` which will build and run the decompiled typescript. * Run `npm install` to install the npm packages.
* You will need a `.env` file in the root of the project directory with the bot's token.
* For example, `CLIENT_TOKEN = [Bot Token]`
* Now, you can run the bot by running `npm run start` 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).
* Refer to the [resources](#resources) on what node version to use. * Refer to the [resources](#resources) on what node version to use.
## Resources ## Resources
@@ -13,8 +27,8 @@ Ollama is an AI model management tool that allows users to install and use custo
* To run dev with `tsx`, you can use `v20.10.0` or earlier. * To run dev with `tsx`, you can use `v20.10.0` or earlier.
* This project supports any NodeJS version above `16.x.x` to only allow ESModules. * This project supports any NodeJS version above `16.x.x` to only allow ESModules.
* [Ollama](https://ollama.ai/) * [Ollama](https://ollama.ai/)
* [Docker Documentation](https://docs.docker.com/?_gl=1*nof6f8*_ga*MTQxNTc1MTYxOS4xNzAxNzI1ODAx*_ga_XJWPQMJYHQ*MTcwMjQxODUzOS4yLjEuMTcwMjQxOTgyMC41OS4wLjA.)
* [Discord Developer Portal](https://discord.com/developers/docs/intro) * [Discord Developer Portal](https://discord.com/developers/docs/intro)
* [Discord.js Docs](https://discord.js.org/docs/packages/discord.js/main)
## Acknowledgement ## Acknowledgement
* [Kevin Dang](https://github.com/kevinthedang) * [Kevin Dang](https://github.com/kevinthedang)

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.0.1", "version": "0.1.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.0.1", "version": "0.1.1",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"axios": "^1.6.2", "axios": "^1.6.2",

View File

@@ -1,6 +1,6 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.0.1", "version": "0.1.1",
"description": "Ollama Integration into discord", "description": "Ollama Integration into discord",
"main": "dist/index.js", "main": "dist/index.js",
"exports": "./dist/index.js", "exports": "./dist/index.js",

View File

@@ -14,7 +14,14 @@ const client = new Client({
] ]
}); });
registerEvents(client, Events) const messageHistory = [
{
role: 'assistant',
content: 'My name is Ollama GU.'
}
]
registerEvents(client, Events, messageHistory)
// Try to log in the client // Try to log in the client
client.login(Keys.clientToken) client.login(Keys.clientToken)

View File

@@ -4,39 +4,47 @@ import Axios from 'axios'
/* /*
* Max Message length for free users is 2000 characters (bot or not). * Max Message length for free users is 2000 characters (bot or not).
*/ */
export default event(Events.MessageCreate, ({ log }, message) => { export default event(Events.MessageCreate, ({ log, msgHist }, message) => {
log(`Message created \"${message.content}\" from ${message.author.tag}.`) log(`Message created \"${message.content}\" from ${message.author.tag}.`)
// Hard-coded channel to test output there only, in our case "ollama-endpoint" // Hard-coded channel to test output there only, in our case "ollama-endpoint"
if (message.channelId != '1188262786497785896') { if (message.channelId != '1188262786497785896') return
log(`Unauthorized Channel input, Aborting...`)
return
}
log(`Channel id OK!`)
// Do not respond if bot talks in the chat // Do not respond if bot talks in the chat
if (message.author.tag === message.client.user.tag) { if (message.author.tag === message.client.user.tag) return
log(`Found Bot message reply, Aborting...`)
return
}
log(`Sender Checked!`)
// Request made to API // push user response
const request = async () => { msgHist.push({
try { role: 'user',
const response = await Axios.post('http://127.0.0.1:11434/api/generate', { content: message.content
model: 'llama2', })
prompt: message.content,
stream: false // Reply with something to prompt that ollama is working
}) message.reply("Generating Response . . .").then(sentMessage => {
log(response.data) // Request made to API
// const request = async () => {
message.reply(response.data['response']) try {
} catch (error) { // change this when using an actual hosted server or use ollama.js
log(error) const response = await Axios.post('http://127.0.0.1:11434/api/chat', {
} model: 'llama2',
} messages: msgHist,
stream: false
})
log(response.data)
// Attempt to call ollama's endpoint sentMessage.edit(response.data.message.content)
request()
// push bot response
msgHist.push({
role: 'assistant',
content: response.data.message.content
})
} catch (error) {
log(error)
}
}
// Attempt to call ollama's endpoint
request()
})
}) })

View File

@@ -1,43 +1,44 @@
import type { ClientEvents, Awaitable, Client } from 'discord.js'; import type { ClientEvents, Awaitable, Client } from 'discord.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'
export type LogMethod = (...args: unknown[]) => void; export type LogMethod = (...args: unknown[]) => void
export type EventKeys = keyof ClientEvents; // only wants keys of ClientEvents object export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
// Event properties // Event properties
export interface EventProps { export interface EventProps {
client: Client; client: Client
log: LogMethod; log: LogMethod
msgHist: { role: string, content: string }[]
} }
export type EventCallback<T extends EventKeys> = ( export type EventCallback<T extends EventKeys> = (
props: EventProps, props: EventProps,
...args: ClientEvents[T] ...args: ClientEvents[T]
) => Awaitable<unknown>; // Method can be synchronous or async, unknown so we can return anything ) => Awaitable<unknown> // Method can be synchronous or async, unknown so we can return anything
// Event interface // Event interface
export interface Event<T extends EventKeys = EventKeys> { export interface Event<T extends EventKeys = EventKeys> {
key: T; key: T
callback: EventCallback<T>; callback: EventCallback<T>
} }
export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> { export function event<T extends EventKeys>(key: T, callback: EventCallback<T>): Event<T> {
return { key, callback }; return { key, callback }
} }
export function registerEvents(client: Client, events: Event[]): void { export function registerEvents(client: Client, events: Event[], msgHist: { role: string, content: string }[]): void {
for (const { key, callback } of events) { for (const { key, callback } of events) {
client.on(key, (...args) => { client.on(key, (...args) => {
// Create a new log method for this event // Create a new log method for this event
const log = console.log.bind(console, `[Event: ${key}]`); const log = console.log.bind(console, `[Event: ${key}]`)
// Handle Errors, call callback, log errors as needed // Handle Errors, call callback, log errors as needed
try { try {
callback({ client, log }, ...args); callback({ client, log, msgHist }, ...args)
} catch (error) { } catch (error) {
log('[Uncaught Error]', error); log('[Uncaught Error]', error)
} }
}); })
} }
} }