Compare commits

...

10 commits

Author SHA1 Message Date
Nikhil Nawgiri 9d5d30e743 Finalize v0.0.1: Make the bot relay the openai response to the matrix
user
2024-08-25 03:44:14 +02:00
Nikhil Nawgiri f2efdebc90 Refactor 2024-08-25 03:40:54 +02:00
Nikhil Nawgiri 644ce2096c Carryover and adapt openai helper 2024-08-25 03:40:21 +02:00
Nikhil Nawgiri 276896b6e1 Refactor 2024-08-25 02:00:04 +02:00
Nikhil Nawgiri 393ffd44c5 Update ts-node settings 2024-08-25 01:59:14 +02:00
Nikhil Nawgiri 4903a6cdf7 Update config.js.example 2024-08-25 01:58:35 +02:00
Nikhil Nawgiri 6dfc40a6ad Refactor 2024-08-24 22:59:14 +02:00
Nikhil Nawgiri 759f1e3998 Update deps and package.json script 2024-08-24 22:59:00 +02:00
Nikhil Nawgiri b4541b538f Update deps 2024-08-24 22:52:03 +02:00
Nikhil Nawgiri 4664c36a2a Update config setup 2024-08-24 22:51:54 +02:00
8 changed files with 211 additions and 28 deletions

2
.gitignore vendored
View file

@ -1,6 +1,6 @@
crypto
bot.json
config.json
config.ts
main.js
# Logs

View file

@ -1,6 +0,0 @@
{
"homeserverUrl": "",
"accessToken": "",
"storageLocation": "bot.json",
"cryptoFolderLocation": "crypto"
}

9
config.ts.example Normal file
View file

@ -0,0 +1,9 @@
module.exports = {
config: {
"homeserverUrl": "",
"accessToken": "",
"storageLocation": "bot.json",
"cryptoFolderLocation": "crypto"
"openaiApiKey": "",
}
}

View file

@ -5,11 +5,14 @@
"main": "main.ts",
"author": "Nikhil Nawgiri",
"license": "MIT",
"type": "module",
"scripts": {
"start": "node main.js",
"compile": "tsc main.ts --resolveJsonModule"
"start": "ts-node src/main.ts"
},
"dependencies": {
"matrix-bot-sdk": "^0.7.1"
"@types/node": "^22.5.0",
"matrix-bot-sdk": "^0.7.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
}

View file

