Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzz1y committed Aug 9, 2023
0 parents commit 2607c06
Show file tree
Hide file tree
Showing 18 changed files with 907 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Docker Publish

on:
push:
branches:
- master
tags:
- '*'

jobs:
docker:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get Git Tag
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
run: |
echo "GIT_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Build and push Docker image with tag
if: env.GIT_TAG != ''
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ env.GIT_TAG }}
build-args: |
BUILD_TAG=${{ env.GIT_TAG }}
- name: Build and push Docker image with latest
if: github.ref == 'refs/heads/master'
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.20 as build

ENV CGO_ENABLED 1
RUN apt-get update && apt-get install -y libolm-dev && \
rm -rf /var/lib/apt/lists/*

COPY . /app
RUN cd /app && \
go build -ldflags="-s -w" -trimpath -o /matrix-gpt ./cmd/matrix-gpt

FROM ubuntu:22.04
RUN apt-get update && \
apt-get install -y libolm3 ca-certificates tzdata && \
rm -rf /var/lib/apt/lists/*

COPY --from=build /matrix-gpt /matrix-gpt
USER 1337
CMD ["/matrix-gpt"]
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Matrix GPT

Matrix GPT is a Matrix chatbot that uses OpenAI for real-time chatting.
## Installation

### Docker

Run the Docker container:

```bash
docker run -d --name matrix-gpt \
-p 8080:8080 \
-e MATRIX_PASSWORD="matrix password" \
-e MATRIX_ID="matrix id" \
-e MATRIX_URL="matrix server url" \
-e OPENAI_TOKEN="openai token" \
-e SQLITE_PATH="persistent path for sqlite database"
-e USER_IDS="allowed user ids"
ghcr.io/mazzz1y/matrix-gpt:latest

```
## Configuration

You can configure GPT Matrix using the following environment variables:

- `SERVER_URL`: The URL to the Matrix homeserver.
- `USER_ID`: Your Matrix user ID for the bot.
- `PASSWORD`: The password for your Matrix bot's account.
- `SQLITE_PATH`: Path to SQLite database for end-to-end encryption.
- `HISTORY_EXPIRE`: Duration after which chat history expires.
- `GPT_MODEL`: The OpenAI GPT model being used.
- `GPT_HISTORY_LIMIT`: Limit for number of chat messages retained in history.
- `GPT_TIMEOUT`: Duration for OpenAI API timeout.
- `GPT_MAX_ATTEMPTS`: Maximum number of attempts for GPT API retries.
- `GPT_USER_IDS`: List of authorized user IDs for the bot.

Alternatively, you can set these options using command-line flags. Run `./matrix-gpt --help` for more
information.

## Usage

1. Begin by adding the bot to your contact list.
Once added, you can start interacting with it. Simply send your questions or commands, and the bot will respond.
2. If at any point you wish to reset the context of the conversation, use the `!reset` command.
Send `!reset` as a message to the bot, and it will clear the existing context, allowing you to start a fresh
conversation.

## TODO

* Add image generation using DALL·E model
37 changes: 37 additions & 0 deletions cmd/gpt-matrix/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"github.com/mazzz1y/matrix-gpt/internal/bot"
"github.com/mazzz1y/matrix-gpt/internal/gpt"
"github.com/urfave/cli/v2"
)

func run(c *cli.Context) error {
mPassword := c.String("matrix-password")
mUserId := c.String("matrix-id")
mUrl := c.String("matrix-url")
mRoom := c.String("matrix-room")
sqlitePath := c.String("sqlite-path")

gptModel := c.String("gpt-model")
gptTimeout := c.Int("gpt-timeout")
openaiToken := c.String("openai-token")
maxAttempts := c.Int("max-attempts")

historyExpire := c.Int("history-expire")
historyLimit := c.Int("history-limit")
userIDs := c.StringSlice("user-ids")

logLevel := c.String("log-level")
logType := c.String("log-type")

setLogLevel(logLevel, logType)

g := gpt.New(openaiToken, gptModel, historyLimit, gptTimeout, maxAttempts, userIDs)
m, err := bot.NewBot(mUrl, mUserId, mPassword, sqlitePath, mRoom, historyExpire, g)
if err != nil {
return err
}

return m.StartHandler()
}
41 changes: 41 additions & 0 deletions cmd/gpt-matrix/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"os"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func setLogLevel(logLevel string, logType string) {
switch logType {
case "json":
case "pretty":
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
default:
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Msgf("invalid log type: %s. using 'pretty' as default", logType)
}

switch logLevel {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
case "no":
zerolog.SetGlobalLevel(zerolog.NoLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Info().Msgf("invalid log level: %s. using 'info' as default", logLevel)
}
}
103 changes: 103 additions & 0 deletions cmd/gpt-matrix/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"fmt"
"github.com/sashabaranov/go-openai"
"github.com/urfave/cli/v2"
"os"
)

var version = "git"

func main() {
app := &cli.App{
Name: "matrix-gpt",
Version: version,
Usage: "GPT Matrix Bot",
Action: run,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "matrix-password",
Usage: "Matrix password",
EnvVars: []string{"MATRIX_PASSWORD"},
Required: true,
},
&cli.StringFlag{
Name: "matrix-id",
Usage: "Matrix user ID",
EnvVars: []string{"MATRIX_ID"},
Required: true,
},
&cli.StringFlag{
Name: "matrix-url",
Usage: "Matrix server URL",
EnvVars: []string{"MATRIX_URL"},
Required: true,
},
&cli.StringFlag{
Name: "openai-token",
Usage: "OpenAI API token",
EnvVars: []string{"OPENAI_TOKEN"},
Required: true,
},
&cli.StringFlag{
Name: "sqlite-path",
Usage: "Path to SQLite database",
EnvVars: []string{"SQLITE_PATH"},
Required: true,
},
&cli.IntFlag{
Name: "history-limit",
Usage: "Maximum number of history entries",
EnvVars: []string{"HISTORY_LIMIT"},
Value: 5,
},
&cli.IntFlag{
Name: "history-expire",
Usage: "Time after which history entries expire (in hours)",
EnvVars: []string{"HISTORY_EXPIRE"},
Value: 3,
},
&cli.StringFlag{
Name: "gpt-model",
Usage: "GPT model name/version",
EnvVars: []string{"GPT_MODEL"},
Value: openai.GPT3Dot5Turbo,
},
&cli.IntFlag{
Name: "gpt-timeout",
Usage: "Time to wait for a GPT response (in seconds)",
EnvVars: []string{"GPT_TIMEOUT"},
Value: 45,
},
&cli.IntFlag{
Name: "max-attempts",
Usage: "Maximum number of retry attempts for GPT",
EnvVars: []string{"MAX_ATTEMPTS"},
Value: 3,
},
&cli.StringSliceFlag{
Name: "user-ids",
Usage: "List of allowed Matrix user IDs",
EnvVars: []string{"USER_IDS"},
Required: true,
},
&cli.StringFlag{
Name: "log-level",
Value: "info",
Usage: "Logging level (e.g. debug, info, warn, error, fatal, panic, no)",
EnvVars: []string{"LOG_LEVEL"},
},
&cli.StringFlag{
Name: "log-type",
Value: "pretty",
Usage: "Logging format/type (e.g. pretty, json)",
EnvVars: []string{"LOG_TYPE"},
},
},
}

if err := app.Run(os.Args); err != nil {
fmt.Printf("\n" + err.Error())
}
}
29 changes: 29 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/mazzz1y/matrix-gpt

go 1.20

require (
github.com/mattn/go-sqlite3 v1.14.17
github.com/rs/zerolog v1.30.0
github.com/sashabaranov/go-openai v1.14.1
github.com/urfave/cli/v2 v2.25.7
maunium.net/go/mautrix v0.15.4
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yuin/goldmark v1.5.4 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
maunium.net/go/maulogger/v2 v2.4.1 // indirect
)
52 changes: 52 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sashabaranov/go-openai v1.14.1 h1:jqfkdj8XHnBF84oi2aNtT8Ktp3EJ0MfuVjvcMkfI0LA=
github.com/sashabaranov/go-openai v1.14.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=
maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho=
maunium.net/go/mautrix v0.15.4 h1:Ug3n2Mo+9Yb94AjZTWJQSNHmShaksEzZi85EPl3S3P0=
maunium.net/go/mautrix v0.15.4/go.mod h1:dBaDmsnOOBM4a+gKcgefXH73pHGXm+MCJzCs1dXFgrw=
Loading

0 comments on commit 2607c06

Please sign in to comment.