Switch to waylaidwanderer

This commit is contained in:
bertybuttface 2023-02-06 14:08:45 +00:00
parent 205d87ffcf
commit e39f3fdeee
5 changed files with 2449 additions and 121 deletions

2509
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,7 @@
"@keyv/postgres": "^1.4.1",
"@keyv/redis": "^2.5.4",
"@keyv/sqlite": "^3.6.4",
"chatgpt": "^4.1.1",
"@waylaidwanderer/chatgpt-api": "^1.9.0",
"dotenv": "^16.0.3",
"hash.js": "^1.1.7",
"keyv": "^4.5.2",

View File

@ -1,4 +1,4 @@
import { ChatGPTAPI } from "chatgpt";
import ChatGPTClient from '@waylaidwanderer/chatgpt-api';
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: ChatGPTAPI) { }
constructor(private client: MatrixClient, private chatGPT: ChatGPTClient) { }
public async start() {
await this.prepareProfile(); // Populate the variables above (async)
@ -122,13 +122,14 @@ export default class CommandHandler {
}
const result = await sendChatGPTMessage(this.chatGPT, await bodyWithoutPrefix, storedConversation);
console.log(result)
await Promise.all([
this.client.setTyping(roomId, false, 500),
sendReply(this.client, roomId, this.getRootEventId(event), `${result.text}`, MATRIX_THREADS, MATRIX_RICH_TEXT)
sendReply(this.client, roomId, this.getRootEventId(event), `${result.response}`, MATRIX_THREADS, MATRIX_RICH_TEXT)
]);
const storedConfig = ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {})
const configString: string = JSON.stringify({conversationId: result.conversationId, messageId: result.id, config: storedConfig})
const configString: string = JSON.stringify({conversationId: result.conversationId, messageId: result.messageId, 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

@ -1,4 +1,4 @@
import { ChatGPTAPI } from 'chatgpt'
import ChatGPTClient from '@waylaidwanderer/chatgpt-api';
import Keyv from 'keyv'
import { KeyvFile } from 'keyv-file';
import {
@ -46,18 +46,27 @@ async function main() {
}
if (!MATRIX_THREADS && CHATGPT_CONTEXT !== "room") throw Error("You must set CHATGPT_CONTEXT to 'room' if you set MATRIX_THREADS to false")
const client: MatrixClient = new MatrixClient(MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, storage, cryptoStore);
const chatGPT: ChatGPTAPI = new ChatGPTAPI({
apiKey: OPENAI_API_KEY,
completionParams: {
model: CHATGPT_MODEL,
const clientOptions = { // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
modelOptions: {
model: CHATGPT_MODEL, // The model is set to text-chat-davinci-002-20221122 by default
},
messageStore: chatgptStore
})
// (Optional) Set custom instructions instead of "You are ChatGPT...".
// promptPrefix: 'You are Bob, a cowboy in Western times...',
// (Optional) Set a custom name for the user
// userLabel: 'User',
// (Optional) Set a custom name for ChatGPT
// chatGptLabel: 'ChatGPT',
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
};
const cacheOptions = { // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
store: chatgptStore,
};
const chatgpt = new ChatGPTClient(OPENAI_API_KEY, clientOptions, cacheOptions);
// Automatically join rooms the bot is invited to
if (MATRIX_AUTOJOIN) {
AutojoinRoomsMixin.setupOnClient(client);
}
if (MATRIX_AUTOJOIN) AutojoinRoomsMixin.setupOnClient(client);
client.on("room.failed_decryption", async (roomId, event, error) => {
// handle `m.room.encrypted` event that could not be decrypted
@ -74,7 +83,7 @@ async function main() {
});
// Prepare the command handler
const commands = new CommandHandler(client, chatGPT);
const commands = new CommandHandler(client, chatgpt);
await commands.start();
LogService.info("index", `Starting bot using ChatGPT model: ${CHATGPT_MODEL}`);

View File

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