Skip to content

Commit

Permalink
Liar task
Browse files Browse the repository at this point in the history
  • Loading branch information
redji committed Mar 22, 2024
1 parent f5bef3c commit 04da7ce
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 40 deletions.
28 changes: 28 additions & 0 deletions openAPI/index.js
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
106 changes: 66 additions & 40 deletions prompt.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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}`, {
Expand All @@ -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: {
Expand All @@ -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 }],
Expand All @@ -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));
18 changes: 18 additions & 0 deletions utils/makeRequest.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 04da7ce

Please sign in to comment.