add GptGo and example

This commit is contained in:
2024-01-11 22:54:48 +03:00
parent 952dd831d9
commit 842aa4b7f6
9 changed files with 2975 additions and 2 deletions

View File

@@ -0,0 +1,64 @@
import { PromptTemplate } from "@langchain/core/prompts";
import { BufferMemory } from "langchain/memory";
// @ts-ignore
import { GptGo } from "gpt4free-typescript";
import { LLMChain } from "langchain/chains";
import { createInterface } from "readline/promises";
const serializeChatHistory = (chatHistory: string | Array<string>) => {
if (Array.isArray(chatHistory)) {
return chatHistory.join("\n");
}
return chatHistory;
};
const model = new GptGo();
const memory = new BufferMemory({
memoryKey: "chatHistory",
});
const prompt = PromptTemplate.fromTemplate(
`Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
CHAT HISTORY: {chatHistory}
----------------
QUESTION: {question}
----------------
Helpful Answer:`
);
const chain = new LLMChain({
llm: model,
prompt,
});
const rl = createInterface({
input: process.stdin,
output: process.stdout,
});
console.log('Bot: Hello! How can I assist you today? (Type "exit" to quit)');
const run = async () => {
let question;
do {
question = await rl.question("You: ");
if (question != "exit") {
const memoryResult = await memory.loadMemoryVariables({});
const { text } = await chain.call({
question,
chatHistory: serializeChatHistory(memoryResult.chatHistory ?? ""),
});
console.log(`Bot: ${text}`);
await memory.saveContext(
{
human: question,
},
{
ai: text,
}
);
}
} while (question != "exit");
};
run();

1257
examples/01-simple-app/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,21 @@
{
"name": "01-simple-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.3.3"
},
"dependencies": {
"@langchain/core": "^0.1.12",
"gpt4free-typescript": "file:../..",
"langchain": "^0.1.2",
"readline": "^1.3.0"
}
}