Change bot to use official API keys

This commit is contained in:
Max Kammler 2023-02-02 13:44:14 +01:00
parent 36636f460a
commit e45ac29a8e
8 changed files with 18 additions and 6827 deletions

View File

@ -1,8 +1,6 @@
# ChatGPT Settings (required)
OPENAI_EMAIL=
OPENAI_PASSWORD=
# What type of Login it is, possibility's are google, openai, microsoft
OPENAI_LOGIN_TYPE=google
# Set the API Key from OpenAI
OPENAI_API_KEY=
# Set the next line to true if you are using a ChatGPT pro account.
OPENAI_PRO=false

View File

@ -2,21 +2,6 @@ FROM satantime/puppeteer-node:19-slim
WORKDIR /usr/src/app
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
ENV CHROME_PATH=/usr/bin/chromium
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update -qq \
&& apt install -qq -y --no-install-recommends \
chromium \
dumb-init \
# To run Headful mode we just need to install Xvfb and Puppeteer related dependencies.
xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic \
xvfb xauth\
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /src/*.deb
COPY package*.json ./
RUN yarn install --frozen-lockfile --production && yarn cache clean
@ -27,5 +12,4 @@ RUN yarn build
VOLUME /storage
ENV DATA_PATH="/storage"
# We run a fake display and run our script using Xvfb
CMD xvfb-run --server-args="-screen 0 1024x768x16" yarn start
CMD yarn start

6779
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,7 +15,7 @@
"typecheck": "npx tsc"
},
"dependencies": {
"chatgpt": "^3.5.1",
"chatgpt": "^4.0.4",
"dotenv": "^16.0.3",
"markdown-it": "^13.0.1",
"matrix-bot-sdk": "^0.6.3",

View File

@ -23,9 +23,7 @@ export const {
MATRIX_DEFAULT_PREFIX,
MATRIX_DEFAULT_PREFIX_REPLY,
/** ChatGPT Settings */
OPENAI_EMAIL,
OPENAI_PASSWORD,
OPENAI_LOGIN_TYPE,
OPENAI_API_KEY,
OPENAI_PRO,
CHATGPT_CONTEXT,
CHATGPT_TIMEOUT
@ -49,9 +47,7 @@ export const {
MATRIX_DEFAULT_PREFIX: { schema: z.string().default(""), description: "Set to a string if you want the bot to respond only when messages start with this prefix. Trailing space matters. Empty for no prefix." },
MATRIX_DEFAULT_PREFIX_REPLY: { schema: z.boolean().default(false), description: "Set to false if you want the bot to answer to all messages in a thread/conversation" },
/** ChatGPT Settings */
OPENAI_EMAIL: { schema: z.string().min(3), description: "Set full username of OpenAI's account" },
OPENAI_PASSWORD: { schema: z.string().min(1), description: "Set password of OpenAI's account" },
OPENAI_LOGIN_TYPE: { schema: z.enum(["google", "openai", "microsoft"]).default("google"), description: "Set authentication provider to 'google', 'openai' or 'microsoft'" },
OPENAI_API_KEY: { schema: z.string().default(""), description: "Set to the API key from https://platform.openai.com/account/api-keys"},
OPENAI_PRO: { schema: z.boolean().default(false), description: "Set to true if you have a paid ChatGPT subscription." },
CHATGPT_TIMEOUT: { schema: z.number().default(2 * 60 * 1000), description: "Set number of milliseconds to wait for ChatGPT responses" },
CHATGPT_CONTEXT: { schema: z.enum(["thread", "room", "both"]).default("thread"), description: "Set the ChatGPT conversation context to 'thread', 'room' or 'both'" }

View File

@ -1,4 +1,4 @@
import { ChatGPTAPIBrowser } from "chatgpt";
import { ChatGPTAPI } from "chatgpt";
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk";
import { CHATGPT_CONTEXT, CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST, MATRIX_RICH_TEXT, MATRIX_PREFIX_DM, MATRIX_THREADS } from "./env.js";
import { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js";
@ -11,7 +11,7 @@ export default class CommandHandler {
private userId: string;
private localpart: string;
constructor(private client: MatrixClient, private chatGPT: ChatGPTAPIBrowser) { }
constructor(private client: MatrixClient, private chatGPT: ChatGPTAPI) { }
public async start() {
await this.prepareProfile(); // Populate the variables above (async)
@ -124,11 +124,11 @@ export default class CommandHandler {
const result = await sendChatGPTMessage(this.chatGPT, await bodyWithoutPrefix, storedConversation);
await Promise.all([
this.client.setTyping(roomId, false, 500),
sendReply(this.client, roomId, this.getRootEventId(event), `${result.response}`, MATRIX_THREADS, MATRIX_RICH_TEXT)
sendReply(this.client, roomId, this.getRootEventId(event), `${result.text}`, MATRIX_THREADS, MATRIX_RICH_TEXT)
]);
const storedConfig = ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {})
const configString: string = JSON.stringify({conversationId: result.conversationId, messageId: result.messageId, config: storedConfig})
const configString: string = JSON.stringify({conversationId: result.conversationId, messageId: result.id, config: storedConfig})
await this.client.storageProvider.storeValue('gpt-' + storageKey, configString);
if ((storageKey === roomId) && (CHATGPT_CONTEXT === "both")) await this.client.storageProvider.storeValue('gpt-' + event.event_id, configString);
} catch (err) {

View File

@ -7,10 +7,10 @@ import {
} from "matrix-bot-sdk";
import * as path from "path";
import { DATA_PATH, OPENAI_EMAIL, OPENAI_PASSWORD, OPENAI_LOGIN_TYPE, OPENAI_PRO, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION, MATRIX_THREADS, CHATGPT_CONTEXT } from './env.js'
import { DATA_PATH, OPENAI_API_KEY, OPENAI_PRO, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION, MATRIX_THREADS, CHATGPT_CONTEXT } from './env.js'
import { parseMatrixUsernamePretty } from './utils.js';
import CommandHandler from "./handlers.js"
import { ChatGPTAPIBrowser } from 'chatgpt'
import { ChatGPTAPI } from 'chatgpt'
LogService.setLogger(new RichConsoleLogger());
@ -42,18 +42,10 @@ async function main() {
const client: MatrixClient = new MatrixClient(MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, storage, cryptoStore);
// use puppeteer to bypass cloudflare (headful because of captchas)
const chatGPT: ChatGPTAPIBrowser = new ChatGPTAPIBrowser({
email: OPENAI_EMAIL,
password: OPENAI_PASSWORD,
isGoogleLogin: (OPENAI_LOGIN_TYPE == "google"),
isMicrosoftLogin: (OPENAI_LOGIN_TYPE == "microsoft"),
isProAccount: OPENAI_PRO
const chatGPT: ChatGPTAPI = new ChatGPTAPI({
apiKey: OPENAI_API_KEY
})
chatGPT.initSession().then(() => {
LogService.info('ChatGPT session initialized');
});
// // call `api.refreshSession()` every hour to refresh the session
// setInterval(() => {
// chatGPT.refreshSession().then(() => {

View File

@ -1,4 +1,4 @@
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
import { ChatGPTAPI, ChatMessage } from "chatgpt";
import Markdown from 'markdown-it';
import { MatrixClient } from "matrix-bot-sdk";
import { MessageEvent, StoredConversation } from "./interfaces.js";
@ -76,8 +76,8 @@ export async function sendReply(client: MatrixClient, roomId: string, rootEventI
await client.sendEvent(roomId, "m.room.message", finalContent);
}
export async function sendChatGPTMessage(chatGPT: ChatGPTAPIBrowser, question: string, storedConversation: StoredConversation) {
let result: ChatResponse
export async function sendChatGPTMessage(chatGPT: ChatGPTAPI, question: string, storedConversation: StoredConversation) {
let result: ChatMessage
if (storedConversation !== undefined) {
result = await chatGPT.sendMessage(question, {
timeoutMs: CHATGPT_TIMEOUT,