Advertisement
adam-techtailor

task 11

Jan 12th, 2025
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Command, CommandRunner } from 'nest-commander';
  2. import OpenAI from 'openai';
  3. import * as fs from 'fs';
  4. import * as path from 'path';
  5.  
  6. function concatenateTextFiles(directory: string): string {
  7.   // Read all files in the directory
  8.   const files = fs.readdirSync(directory);
  9.  
  10.   // Filter out non-.txt files
  11.   const txtFiles = files.filter((file) => path.extname(file) === '.txt');
  12.  
  13.   // Read and concatenate contents of each .txt file
  14.   const contents = txtFiles.map((file) => {
  15.     const filePath = path.join(directory, file);
  16.     return fs.readFileSync(filePath, 'utf-8');
  17.   });
  18.  
  19.   // Join all contents with two empty lines
  20.   return contents.join('\n\n');
  21. }
  22.  
  23. @Command({ name: 'task11', options: { isDefault: false } })
  24. export class Task11Command extends CommandRunner {
  25.   async run(inputs: string[], options: Record<string, any>) {
  26.     console.log({ inputs, options });
  27.  
  28.     /*
  29.     const facts = concatenateTextFiles('./src/task11/facts');
  30.  
  31.     const openai = new OpenAI();
  32.  
  33.     const files = fs.readdirSync('./src/task11/reports');
  34.     const reports = files.filter((file) => path.extname(file) === '.txt');
  35.  
  36.     const keywordsMap = {};
  37.     // Read and concatenate contents of each .txt file
  38.     for (const report of reports) {
  39.       const filePath = path.join('./src/task11/reports', report);
  40.       const reportCont = fs.readFileSync(filePath, 'utf-8');
  41.  
  42.       const keywords = await this.getKeywords(
  43.         facts,
  44.         report + ': ' + reportCont,
  45.         openai,
  46.       );
  47.       keywordsMap[report] = keywords;
  48.     }
  49.  
  50.     console.log(keywordsMap);
  51.     fs.writeFileSync('./src/task11/keywords.json', JSON.stringify(keywordsMap));
  52. */
  53.  
  54.     const keywordsMap = JSON.parse(
  55.       fs.readFileSync('./src/task11/keywords.json', 'utf-8'),
  56.     );
  57.     const apiRest2 = await fetch('https://centrala.ag3nts.org/report', {
  58.       method: 'POST',
  59.       headers: {
  60.         'Content-Type': 'application/json',
  61.       },
  62.       body: JSON.stringify({
  63.         task: 'dokumenty',
  64.         apikey: '1bed77d2-4be1-4c28-b776-706fc3a359bb',
  65.         answer: keywordsMap,
  66.       }),
  67.     });
  68.  
  69.     console.log(await apiRest2.json());
  70.   }
  71.  
  72.   private async getKeywords(facts: string, report: string, openai: OpenAI) {
  73.     const messages: { role: 'user' | 'system'; content: string }[] = [
  74.       {
  75.         role: 'system',
  76.         content: `
  77. <facts>
  78. ${facts}
  79. </facts>
  80. For the report below identify any facts that are relevant to the person mentioned in the report.
  81. Return a combined list of keywords of relevant facts and the report itself. List should be extensive and helpful in keyword search.
  82. 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"}
  83. <report>
  84. ${report}
  85. </report>
  86. `,
  87.       },
  88.     ];
  89.  
  90.     const completion = await openai.chat.completions.create({
  91.       model: 'gpt-4o',
  92.       messages: messages,
  93.     });
  94.  
  95.     const answer = completion.choices[0].message.content;
  96.     console.log(answer);
  97.     return answer;
  98.   }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement