2024-08-22 15:36:49 -05:00
|
|
|
import { Ollama } from "@langchain/ollama";
|
|
|
|
import { PuppeteerWebBaseLoader } from "@langchain/community/document_loaders/web/puppeteer";
|
|
|
|
import * as path from 'path'
|
|
|
|
import http from 'http';
|
|
|
|
import { URL } from 'url';
|
|
|
|
const debugMode = false
|
|
|
|
const __dirname = path.resolve();
|
|
|
|
|
|
|
|
function llamaLoader(model="sparksammy/samantha-3.1", url="http://localhost:11434") {
|
|
|
|
return new Ollama({
|
|
|
|
baseUrl: url, // Default value
|
|
|
|
model: model, // Default value
|
|
|
|
});
|
|
|
|
}
|
2024-04-29 22:12:44 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
const ollama = llamaLoader()
|
2024-04-29 22:12:44 -05:00
|
|
|
|
|
|
|
|
2024-04-29 23:03:13 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
async function loadDoc(www) {
|
|
|
|
try {
|
|
|
|
const loader = new PuppeteerWebBaseLoader(String(www), {
|
|
|
|
launchOptions: {
|
|
|
|
headless: true,
|
|
|
|
},
|
|
|
|
gotoOptions: {
|
|
|
|
waitUntil: "domcontentloaded",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const docs = await loader.load();
|
|
|
|
return docs[0].pageContent;
|
|
|
|
} catch {
|
|
|
|
return ""
|
2024-04-29 23:03:13 -05:00
|
|
|
}
|
2024-08-22 15:36:49 -05:00
|
|
|
}
|
2024-04-29 23:03:13 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
async function getReply(q, www='docChatterThing.testfile.invalid') {
|
|
|
|
let document
|
|
|
|
let query
|
|
|
|
if (www == "docChatterThing.testfile.invalid" || www == "") {
|
|
|
|
document = "Chatting."
|
|
|
|
} else {
|
|
|
|
document = await loadDoc(www)
|
2024-04-29 23:03:13 -05:00
|
|
|
}
|
2024-08-22 15:36:49 -05:00
|
|
|
|
|
|
|
query = String(`Context: ${document} Query: ${q}`)
|
|
|
|
|
|
|
|
if (debugMode) {
|
|
|
|
console.log(query) //Debug feature that prints out the user's query.
|
2024-04-29 22:12:44 -05:00
|
|
|
}
|
2024-08-22 15:36:49 -05:00
|
|
|
|
|
|
|
const stream = await ollama.stream(
|
|
|
|
query
|
|
|
|
);
|
|
|
|
|
|
|
|
const chunks = [];
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
chunks.push(chunk);
|
2024-04-29 22:12:44 -05:00
|
|
|
}
|
2024-08-22 15:36:49 -05:00
|
|
|
|
|
|
|
return chunks.join("");
|
|
|
|
}
|
2024-04-29 22:12:44 -05:00
|
|
|
|
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
async function main(port) {
|
|
|
|
const hostname = '127.0.0.1';
|
2024-04-29 22:12:44 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
res.statusCode = 200;
|
2024-04-29 22:12:44 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
// Create URL object and parse query parameters
|
|
|
|
const urlObj = new URL(req.url, `http://${req.headers.host}`);
|
|
|
|
const queryParams = urlObj.searchParams;
|
2024-04-29 22:12:44 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
// Get query parameters with default values if not provided
|
|
|
|
const q = queryParams.get('q') || "Create an error 500 message explaining that the user didn't input a query and optionally a URL.";
|
|
|
|
const www = queryParams.get('www') || "docChatterThing.testfile.invalid";
|
2024-04-29 22:12:44 -05:00
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
// Get reply and send response
|
|
|
|
getReply(q, www).then(answer => {
|
|
|
|
res.setHeader('Content-Type', 'text/plain');
|
|
|
|
res.end(String(answer));
|
|
|
|
}).catch(err => {
|
|
|
|
res.statusCode = 500;
|
|
|
|
res.end(`Internal Server Error: ${err.message}`);
|
2024-04-29 22:12:44 -05:00
|
|
|
});
|
|
|
|
});
|
2024-08-22 15:36:49 -05:00
|
|
|
|
|
|
|
server.listen(port, hostname, () => {
|
|
|
|
console.log(`Server running at http://${hostname}:${port}/`);
|
|
|
|
});
|
2024-04-29 22:12:44 -05:00
|
|
|
}
|
|
|
|
|
2024-08-22 15:36:49 -05:00
|
|
|
main(8543)
|