kevinthedang / discord-ollama

Discord Bot that utilizes Ollama to interact with any Large Language Models to talk with users and allow them to host/create their own models.
Creative Commons Attribution 4.0 International
81 stars 9 forks source link

The data/ directory does not self create on chat when it does not exists #101

Closed kikoferrer closed 2 months ago

kikoferrer commented 2 months ago

I don't know what I am doing wrong but it is not responding. There are no errors in the terminal but there are no responses in discord. both app and web. I have ollama open and installed. I am trying to add a custom model though. Do I need a generic model from ollama?

testing

testing2

kevinthedang commented 2 months ago

Hiya there @SmoothBrainApe!

Are you able to provide your .env file for the project? I provided a .env.sample that you can follow to set it up.

id token of a discord server

GUILD_ID = MY_GUILD_ID

model for the bot to query from (i.e. llama2 [llama2:13b], mistral, ... )

MODEL = codellama # in this case your custom model name whatever shows up

discord bot user id for mentions

CLIENT_UID = MY_APP_UID

ip/port address of docker container, I use 172.18.0.2 for docker per docker-compose file, 127.0.0.1 for local

OLLAMA_IP = 127.0.0.1 OLLAMA_PORT = 11434

ip address for discord bot container

DISCORD_IP = 172.18.0.3

subnet address, ex. 172.18.0.0 as we use /16.

SUBNET_ADDRESS = 172.18.0.0


If the custom model is already on your system, it should show up upon running `ollama list`
```sh
$ ollama list
NAME                    ID              SIZE    MODIFIED
codellama:latest        8fdf8f752f6e    3.8 GB  3 months ago

Personally. I have not tested much with Modelfiles but the application should function as normal with any model within your working environment.

To replicate your issue, my CLIENT_UID had to be missing, is this the case? Please let me know :)

kevinthedang commented 2 months ago

@SmoothBrainApe I've created a custom model called mario as reference on the official ollama README.md using the Modelfile:

FROM codellama:latest

# set the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 1

# set the system message
SYSTEM """
You are Mario from Super Mario Bros. Answer as Mario, the assistant, only.
"""

It should be visible when running ollama list after it completes the layers it needs.

$ ollama list
NAME                    ID              SIZE    MODIFIED      
codellama:latest        8fdf8f752f6e    3.8 GB  3 months ago
mario:latest            9cad62210470    3.8 GB  4 seconds ago

Then I referenced it in my .env like shown above but used mario

I then ran the bot using npm run client and received the following:

image

Logs provided:

$ npm run client

> discord-ollama@0.5.8 client  
> npm run build && npm run prod

> discord-ollama@0.5.8 build
> tsc

> discord-ollama@0.5.8 prod
> node .

[Event: ready] Logged in as Ollama GU.
[Command: thread] Registered on Discord
[Command: private-thread] Registered on Discord
[Command: message-style] Registered on Discord
  - embed
[Command: message-stream] Registered on Discord
  - stream
[Command: toggle-chat] Registered on Discord
  - enabled
[Event: messageCreate] Message "Hello there!" from kevinthedang in channel/thread 1188262786497785896.
[Event: messageCreate] Capacity is undefined, using default capacity of 5.
[Event: messageCreate] Message "Generating Response . . ." from Ollama GU#1572 in channel/thread 1188262786497785896.
[Command: shutoff] Registered on Discord
  - are-you-sure
[Command: modify-capacity] Registered on Discord
  - context-capacity
[Command: channel-toggle] Registered on Discord
  - toggle-channel
[Command: clear-user-channel-history] Registered on Discord

References

kikoferrer commented 2 months ago

I have this .env file

# Discord token for the bot
CLIENT_TOKEN = # followed from the docs

# id token of a discord server
GUILD_ID = # followed from the docs too

# model for the bot to query from (i.e. llama2 [llama2:13b], mistral, codellama, etc... )
MODEL = bot # I created a modelfile named 'bot' to test

# discord bot user id for mentions
CLIENT_UID = Testbot

# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local
OLLAMA_IP = 127.0.0.1
OLLAMA_PORT = 11434

# ip address for discord bot container, I use 172.18.0.2, use different IP than ollama_ip
DISCORD_IP = IP_ADDRESS

# subnet address, ex. 172.18.0.0 as we use /16.
SUBNET_ADDRESS = ADDRESS

Do I need to put something in DISCORD_IP and SUBNET_ADDRESS? I installed without docker if that helps.

my bot is named Testbot so I put in CLIENT_UID = Testbot. I just tried again and still no response. no error whatsoever.

Edit: Looking at your log, my log is missing these lines

[Event: messageCreate] Capacity is undefined, using default capacity of 5.
[Event: messageCreate] Message "Generating Response . . ." from Ollama GU#1572 in channel/thread 1188262786497785896.

Do I need to do ollama run 'model_name' first before running the client?

kevinthedang commented 2 months ago

@SmoothBrainApe Oh I see it here.

TLDR: change CLIENT_UID = Testbot to CLIENT_UID = 1269234765316231249

img

I underlined your bots CLIENT_UID above and that should be filtered out whenever you query the application.

I have replicated logs on my end with your issue: image

$ npm run client

> discord-ollama@0.5.8 client  
> npm run build && npm run prod

> discord-ollama@0.5.8 build
> tsc

> discord-ollama@0.5.8 prod
> node .

[Event: ready] Logged in as Ollama GU.
[Command: thread] Registered on Discord
[Command: private-thread] Registered on Discord
[Command: message-style] Registered on Discord
  - embed
[Command: message-stream] Registered on Discord
  - stream
[Command: toggle-chat] Registered on Discord
  - enabled
[Command: shutoff] Registered on Discord
  - are-you-sure
[Command: modify-capacity] Registered on Discord
  - context-capacity
[Command: channel-toggle] Registered on Discord
  - toggle-channel
[Command: clear-user-channel-history] Registered on Discord
[Event: messageCreate] Message "<@1184699294314410115> Hi there!" from kevinthedang in channel/thread 1188262786497785896.
[Event: messageCreate] Message "<@1184699294314410115> Oops! I do not have my id set for ya!" from kevinthedang in channel/thread 1188262786497785896.

You need to set 1269234765316231249 as your CLIENT_UID in a file called .env in the root of the project folder.

If this is the case already, please let me know.

kikoferrer commented 2 months ago

I changed it as you instructed and got this error instead

> discord-ollama@0.5.8 client
> npm run build && npm run prod

> discord-ollama@0.5.8 build
> tsc

> discord-ollama@0.5.8 prod
> node .

[Event: ready] Logged in as Testbot.
[Command: thread] Registered on Discord
[Command: private-thread] Registered on Discord
[Command: message-style] Registered on Discord
  - embed
[Command: message-stream] Registered on Discord
  - stream
[Command: toggle-chat] Registered on Discord
  - enabled
[Event: messageCreate] Message "Hi" from kikopogi22 in channel/thread 1269233205181943891.
node:fs:581
  return binding.open(
                 ^

Error: ENOENT: no such file or directory, open 'data/1267432269111627787-config.json'
    at Object.openSync (node:fs:581:18)
    at Object.writeFileSync (node:fs:2345:35)
    at openConfig (file:///home/chupz22/discord-ollama/build/utils/handlers/configHandler.js:35:12)
    at file:///home/chupz22/discord-ollama/build/events/messageCreate.js:27:21
    at getServerConfig (file:///home/chupz22/discord-ollama/build/utils/handlers/configHandler.js:58:9)
    at serverConfig (file:///home/chupz22/discord-ollama/build/events/messageCreate.js:23:13)
    at new Promise (<anonymous>)
    at file:///home/chupz22/discord-ollama/build/events/messageCreate.js:22:36
    at Client.<anonymous> (file:///home/chupz22/discord-ollama/build/utils/events.js:21:17)
    at Client.emit (node:events:519:28) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'data/1267432269111627787-config.json'
}

Node.js v21.2.0
kevinthedang commented 2 months ago

Oh lovely...

Seems like the app uid changed somehow?

Try CLIENT_UID = 1269236096680722524 since I see it in your logs. I will test the error you have right now.

EDIT: I see you just changed it. I will investigate the issue right now.

Notes

kikoferrer commented 2 months ago

I did notice that which was wierd so I did change the client id. I believe something is happening on the messageCreate.js file that is not working well with my case. I will keep trying to tweak things to my end hoping it would work. Thanks for the assistance.

kevinthedang commented 2 months ago

Alright I ran without an server configuration as that what it seems to be getting mad about and got this:

image

$ npm run client

> discord-ollama@0.5.8 client  
> npm run build && npm run prod

> discord-ollama@0.5.8 build
> tsc

> discord-ollama@0.5.8 prod
> node .

[Event: ready] Logged in as Ollama GU.
[Command: thread] Registered on Discord
[Command: private-thread] Registered on Discord
[Command: message-style] Registered on Discord
  - embed
[Command: message-stream] Registered on Discord
  - stream
[Command: toggle-chat] Registered on Discord
  - enabled
[Command: shutoff] Registered on Discord
  - are-you-sure
[Command: modify-capacity] Registered on Discord
  - context-capacity
[Command: channel-toggle] Registered on Discord
  - toggle-channel
[Command: clear-user-channel-history] Registered on Discord
[Event: messageCreate] Message "Howdy" from kevinthedang in channel/thread 1188262786497785896.
[Util: openConfig] Created '1184705769749368892-config.json' in working directory
[Event: messageCreate] Message "**Error Occurred:**

**Reason:** *No Server Preferences is set up.

Creating default server preferences file...
Please try chatting again.*" from Ollama GU#1572 in channel/thread 1188262786497785896.

Are you able to provide what system you are running this on (Linux Distro, Windows, Mac)?

It should have created the config file like this

[Util: openConfig] Created '1184705769749368892-config.json' in working directory

Oh I see it!

Are you able to run the application using the latest of NodeJS/hydrogen (This is NodeJS 18)?

Edit: nevermind I ran it on NodeJS v21.2.0 and it works as shown above...

Are you able to show me your data/ files?

$ ls -alrt data/
total 4
-rwxrwxrwx 1 kevin kevin   83 Jul 14 20:26 kevinthedang-config.json
drwxrwxrwx 1 kevin kevin 4096 Aug  3 04:23 ..
-rwxrwxrwx 1 kevin kevin  673 Aug  3 04:26 1188262786497785896-kevinthedang.json
drwxrwxrwx 1 kevin kevin 4096 Aug  3 05:02 .
-rwxrwxrwx 1 kevin kevin  110 Aug  3 05:02 1184705769749368892-config.json

You can also try removing this directory and try chatting with the bot again 🤞🏼

kikoferrer commented 2 months ago

I am running Linux Mint 21.3 at the moment.

$ ls -alrt data/
ls: cannot access 'data/': No such file or directory

I ran the command in the discord-ollama/ directory. Is that correct?

kevinthedang commented 2 months ago

Yup! Okay, I was able to replicate the issue and it seems like its a bug with some directory creation. This is an issue and I will use this issue as reference thanks.

For now, a current workaround is to just create a data/ directory by running mkdir data. I was able to do this and it seems to not be mad at me.

Let me know if this works!

kikoferrer commented 2 months ago

Thanks that somehow got through the previous error. Then I have this one now.

[Event: messageCreate] Message "How about now?" from kikopogi22 in channel/thread 1269233205181943891.
[Util: openConfig] Created '1267432269111627787-config.json' in working directory
[Event: messageCreate] Message "**Error Occurred:**

**Reason:** *No Server Preferences is set up.

Creating default server preferences file...
Please try chatting again.*" from Testbot#2143 in channel/thread 1269233205181943891.
[Command: shutoff] Registered on Discord
  - are-you-sure
[Command: modify-capacity] Registered on Discord
  - context-capacity
[Command: channel-toggle] Registered on Discord
  - toggle-channel
[Command: clear-user-channel-history] Registered on Discord

Then I tried chatting again and got this instead.

[Event: messageCreate] Message "Are you good now?" from kikopogi22 in channel/thread 1269233205181943891.
[Util: openChannelInfo] Created 'data/1269233205181943891-kikopogi22.json' in working directory
[Util: openConfig] Created 'kikopogi22-config.json' in working directory
[Event: messageCreate] Message "**Error Occurred:**

**Reason:** *No User Preferences is set up.

Creating preferences file with `message-style` set as `false` for regular messages.
Please try chatting again.*" from Testbot#2143 in channel/thread 1269233205181943891.
kikoferrer commented 2 months ago

Oh finally it worked on the third try! Working as intended now. Thanks

kevinthedang commented 2 months ago

Issue

Solution

References

Error: ENOENT: no such file or directory, open 'data/1267432269111627787-config.json' at Object.openSync (node:fs:581:18) at Object.writeFileSync (node:fs:2345:35) at openConfig (file:///home/chupz22/discord-ollama/build/utils/handlers/configHandler.js:35:12) at file:///home/chupz22/discord-ollama/build/events/messageCreate.js:27:21 at getServerConfig (file:///home/chupz22/discord-ollama/build/utils/handlers/configHandler.js:58:9) at serverConfig (file:///home/chupz22/discord-ollama/build/events/messageCreate.js:23:13) at new Promise () at file:///home/chupz22/discord-ollama/build/events/messageCreate.js:22:36 at Client. (file:///home/chupz22/discord-ollama/build/utils/events.js:21:17) at Client.emit (node:events:519:28) { errno: -2, code: 'ENOENT', syscall: 'open', path: 'data/1267432269111627787-config.json' }

Node.js v21.2.0

* Implementation found in `src/utils/handlers/chatHistoryHandler.ts`
```ts
/**
 * Method to open the channel history
 * 
 * @param filename name of the json file for the channel by user
 * @param channel the text channel info
 * @param user the user's name
 * @param messages their messages
 */
export async function openChannelInfo(filename: string, channel: TextChannel, user: string, messages: UserMessage[] = []): Promise<void> {
    const isThread: boolean = await new Promise((resolve) => {
        getThread(`${channel.id}.json`, (threadInfo) => {
            if (threadInfo?.messages)
                resolve(true)
            else
                resolve(false)
        })
    })

    // this is a thread channel, do not duplicate files
    if (isThread) return

    const fullFileName = `data/${filename}-${user}.json`
    if (fs.existsSync(fullFileName)) {
        fs.readFile(fullFileName, 'utf8', (error, data) => {
            if (error)
                console.log(`[Error: openChannelInfo] Incorrect file format`)
            else {
                const object = JSON.parse(data)
                if (object['messages'].length === 0)
                    object['messages'] = messages as []
                else if (object['messages'].length !== 0 && messages.length !== 0)
                    object['messages'] = messages as []
                fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
            }
        })
    } else { // file doesn't exist, create it
        const object: Configuration = JSON.parse(`{ \"id\": \"${channel?.id}\", \"name\": \"${channel?.name}\", \"user\": \"${user}\", \"messages\": []}`)

        const directory = path.dirname(fullFileName)
        if (!fs.existsSync(directory))
            fs.mkdirSync(directory, { recursive: true })

        // only creating it, no need to add anything
        fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
        console.log(`[Util: openChannelInfo] Created '${fullFileName}' in working directory`)
    }
}

cc: @JT2M0L3Y

kevinthedang commented 2 months ago

@SmoothBrainApe Thank you for reporting this! Seems like a small fix :)

We'll try our best to make documentation clear.