mirror of
https://github.com/tcsenpai/PerplexiBot.git
synced 2025-06-04 10:20:06 +00:00
improved lt memory
This commit is contained in:
parent
d9f3f90bf3
commit
19b365aae0
63
index.ts
63
index.ts
@ -2,35 +2,38 @@ import Perplexity from "./libs/Perplexity";
|
||||
import "dotenv/config";
|
||||
import required from "./libs/required";
|
||||
import readin from "./libs/readin";
|
||||
import fs from "fs"
|
||||
import parseCommands from "./libs/parseCommands";
|
||||
import config from "./config"
|
||||
import type Message from "./libs/Perplexity"
|
||||
import type { ListFormat } from "typescript";
|
||||
import fs from "fs";
|
||||
import config from "./config";
|
||||
import { ContextPart, parseCommands } from "./libs/parseCommands";
|
||||
|
||||
required(process.env.PPLX_API_KEY);
|
||||
const PPLX_API_KEY = process.env.PPLX_API_KEY as string;
|
||||
let perplexity: Perplexity;
|
||||
|
||||
// NOTE Helper to load context from file
|
||||
function inject_context() {
|
||||
let loaded_context = fs.readFileSync("./context.json", {encoding: "utf-8"})
|
||||
let list_context = JSON.parse(loaded_context)
|
||||
for (let i=0; i<list_context.length; i++) {
|
||||
perplexity.add_to_context(list_context[i])
|
||||
}
|
||||
function inject_context(verbose: boolean = true) {
|
||||
let loaded_context = fs.readFileSync("./context.json", { encoding: "utf-8" });
|
||||
let list_context = JSON.parse(loaded_context) as ContextPart[];
|
||||
for (let i = 0; i < list_context.length; i++) {
|
||||
let msg = list_context[i] as ContextPart;
|
||||
perplexity.add_to_context(msg);
|
||||
if (verbose) console.log("[" + msg.role + "]> " + msg.content);
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE Helper to save context long term
|
||||
function save_in_context(role: string, message: string) {
|
||||
let loaded_context = fs.readFileSync("./context.json", {encoding: "utf-8"})
|
||||
let list_context = JSON.parse(loaded_context) as Array<{"role": string, "content": string}>
|
||||
let new_insertion = {
|
||||
role: role,
|
||||
content: message
|
||||
}
|
||||
list_context.push(new_insertion)
|
||||
fs.writeFileSync("./context.json", JSON.stringify(list_context))
|
||||
let loaded_context = fs.readFileSync("./context.json", { encoding: "utf-8" });
|
||||
let list_context = JSON.parse(loaded_context) as Array<{
|
||||
role: string;
|
||||
content: string;
|
||||
}>;
|
||||
let new_insertion = {
|
||||
role: role,
|
||||
content: message,
|
||||
};
|
||||
list_context.push(new_insertion);
|
||||
fs.writeFileSync("./context.json", JSON.stringify(list_context));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@ -39,14 +42,14 @@ async function main() {
|
||||
perplexity = new Perplexity(
|
||||
PPLX_API_KEY,
|
||||
"https://api.perplexity.ai",
|
||||
"sonar-medium-chat",
|
||||
"sonar-small-chat",
|
||||
false,
|
||||
{}
|
||||
);
|
||||
// Setting our ai personality
|
||||
perplexity.add_to_context(config);
|
||||
// If any, inject context
|
||||
inject_context()
|
||||
inject_context();
|
||||
// Chatting
|
||||
await chat();
|
||||
}
|
||||
@ -55,18 +58,18 @@ async function chat(loop: boolean = true, save: boolean = true) {
|
||||
var proceed = true;
|
||||
while (proceed) {
|
||||
proceed = loop;
|
||||
let question = await readin("You: ");
|
||||
let question = await readin("[You]> ");
|
||||
let parsed = parseCommands(question);
|
||||
proceed = parsed.proceed
|
||||
let logpart = parsed.logpart
|
||||
proceed = parsed.proceed;
|
||||
let logpart = parsed.logpart;
|
||||
let response = await perplexity.ask(question);
|
||||
console.log("Assistant: " + response);
|
||||
console.log("[Assistant]> " + response);
|
||||
if (save) {
|
||||
save_in_context("user", question)
|
||||
if (logpart) {
|
||||
response += +"\n[" + logpart + "]"
|
||||
}
|
||||
save_in_context("assistant", response as string)
|
||||
save_in_context("user", question);
|
||||
if (logpart) {
|
||||
response += " " + logpart;
|
||||
}
|
||||
save_in_context("assistant", response as string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ export interface ContextPart {
|
||||
content: string
|
||||
}
|
||||
|
||||
export default function parseCommands(input: string): {proceed: boolean, logpart: string | null} {
|
||||
export function parseCommands(input: string): {proceed: boolean, logpart: string | null} {
|
||||
let proceed = true
|
||||
let logpart = null
|
||||
switch (input) {
|
||||
@ -13,7 +13,7 @@ export default function parseCommands(input: string): {proceed: boolean, logpart
|
||||
case "end":
|
||||
case "bye":
|
||||
proceed = false
|
||||
logpart = "Last conversation ended at: " + String(Date.now())
|
||||
logpart = "{ Memory: Last conversation ended at: " + String(Date.now() + " }")
|
||||
break
|
||||
default:
|
||||
break
|
||||
|
@ -11,7 +11,7 @@
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"verbatimModuleSyntax": false,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
|
Loading…
x
Reference in New Issue
Block a user