Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Command, CommandRunner } from 'nest-commander';
- import OpenAI from 'openai';
- import * as fs from 'fs';
- import * as path from 'path';
- function concatenateTextFiles(directory: string): string {
- // Read all files in the directory
- const files = fs.readdirSync(directory);
- // Filter out non-.txt files
- const txtFiles = files.filter((file) => path.extname(file) === '.txt');
- // Read and concatenate contents of each .txt file
- const contents = txtFiles.map((file) => {
- const filePath = path.join(directory, file);
- return fs.readFileSync(filePath, 'utf-8');
- });
- // Join all contents with two empty lines
- return contents.join('\n\n');
- }
- @Command({ name: 'task11', options: { isDefault: false } })
- export class Task11Command extends CommandRunner {
- async run(inputs: string[], options: Record<string, any>) {
- console.log({ inputs, options });
- /*
- const facts = concatenateTextFiles('./src/task11/facts');
- const openai = new OpenAI();
- const files = fs.readdirSync('./src/task11/reports');
- const reports = files.filter((file) => path.extname(file) === '.txt');
- const keywordsMap = {};
- // Read and concatenate contents of each .txt file
- for (const report of reports) {
- const filePath = path.join('./src/task11/reports', report);
- const reportCont = fs.readFileSync(filePath, 'utf-8');
- const keywords = await this.getKeywords(
- facts,
- report + ': ' + reportCont,
- openai,
- );
- keywordsMap[report] = keywords;
- }
- console.log(keywordsMap);
- fs.writeFileSync('./src/task11/keywords.json', JSON.stringify(keywordsMap));
- */
- const keywordsMap = JSON.parse(
- fs.readFileSync('./src/task11/keywords.json', 'utf-8'),
- );
- const apiRest2 = await fetch('https://centrala.ag3nts.org/report', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- task: 'dokumenty',
- apikey: '1bed77d2-4be1-4c28-b776-706fc3a359bb',
- answer: keywordsMap,
- }),
- });
- console.log(await apiRest2.json());
- }
- private async getKeywords(facts: string, report: string, openai: OpenAI) {
- const messages: { role: 'user' | 'system'; content: string }[] = [
- {
- role: 'system',
- content: `
- <facts>
- ${facts}
- </facts>
- For the report below identify any facts that are relevant to the person mentioned in the report.
- Return a combined list of keywords of relevant facts and the report itself. List should be extensive and helpful in keyword search.
- Keywords should be in Polish in Nominative. Return them as array of strings like this {"keywords": "Człowiek", "Javascript", "Jan Kowalski", "Sektor A", "Godzina 8:45", "2024-11-12", "Nauczyciel"}
- <report>
- ${report}
- </report>
- `,
- },
- ];
- const completion = await openai.chat.completions.create({
- model: 'gpt-4o',
- messages: messages,
- });
- const answer = completion.choices[0].message.content;
- console.log(answer);
- return answer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement