diff --git a/openAPI/index.js b/openAPI/index.js new file mode 100644 index 0000000..92c12b7 --- /dev/null +++ b/openAPI/index.js @@ -0,0 +1,28 @@ +import { config } from 'dotenv'; +import OpenAI from 'openai'; +import { makeRequestWithDelay } from "../utils/makeRequest.js" +config(); +const OpenAIKey = process.env['OPENAI_API_KEY']; + + +const openai = new OpenAI({ + apiKey: OpenAIKey // This is the default and can be omitted + }); + +export async function chatCompletion(createObject) { + return await openai.chat.completions.create(createObject); +} +export async function moderations (input) { + return await makeRequestWithDelay(`https://api.openai.com/v1/moderations`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${OpenAIKey}` + }, + body: JSON.stringify({ input }) + }); +} +export default { + moderations, + chatCompletion +} \ No newline at end of file diff --git a/package.json b/package.json index 16f4b87..84e22b1 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,9 @@ "author": "MichaƂ Furmaniak", "license": "ISC", "dependencies": { + "axios": "^1.6.8", "dotenv": "^16.4.5", + "form-data": "^4.0.0", "langchain": "^0.1.28", "node-fetch": "^3.3.2", "openai": "^4.29.2" diff --git a/prompt.js b/prompt.js index 6caa204..128734c 100644 --- a/prompt.js +++ b/prompt.js @@ -1,35 +1,19 @@ import fetch from 'node-fetch'; -import OpenAI from 'openai'; +//import shouldStringBeAllowed from './openAPI/moderation/moderate' import { config } from 'dotenv'; +import { chatCompletion, moderations } from './openAPI/index.js'; +import { makeRequestWithDelay } from './utils/makeRequest.js'; +import FormData from 'form-data'; +import axios from 'axios' config(); - -const OpenAIKey = process.env['OPENAI_API_KEY']; const APIKey = process.env['API_KEY']; -const openai = new OpenAI({ - apiKey: OpenAIKey // This is the default and can be omitted - }); - let moderationAnswer = []; let blogAnswer = []; - -// Function to make a request with a delay -async function makeRequestWithDelay(url, options, delay) { - return new Promise((resolve, reject) => { - setTimeout(async () => { - try { - const response = await fetch(url, options); - const data = await response.json(); - resolve(data); - } catch (error) { - reject(error); - } - }, delay); - }); -} - -// First curl command +let liarQuestion = ""; +let liarAnswer = []; +//helloApi fetch('https://tasks.aidevs.pl/token/helloapi', { method: 'POST', headers: { @@ -61,8 +45,7 @@ fetch('https://tasks.aidevs.pl/token/helloapi', { console.log(response3); }) .catch(error => console.error('Error:', error)); - -// Second curl command +//moderation fetch('https://tasks.aidevs.pl/token/moderation', { method: 'POST', headers: { @@ -86,15 +69,7 @@ fetch('https://tasks.aidevs.pl/token/moderation', { moderationAnswer = []; // Clear previous values for (const element of response2.input) { - const response3 = await makeRequestWithDelay(`https://api.openai.com/v1/moderations`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${OpenAIKey}` - }, - body: JSON.stringify({ input: element }) - }); - moderationAnswer.push(response3.results[0].flagged ? 1 : 0); + moderationAnswer.push(shouldStringBeAllowed(element) ? 1 : 0); } // Third curl command with a delay of 2000 milliseconds (2 seconds) const response4 = await makeRequestWithDelay(`https://tasks.aidevs.pl/answer/${token}`, { @@ -108,7 +83,7 @@ fetch('https://tasks.aidevs.pl/token/moderation', { }) .catch(error => console.error('Error:', error)); -// Second curl command +//blogger fetch('https://tasks.aidevs.pl/token/blogger', { method: 'POST', headers: { @@ -120,17 +95,16 @@ fetch('https://tasks.aidevs.pl/token/blogger', { const data = await response.json(); const token = data.token; const url = `https://tasks.aidevs.pl/task/${token}`; - - // Second curl command with a delay of 2000 milliseconds (2 seconds) + const response2 = await makeRequestWithDelay(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } }, 10); - + blogAnswer = []; // Clear previous values - + for (const element of response2.blog) { const chatCompletion = await openai.chat.completions.create({ messages: [{ role: 'user', content: 'Napisz 4 zdania na temat: ' + element }], @@ -148,4 +122,56 @@ fetch('https://tasks.aidevs.pl/token/blogger', { body: JSON.stringify({answer: blogAnswer}) }, 10); }) +.catch(error => console.error('Error:', error)); + +//liar +fetch('https://tasks.aidevs.pl/token/liar', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({apikey: APIKey}) +}) +.then(async (response) => { + const data = await response.json(); + const token = data.token; + const url = `https://tasks.aidevs.pl/task/${token}`; + + await chatCompletion({ + messages: [{ role: 'user', content: 'No chit-chat, ask one question'}], + model: 'gpt-3.5-turbo', + }).then(async (response) => { + liarQuestion = response.choices[0].message.content; + liarAnswer = []; // Clear previous values + axios.get(url).then((response)=>{ + console.log(response.data); + const form = new FormData(); + form.append('question', liarQuestion); + axios.post(url, form, { + headers: form.getHeaders() + }) + .then(async (response) => { + console.log('Response:', response.data); + await chatCompletion({ + messages: [{ role: 'user', content: 'Return YES or NO, uppercase. Is it true that for question: "' + liarQuestion + '" The answer might be: "' + response.data.answer + '".'}], + model: 'gpt-3.5-turbo', + }).then(async (response) => { + console.log('Response2', response.choices[0].message.content) + const response4 = await makeRequestWithDelay(`https://tasks.aidevs.pl/answer/${token}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({answer: response.choices[0].message.content}) + }, 10); + console.log('Answer from API', response4) + }); + }) + .catch((error) => { + // Handle error + console.error('Error:', error); + }); + }); + }); +}) .catch(error => console.error('Error:', error)); \ No newline at end of file diff --git a/utils/makeRequest.js b/utils/makeRequest.js new file mode 100644 index 0000000..2b1d7a5 --- /dev/null +++ b/utils/makeRequest.js @@ -0,0 +1,18 @@ +// Function to make a request with a delay +export async function makeRequestWithDelay(url, options, delay) { + return new Promise((resolve, reject) => { + setTimeout(async () => { + try { + const response = await fetch(url, options); + const data = await response.json(); + resolve(data); + } catch (error) { + reject(error); + } + }, delay); + }); +} + +export default { + makeRequestWithDelay +} \ No newline at end of file