From 8598adb280bec24ce2bbadd4ff0c590bca6f1817 Mon Sep 17 00:00:00 2001 From: gohumble <55599638+gohumble@users.noreply.github.com> Date: Sun, 11 Dec 2022 19:12:34 +0100 Subject: [PATCH] added chatGPT (#435) --- config.yaml.example | 1 + go.mod | 6 ++- go.sum | 29 ++++++---- internal/config.go | 7 +-- internal/gpt/gpt.go | 102 +++++++++++++++++++++++++++++++++++ internal/telegram/gpt.go | 73 +++++++++++++++++++++++++ internal/telegram/handler.go | 16 ++++++ translations/cs.toml | 3 +- translations/de.toml | 3 +- translations/en.toml | 3 +- translations/es.toml | 3 +- translations/fr.toml | 3 +- translations/id.toml | 3 +- translations/it.toml | 3 +- translations/nl.toml | 3 +- translations/pl.toml | 3 +- translations/pt-br.toml | 4 +- 17 files changed, 241 insertions(+), 24 deletions(-) create mode 100644 internal/gpt/gpt.go create mode 100644 internal/telegram/gpt.go diff --git a/config.yaml.example b/config.yaml.example index 7bfa5530..ac374653 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -26,6 +26,7 @@ database: shop_buntdb_path: "data/shop.db" groupsdb_path: "data/groups.db" generate: + open_ai_bearer_token: "token_here" dalle_key: "asd" dalle_price: 1000 worker: 2 \ No newline at end of file diff --git a/go.mod b/go.mod index 8a527427..75e3ba75 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/BurntSushi/toml v0.3.1 github.com/PuerkitoBio/goquery v1.8.0 + github.com/PullRequestInc/go-gpt3 v1.1.10 github.com/btcsuite/btcd/btcec/v2 v2.2.0 github.com/eko/gocache v1.2.0 github.com/fiatjaf/go-lnurl v1.11.3-0.20220819192234-5c5819dd0aa7 @@ -18,6 +19,7 @@ require ( github.com/orcaman/concurrent-map v1.0.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/r3labs/sse v0.0.0-20210224172625-26fe804710bc + github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.7.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/tidwall/buntdb v1.2.7 @@ -26,6 +28,7 @@ require ( golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f golang.org/x/text v0.3.7 golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba + gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220828121412-0dea11ecc6dd gorm.io/driver/sqlite v1.1.4 gorm.io/gorm v1.21.12 ) @@ -55,7 +58,6 @@ require ( github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/decred/dcrd/lru v1.0.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect - github.com/dillonstreator/dalle v0.0.0-20220826164958-b15c236ceadc // indirect github.com/go-errors/errors v1.0.1 // indirect github.com/go-redis/redis/v8 v8.8.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect @@ -84,11 +86,11 @@ require ( go.opentelemetry.io/otel/metric v0.20.0 // indirect go.opentelemetry.io/otel/trace v0.20.0 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect + golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect - gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220828121412-0dea11ecc6dd // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum index a6e1cc9f..597281a9 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0g github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/PullRequestInc/go-gpt3 v1.1.10 h1:Z2fYKq7oGrr4Cs0yDmuq25FYR4hgvCowIR9sENHmSZ0= +github.com/PullRequestInc/go-gpt3 v1.1.10/go.mod h1:F9yzAy070LhkqHS2154/IH0HVj5xq5g83gLTj7xzyfw= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= @@ -157,6 +159,7 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0 h1:J9B4L7e3oqhXOcm+2IuNApwzQec85lE+QaikUcCs+dk= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc= github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= @@ -219,8 +222,6 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUn github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/dillonstreator/dalle v0.0.0-20220826164958-b15c236ceadc h1:gI9DEfWBOylVhQSIAuvKbaTkqvtlDmOTKGcg7i3Y3uE= -github.com/dillonstreator/dalle v0.0.0-20220826164958-b15c236ceadc/go.mod h1:FyvK9n5UiK1wDTgGSkyMckbBAYVZiqaTH1LINKCgbIQ= github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM= github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q= github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo= @@ -251,8 +252,6 @@ github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGE github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fergusstrange/embedded-postgres v1.10.0 h1:YnwF6xAQYmKLAXXrrRx4rHDLih47YJwVPvg8jeKfdNg= github.com/fergusstrange/embedded-postgres v1.10.0/go.mod h1:a008U8/Rws5FtIOTGYDYa7beVWsT3qVKyqExqYYjL+c= -github.com/fiatjaf/go-lnurl v1.11.2 h1:XyZS6Uuu/2ZbhkZjOm3W+FaFvRzRRTFel3aMoO6eDUE= -github.com/fiatjaf/go-lnurl v1.11.2/go.mod h1:9PAgeVpZ5u6olV/dfkoyPSOJs3j4q0JIGLSbBbmMci8= github.com/fiatjaf/go-lnurl v1.11.3-0.20220819192234-5c5819dd0aa7 h1:0m0ph1FcZY7p89OpaXOUs8N44GhP7gE/98l+eWCNSkQ= github.com/fiatjaf/go-lnurl v1.11.3-0.20220819192234-5c5819dd0aa7/go.mod h1:KJfs14iAY3gCgt/3T6fxfBvPhU67OfIp7PSrBg/v/R8= github.com/fiatjaf/ln-decodepay v1.1.0 h1:HigjqNH+ApiO6gm7RV23jXNFuvwq+zgsWl4BJAfPWwE= @@ -265,6 +264,7 @@ github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVB github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -366,6 +366,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -497,6 +498,8 @@ github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/joefitzgerald/rainbow-reporter v0.1.0/go.mod h1:481CNgqmVHQZzdIbN52CupLJyoVwB10FQ/IQlF1pdL8= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ= github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= @@ -573,6 +576,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -659,6 +663,7 @@ github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KK github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/maxbrunsfeld/counterfeiter/v6 v6.2.3/go.mod h1:1ftk08SazyElaaNvmqAfZWGwJzshjCfBXDLoQtPAMNk= github.com/mholt/archiver/v3 v3.5.0 h1:nE8gZIrw66cu4osS/U7UW7YDuGMHssxKutU8IfWxwWE= github.com/mholt/archiver/v3 v3.5.0/go.mod h1:qqTTPUK/HZPFgFQ/TJ3BzvTpF/dPtFVJXdQbCmeMxwc= github.com/miekg/dns v0.0.0-20171125082028-79bfde677fa8/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= @@ -710,6 +715,7 @@ github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:v github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= @@ -720,6 +726,7 @@ github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5 github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ= github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= @@ -800,13 +807,17 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U= +github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= @@ -856,8 +867,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= @@ -1113,9 +1124,9 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde h1:ejfdSekXMDxDLbRrJMwUk6KnSLZ2McaUCVcIKM+N6jc= +golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20170830134202-bb24a47a89ea/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1137,6 +1148,7 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1253,6 +1265,7 @@ golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapK golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200301222351-066e0c02454c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= @@ -1394,8 +1407,6 @@ gopkg.in/httprequest.v1 v1.1.1/go.mod h1:/CkavNL+g3qLOrpFHVrEx4NKepeqR4XTZWNj4sG gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220326213923-f323bb71ac8e h1:x58PfjVq6dbYSzM4URM9QfKDKVru1CpngxCM5pGqwbk= -gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220326213923-f323bb71ac8e/go.mod h1:kIC/Dlp2OjWhVrOkZDN4iEoCaqoac5w1UjpKY6Ftf+U= gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220828121412-0dea11ecc6dd h1:LI1Q9RRmTKusLTpLGAHFUVT+wgzc0GHFKd3+34fARE8= gopkg.in/lightningtipbot/telebot.v3 v3.0.0-20220828121412-0dea11ecc6dd/go.mod h1:BefN0q4hcqXE1jXo4mTXZ9+29zl7IGsoSFXEQF455Oc= gopkg.in/macaroon-bakery.v2 v2.0.1/go.mod h1:B4/T17l+ZWGwxFSZQmlBwp25x+og7OkhETfr3S9MbIA= @@ -1422,8 +1433,8 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM= gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw= gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw= diff --git a/internal/config.go b/internal/config.go index 7cdc5df9..ed85b458 100644 --- a/internal/config.go +++ b/internal/config.go @@ -18,9 +18,10 @@ var Configuration = struct { }{} type GenerateConfiguration struct { - DalleKey string `yaml:"dalle_key"` - DallePrice int64 `yaml:"dalle_price"` - Worker int `yaml:"worker"` + OpenAiBearerToken string `yaml:"open_ai_bearer_token"` + DalleKey string `yaml:"dalle_key"` + DallePrice int64 `yaml:"dalle_price"` + Worker int `yaml:"worker"` } type SocksConfiguration struct { diff --git a/internal/gpt/gpt.go b/internal/gpt/gpt.go new file mode 100644 index 00000000..69f47e61 --- /dev/null +++ b/internal/gpt/gpt.go @@ -0,0 +1,102 @@ +package gpt + +import ( + "bufio" + "bytes" + "encoding/json" + "fmt" + "github.com/LightningTipBot/LightningTipBot/internal" + "github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept" + "net/http" +) + +type Response struct { + Message struct { + ID string `json:"id"` + Role string `json:"role"` + User interface{} `json:"user"` + CreateTime interface{} `json:"create_time"` + UpdateTime interface{} `json:"update_time"` + Content struct { + ContentType string `json:"content_type"` + Parts []string `json:"parts"` + } `json:"content"` + EndTurn interface{} `json:"end_turn"` + Weight float64 `json:"weight"` + Metadata struct { + } `json:"metadata"` + Recipient string `json:"recipient"` + } `json:"message"` + ConversationID string `json:"conversation_id"` + Error interface{} `json:"error"` +} +type Request struct { + Action string `json:"action"` + ConversationId string `json:"conversation_id,omitempty"` + Messages []Messages `json:"messages"` + ParentMessageID string `json:"parent_message_id"` + Model string `json:"model"` +} +type Content struct { + ContentType string `json:"content_type"` + Parts []string `json:"parts"` +} +type Messages struct { + ID string `json:"id"` + Role string `json:"role"` + Content Content `json:"content"` +} + +var dataPrefix = []byte("data: ") +var doneSequence = []byte("[DONE]") + +func GetRawCompletion(ctx intercept.Context, rr Request, cb func(s string)) (*Response, error) { + rawClient := http.Client{} + r, err := json.Marshal(rr) + if err != nil { + return nil, err + } + req, err := http.NewRequestWithContext(ctx.Context, "POST", "https://chat.openai.com/backend-api/conversation", bytes.NewBuffer(r)) + if err != nil { + return nil, err + } + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", internal.Configuration.Generate.OpenAiBearerToken)) + req.Header.Set("accept", "text/event-stream") + req.Header.Set("authority", "chat.openai.com") + req.Header.Set("accept-language", "de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7") + req.Header.Set("Content-type", "application/json") + req.Header.Set("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36") + + resp, err := rawClient.Do(req) + if err != nil { + return nil, err + } + reader := bufio.NewReader(resp.Body) + defer resp.Body.Close() + output := new(Response) + for { + line, err := reader.ReadBytes('\n') + if err != nil { + return nil, err + } + // make sure there isn't any extra whitespace before or after + line = bytes.TrimSpace(line) + // the completion API only returns data events + if !bytes.HasPrefix(line, dataPrefix) { + continue + } + line = bytes.TrimPrefix(line, dataPrefix) + + // the stream is completed when terminated by [DONE] + if bytes.HasPrefix(line, doneSequence) { + break + } + if err := json.Unmarshal(line, output); err != nil { + return nil, fmt.Errorf("invalid json stream data: %v", err) + } + if len(output.Message.Content.Parts) > 0 { + cb(output.Message.Content.Parts[len(output.Message.Content.Parts)-1]) + } + } + return output, nil +} diff --git a/internal/telegram/gpt.go b/internal/telegram/gpt.go new file mode 100644 index 00000000..bd077344 --- /dev/null +++ b/internal/telegram/gpt.go @@ -0,0 +1,73 @@ +package telegram + +import ( + "fmt" + "github.com/LightningTipBot/LightningTipBot/internal/gpt" + "github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept" + uuid "github.com/satori/go.uuid" + log "github.com/sirupsen/logrus" + "gopkg.in/lightningtipbot/telebot.v3" +) + +func (bot *TipBot) gptHandler(ctx intercept.Context) (intercept.Context, error) { + question := GetMemoFromCommand(ctx.Text(), 1) + req := gpt.Request{ + Action: "next", + Model: "text-davinci-002-render", + Messages: []gpt.Messages{{ + ID: uuid.NewV4().String(), + Role: "user", + Content: gpt.Content{ + ContentType: "text", + Parts: []string{question}, + }, + }, + }, + } + + conversationIdCacheKey := fmt.Sprintf("conversation_%d", ctx.Chat().ID) + parentIdCacheKey := fmt.Sprintf("conversation_parent_%d", ctx.Chat().ID) + conversationId, _ := bot.Cache.Get(conversationIdCacheKey) + + if parentId, _ := bot.Cache.Get(parentIdCacheKey); parentId != nil { + req.ParentMessageID = parentId.(string) + } + if conversationId != nil { + req.ConversationId = conversationId.(string) + } + cbc := 0 + var msg *telebot.Message + completion, err := gpt.GetRawCompletion(ctx, req, func(s string) { + cbc++ + if ctx.Chat().Type == telebot.ChatPrivate { + if cbc == 20 { + msg = bot.trySendMessageEditable(ctx.Sender(), s) + return + } + } else { + if cbc == 20 { + msg = bot.tryReplyMessage(ctx.Message(), s) + return + } + } + if cbc%20 == 0 { + bot.tryEditMessage(msg, s) + } + }) + if err != nil { + bot.tryEditMessage(ctx.Message(), fmt.Sprintf(Translate(ctx, "errorReasonMessage"), "Could not create completion.")) + return ctx, err + } + answer := completion.Message.Content.Parts[len(completion.Message.Content.Parts)-1] + bot.tryEditMessage(msg, answer) + err = bot.Cache.Set(conversationIdCacheKey, completion.ConversationID, nil) + if err != nil { + log.Errorf("[/gpt] error setting conversation id %s: %v", conversationIdCacheKey, err) + } + err = bot.Cache.Set(parentIdCacheKey, completion.Message.ID, nil) + if err != nil { + log.Errorf("[/gpt] error setting parent message id %s: %v", parentIdCacheKey, err) + } + log.Infof("[/gpt] \"%s\" => \"%s\"", question, answer) + return ctx, nil +} diff --git a/internal/telegram/handler.go b/internal/telegram/handler.go index 6e5c8d77..44cc2233 100644 --- a/internal/telegram/handler.go +++ b/internal/telegram/handler.go @@ -227,6 +227,22 @@ func (bot TipBot) getHandler() []InterceptionWrapper { bot.unlockInterceptor, }}, }, + { + Endpoints: []interface{}{"/gpt", "/chat"}, + Handler: bot.gptHandler, + Interceptor: &Interceptor{ + + Before: []intercept.Func{ + bot.localizerInterceptor, + bot.logMessageInterceptor, + bot.requireUserInterceptor, + bot.lockInterceptor, + }, + OnDefer: []intercept.Func{ + bot.unlockInterceptor, + }, + }, + }, { Endpoints: []interface{}{"/balance", &btnBalanceMainMenu}, Handler: bot.balanceHandler, diff --git a/translations/cs.toml b/translations/cs.toml index 2cfdfc9f..533b05de 100644 --- a/translations/cs.toml +++ b/translations/cs.toml @@ -128,7 +128,8 @@ advancedMessage = """%s */tipjar* 🍯 Vytvořit tipjar: `/tipjar <částka_na_uživatele>` */group* 🎟 Vytvořit vstupenky do skupiny: `/group add []` */shop* 🛍 Prohlížet obchody: `/shop` nebo `/shop ` -*/generate* 🎆 Vytvořit obrázky od DALLE 2: `/generate `""" +*/generate* 🎆 Vytvořit obrázky od DALLE 2: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC diff --git a/translations/de.toml b/translations/de.toml index 2c71b6f7..75cfea91 100644 --- a/translations/de.toml +++ b/translations/de.toml @@ -125,7 +125,8 @@ advancedMessage = """%s */tipjar* 🍯 Erzeuge eine Spendendose: `/tipjar ` */group* 🎟 Tickets für Gruppenchats: `/group add []` */shop* 🛍 Durchsuche Shops: `/shop` oder `/shop ` -*/generate* 🎆 Generiere Bilder mit DALLE-2: `/generate `""" +*/generate* 🎆 Generiere Bilder mit DALLE-2: `/generate ` +*/chat* 💬 Chatte mit GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Gebe Betrag zwuschen %d und %d sat ein.""" diff --git a/translations/en.toml b/translations/en.toml index 7e56f08a..3b31e07e 100644 --- a/translations/en.toml +++ b/translations/en.toml @@ -128,7 +128,8 @@ advancedMessage = """%s */tipjar* 🍯 Create a tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC diff --git a/translations/es.toml b/translations/es.toml index 930e183b..c3ff9b54 100644 --- a/translations/es.toml +++ b/translations/es.toml @@ -124,7 +124,8 @@ advancedMessage = """%s */tipjar* 🍯 Crear un tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Introduce un monto entre %d y %d sat.""" diff --git a/translations/fr.toml b/translations/fr.toml index 12532de0..d97c4ba6 100644 --- a/translations/fr.toml +++ b/translations/fr.toml @@ -124,7 +124,8 @@ advancedMessage = """%s */tipjar* 🍯 Créer un tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Choisissez un montant entre %d et %d sat.""" diff --git a/translations/id.toml b/translations/id.toml index 07be1375..071e5bdd 100644 --- a/translations/id.toml +++ b/translations/id.toml @@ -124,7 +124,8 @@ advancedMessage = """%s */tipjar* 🍯 Create a tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Masukkan jumlah diantara %d dan %d sat.""" diff --git a/translations/it.toml b/translations/it.toml index 839ff5f1..a939c273 100644 --- a/translations/it.toml +++ b/translations/it.toml @@ -124,7 +124,8 @@ advancedMessage = """%s */tipjar* 🍯 Crea un tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Imposta un ammontare tra %d e %d sat.""" diff --git a/translations/nl.toml b/translations/nl.toml index 6e883c15..26327a8e 100644 --- a/translations/nl.toml +++ b/translations/nl.toml @@ -124,7 +124,8 @@ advancedMessage = """%s */tipjar* 🍯 Maak een tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` */shop* 🛍 Browse shops: `/shop` or `/shop ` -*/generate* 🎆 Generate DALLE-2 images: `/generate `""" +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Voer een bedrag in tussen %d en %d sat.""" diff --git a/translations/pl.toml b/translations/pl.toml index 4508fafb..7dcc2862 100644 --- a/translations/pl.toml +++ b/translations/pl.toml @@ -129,7 +129,8 @@ advancedMessage = """%s */tipjar* 🍯 Stwórz słoik na napiwki (_tipjar_): `/tipjar ` */group* 🎟 Stwórz bilety na wejście do grupy: `/group add []` */shop* 🛍 Przeglądaj sklepy: `/shop` lub `/shop ` -*/generate* 🎆 Generuj obrazy przy użyciu DALLE-2: `/generate `""" +*/generate* 🎆 Generuj obrazy przy użyciu DALLE-2: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC diff --git a/translations/pt-br.toml b/translations/pt-br.toml index 8f1250d1..448941f8 100644 --- a/translations/pt-br.toml +++ b/translations/pt-br.toml @@ -123,7 +123,9 @@ advancedMessage = """%s */faucet* 🚰 Criar uma torneira: `/faucet ` */tipjar* 🍯 Criar uma tipjar: `/tipjar ` */group* 🎟 Create group tickets: `/group add []` -*/shop* 🛍 Browse shops: `/shop` or `/shop `""" +*/shop* 🛍 Browse shops: `/shop` or `/shop ` +*/generate* 🎆 Generate DALLE-2 images: `/generate ` +*/chat* 💬 Chat with GPT: `/chat `""" # GENERIC enterAmountRangeMessage = """💯 Insira uma quantia entre %d e %d sat."""