@ -1,21 +1,21 @@
import * as config from "./config.json";
import {
MatrixClient,
SimpleFsStorageProvider,
RustSdkCryptoStorageProvider,
AutojoinRoomsMixin
} from "matrix-bot-sdk";
const sdk = require("matrix-bot-sdk");
import { config } from "../config.ts";
import { callOpenAiAPI } from "./openai.js";
const {
homeserverUrl,
accessToken,
storageLocation,
cryptoFolderLocation,
openaiApiKey,
} = config;
const {
MatrixClient,
SimpleFsStorageProvider,
RustSdkCryptoStorageProvider,
AutojoinRoomsMixin
} = sdk;
// In order to make sure the bot doesn't lose its state between restarts, we'll give it a place to cache
// any information it needs to. You can implement your own storage provider if you like, but a JSON file
// will work fine for this example.
@ -43,19 +43,23 @@ interface Event {
// This is the command handler we registered a few lines up
async function handleCommand(roomId: string, event: Event) {
console.log("event:", event);
// console.log("event:", event);
// Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us)
if (event.content?.msgtype !== 'm.text') return;
if (event.sender === await client.getUserId()) return;
// Check to ensure that the `!hello` command is being run
const body = event.content.body;
if (!body?.startsWith("!hello")) return;
// if (!body?.startsWith("!hello")) return;
// Now that we've passed all the checks, we can actually act upon the command
await client.setTyping(roomId, true);
setTimeout(async () => {
await client.setTyping(roomId, false);
await client.replyNotice(roomId, event, "Hello world!");
}, 1500);
const responseFromAi = await callOpenAiAPI({
prompt: body,
bearer: openaiApiKey,
});
await client.setTyping(roomId, false);
await client.replyNotice(roomId, event, responseFromAi);
}

50
src/openai.ts Normal file
View file

@ -0,0 +1,50 @@
const API_URL = 'https://api.openai.com/v1/chat/completions';
interface AiPersonalities {
drunk: string,
hindiBasicsTeacher: string,
spokenHindiTeacher: string,
greekBasicsTeacher: string,
}
const aiPersonalities: AiPersonalities = {
drunk: "I want you to act as a drunk person. You will only answer like a very drunk person texting and nothing else. Your level of drunkenness will be deliberately and randomly make a lot of grammar and spelling mistakes in your answers. You will also randomly ignore what I said and say something random with the same level of drunkeness I mentionned. Do not write explanations on replies.",
/* ~~~~~~~~~ */
hindiBasicsTeacher: "I want you to act as a Hindi teacher and improver for beginners. I will ask you questions in English about Hindi and you will reply to me in both English and Hindi to teach me about the basics. I want you to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.",
spokenHindiTeacher: "I want you to act as a spoken Hindi teacher and improver. I will speak to you in Hindi and you will reply to me in Hindi to practice my spoken Hindi. I want you to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.",
/* ~~~~~~~~~ */
greekBasicsTeacher: "I want you to act as a Greek teacher and improver for beginners. I will ask you questions in English or in German about the Greek language and you will reply to me in either English or German (depending on how I asked) and Greek to teach me about the basics. I want you to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. Now let's start practicing. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.",
};
export const callOpenAiAPI = async ({ prompt, bearer, aiPersonality = "" }: { prompt: string, bearer: string, aiPersonality?: string }) => {
const messages = [];
// if (aiPersonality.length && Object.keys(aiPersonalities).includes(aiPersonality)) {
// messages.push({
// role: "system",
// content: aiPersonalities[aiPersonality],
// });
// }
messages.push({ role: "user", content: prompt });
const data = {
model: "gpt-3.5-turbo",
messages,
temperature: 0.7,
};
const init = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${bearer}`,
},
body: JSON.stringify(data),
};
console.log("init:", init);
const response = await fetch(API_URL, init);
const body = await response.json().then(data => data.choices[0].message.content);
return body;
}

View file

@ -1,4 +1,16 @@
{
"module": "esnext",
"resolveJsonModule": true
"ts-node": {
"compilerOptions": {
"module": "es2015"
}
},
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"experimentalDecorators": true,
"noImplicitAny": true,
"allowImportingTsExtensions": true
},
"exclude": ["node_modules"]
}

113
yarn.lock
View file

@ -2,6 +2,31 @@
# yarn lockfile v1
"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1"
integrity sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==
dependencies:
"@jridgewell/trace-mapping" "0.3.9"
"@jridgewell/resolve-uri@^3.0.3":
version "3.1.2"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz#7a0ee601f60f99a20c7c7c5ff0c80388c1189bd6"
integrity sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==
"@jridgewell/sourcemap-codec@^1.4.10":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.0.tgz#3188bcb273a414b0d215fd22a58540b989b9409a"
integrity sha512-gv3ZRaISU3fjPAgNsriBRqGWQL6quFx04YMPW/zD8XMLsU32mhCCbfbO6KZFLjvYpCZ8zyDEgqsgf+PwPaM7GQ==
"@jridgewell/trace-mapping@0.3.9":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9"
integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==
dependencies:
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"
"@matrix-org/matrix-sdk-crypto-nodejs@0.1.0-beta.6":
version "0.1.0-beta.6"
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-nodejs/-/matrix-sdk-crypto-nodejs-0.1.0-beta.6.tgz#0ecae51103ee3c107af0d6d0738f33eb7cc9857e"
@ -18,6 +43,26 @@
domhandler "^5.0.3"
selderee "^0.11.0"
"@tsconfig/node10@^1.0.7":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.11.tgz#6ee46400685f130e278128c7b38b7e031ff5b2f2"
integrity sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==
"@tsconfig/node12@^1.0.7":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.11.tgz#ee3def1f27d9ed66dac6e46a295cffb0152e058d"
integrity sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==
"@tsconfig/node14@^1.0.0":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.3.tgz#e4386316284f00b98435bf40f72f75a09dabf6c1"
integrity sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==
"@tsconfig/node16@^1.0.2":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.4.tgz#0b92dcc0cc1c81f6f306a381f28e31b1a56536e9"
integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==
"@types/body-parser@*":
version "1.19.5"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.5.tgz#04ce9a3b677dc8bd681a17da1ab9835dc9d3ede4"
@ -63,7 +108,7 @@
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.5.tgz#1ef302e01cf7d2b5a0fa526790c9123bf1d06690"
integrity sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==
"@types/node@*":
"@types/node@*", "@types/node@^22.5.0":
version "22.5.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.5.0.tgz#10f01fe9465166b4cab72e75f60d8b99d019f958"
integrity sha512-DkFrJOe+rfdHTqqMg0bSNlGlQ85hSoh2TPzZyhHsXnMtligRWpxUySiyw8FY14ITt24HVCiQPWxS3KO/QlGmWg==
@ -105,6 +150,18 @@ accepts@~1.3.8:
mime-types "~2.1.34"
negotiator "0.6.3"
acorn-walk@^8.1.1:
version "8.3.3"
resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.3.3.tgz#9caeac29eefaa0c41e3d4c65137de4d6f34df43e"
integrity sha512-MxXdReSRhGO7VlFe1bRG/oI7/mdLV9B9JJT0N8vZOhF7gFRR5l3M8W9G8JxmKV+JC5mGqJ0QvqfSOLsCPa4nUw==
dependencies:
acorn "^8.11.0"
acorn@^8.11.0, acorn@^8.4.1:
version "8.12.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.12.1.tgz#71616bdccbe25e27a54439e0046e89ca76df2248"
integrity sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==
agent-base@6:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
@ -134,6 +191,11 @@ ansi-styles@^4.1.0:
dependencies:
color-convert "^2.0.1"
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
array-flatten@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
@ -283,6 +345,11 @@ core-util-is@1.0.2:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha512-3lqz5YjWTYnW6dlDa5TLaTCcShfar1e40rmcJVwCBJC6mWlFuj0eCHIElmG1g5kyuJ/GD+8Wn4FFCcz4gJPfaQ==
create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==
dashdash@^1.12.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/dashdash/-/dashdash-1.14.1.tgz#853cfa0f7cbe2fed5de20326b8dd581035f6e2f0"
@ -333,6 +400,11 @@ destroy@1.2.0:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.2.0.tgz#4803735509ad8be552934c67df614f94e66fa015"
integrity sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
dom-serializer@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/dom-serializer/-/dom-serializer-2.0.0.tgz#e41b802e1eedf9f6cae183ce5e622d789d7d8e53"
@ -749,6 +821,11 @@ lru-cache@^10.0.1:
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.4.3.tgz#410fc8a17b70e598013df257c2446b7f3383f119"
integrity sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
matrix-bot-sdk@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/matrix-bot-sdk/-/matrix-bot-sdk-0.7.1.tgz#b6b80d9f1a2c06795c61f114c12568ea78a1e694"
@ -1177,6 +1254,25 @@ tough-cookie@^2.3.3, tough-cookie@~2.5.0:
psl "^1.1.28"
punycode "^2.1.1"
ts-node@^10.9.2:
version "10.9.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.2.tgz#70f021c9e185bccdca820e26dc413805c101c71f"
integrity sha512-f0FFpIdcHgn8zcPSbf1dRevwt047YMnaiJM3u2w2RewrB+fob/zePZcrOyQoLMMO7aBIddLcQIEK5dYjkLnGrQ==
dependencies:
"@cspotcode/source-map-support" "^0.8.0"
"@tsconfig/node10" "^1.0.7"
"@tsconfig/node12" "^1.0.7"
"@tsconfig/node14" "^1.0.0"
"@tsconfig/node16" "^1.0.2"
acorn "^8.4.1"
acorn-walk "^8.1.1"
arg "^4.1.0"
create-require "^1.1.0"
diff "^4.0.1"
make-error "^1.1.1"
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"
tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
@ -1197,6 +1293,11 @@ type-is@~1.6.18:
media-typer "0.3.0"
mime-types "~2.1.24"
typescript@^5.5.4:
version "5.5.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.5.4.tgz#d9852d6c82bad2d2eda4fd74a5762a8f5909e9ba"
integrity sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==
undici-types@~6.19.2:
version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
@ -1224,6 +1325,11 @@ uuid@^3.3.2:
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
v8-compile-cache-lib@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"
integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==
vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
@ -1237,3 +1343,8 @@ verror@1.10.0:
assert-plus "^1.0.0"
core-util-is "1.0.2"
extsprintf "^1.2.0"
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==