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