UNPKG

llm-pdf

Version:

Command Line tool to automate LLM and image models to generate a pdf by converting the generated text into markdown format and save it as a pdf document.

144 lines (116 loc) 4.66 kB
import Together from "together-ai"; // import dotenv from "dotenv"; import fs from 'fs/promises'; import convertMarkdownToPdf from "./savePDF.js"; import generatePrompt from "./promptGenerator.js"; import path from 'path'; const startingTime = Date.now(); // dotenv.config(); const together = new Together({ apiKey: "26d7651e279a9c77d6b0a1e3fdd5167788978e10ca35a02a02a0047fa6c714cc" }); async function manageDirectory() { try { await fs.access("./images"); const files = await fs.readdir("./images"); await Promise.all(files.map(file => fs.unlink(path.join("./images", file)))); } catch (err) { if (err.code === 'ENOENT') { await fs.mkdir("./images"); } else { console.error('Error accessing the directory:', err); } } } async function initializeConversationFile() { try { await fs.writeFile('conversation.md', '', 'utf-8'); } catch (err) { console.error('Error initializing conversation file:', err); } } async function generateContentArray() { try { const data = await fs.readFile('content.txt', 'utf8'); return data.split('\n').map(line => line.trim()).filter(Boolean); } catch (err) { console.error('Error reading content file:', err); return []; } } const systemPrompt = "You are a helpful college professor and you provide detailed and informative responses to the queries of the students. Please answer all questions thoroughly in simple and easy to understand language, try to give examples if required and explain in a detailed formal manner, try to use bullet points, use tables (if required). Answer in complex markdown format, meaning use lots and lots of markdown."; let messageArray = [ { role: "system", content: systemPrompt }, ]; async function appendToConversationFile(content) { try { await fs.appendFile('conversation.md', content, 'utf-8'); } catch (err) { console.error('Error writing to conversation file:', err); } } async function startConversation(userInput, languageModel) { try { const userMarkdown = `\n> ## ${userInput}\n`; await appendToConversationFile(userMarkdown); if (userInput.includes(" -img")) { const imageInMarkdown = await generatePrompt(userInput); await appendToConversationFile(`\n${imageInMarkdown}\n`); } messageArray.push({ role: "user", content: userInput }); const contextSize = 7; if (messageArray.length > contextSize) { messageArray = [messageArray[0], ...messageArray.slice(-contextSize)]; } const response = await together.chat.completions.create({ messages: messageArray, model: languageModel, // model: "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free", // model: "deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free", max_tokens: null, temperature: 0.7, top_p: 0.7, top_k: 50, repetition_penalty: 1, stop: ["/bye"], stream: true, }); let assistantResponse = ''; for await (const token of response) { const messageToken = token.choices[0]?.delta?.content || ''; process.stdout.write(messageToken); assistantResponse += messageToken; } console.log("\n"); const assistantMarkdown = `\n${assistantResponse}\n`; await appendToConversationFile(assistantMarkdown); const cleanedResponse = assistantResponse.replace(/<think>[\s\S]*?<\/think>/g, ''); messageArray.push({ role: "assistant", content: cleanedResponse }); } catch (error) { console.error(`\nError: ${error.message}\n`); } } async function main(author, bookname, mentions, languageModel) { await manageDirectory(); await initializeConversationFile(); const contentArray = await generateContentArray(); const initials = `\n<center>\n\n# ${bookname}\n\n### By **${author}** - *${mentions}*\n\n</center>\n` await appendToConversationFile(initials); for (let i = 0; i < contentArray.length; i++) { const line = contentArray[i]; if (line) { await startConversation(line, languageModel); } } await convertMarkdownToPdf(); fs.rm("./images", { recursive: true, force: true }, err => { if (err) { console.error(err); } }); fs.rm("./content.txt", { recursive: true, force: true }, err => { if (err) { console.log(err); } }); fs.rm("./conversation.md", { recursive: true, force: true }, err => { if (err) { console.log(err); } }); const endingTime = Date.now(); console.log("Total time taken: ", (endingTime - startingTime) / 1000); } export default main;