mirror of
https://github.com/Maks1mS/gpt4free-typescript.git
synced 2025-10-22 11:07:29 +03:00
add GptGo and example
This commit is contained in:
64
examples/01-simple-app/index.ts
Normal file
64
examples/01-simple-app/index.ts
Normal 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
1257
examples/01-simple-app/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
21
examples/01-simple-app/package.json
Normal file
21
examples/01-simple-app/package.json
Normal 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"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user