Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Chat GPT and BARD Code #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .github/workflows/lint-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ on:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]



jobs:
build:
Expand All @@ -28,11 +26,11 @@ jobs:
- name: Install dependencies
run: |
npm install

- name: Lint Check
run: |
npm run lint

- name: Prettier Format Check
run: |
run: |
npm run format:check
97 changes: 52 additions & 45 deletions src/filler/engines/gpt-engine.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,94 @@
import GPTEngine from '../../utils/gpt-engines';
import { Configuration, OpenAIApi } from 'openai';
// import process from "process";
import dotenv from 'dotenv';

Check failure on line 3 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `·`
// Import dotenv for handling environment variables

dotenv.config({ path: './develop.sample.env' });

export class GeneratorEngine {
constructor(engine) {
// Input Type : String based on GPTEngine enum
// Specifying the type of GPT engine to query response

this.modelLock = false; // Lock variable for call
this.modelLock = false;

if (engine) {
this.engine = engine;
this.selectedEngine = engine;
} else {
// Specifying the default GPT engine as CHATGPT engine in case of no inputs
this.engine = GPTEngine.CHATGPT;
this.selectedEngine = GPTEngine.CHATGPT;

Check failure on line 15 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `·`
}
if (this.engine === GPTEngine.CHATGPT) {
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,

if (this.selectedEngine === GPTEngine.CHATGPT) {
const openaiConfiguration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,

Check failure on line 20 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `·`
});
this.openai = new OpenAIApi(configuration);
this.chatGpt = new OpenAIApi(openaiConfiguration);
}
}

async getResponse(promptText) {
// Input Type : prompt:string containing the string
// Get the personalized prompt based on type of question and value
// Output Type : string containing the response or null if any error arose

if (promptText !== null) {
// Calling respective engine based on value
if (this.engine === GPTEngine.CHATGPT) {
let response = await Promise.resolve(this.askChatGPT(promptText));
return response;
} else if (this.engine === GPTEngine.BARD) {
return this.askBard(promptText);
if (this.selectedEngine === GPTEngine.CHATGPT) {
let chatGptResponse = await this.askChatGpt(promptText);
return chatGptResponse;
} else if (this.selectedEngine === GPTEngine.BARD) {
let bardResponse = await this.askBard(promptText);
return bardResponse;
}
}

return null;
}

async askChatGPT(prompt) {
// Input Type : prompt:string containing the string
// Output Type : string containing the output, null if any error
// TODO
// Call chatgpt using API, don't push API keys in code
async askChatGpt(prompt) {
if (this.modelLock === false) {
this.modelLock = true;
let responseContent = null;
let chatGptResponseContent = null;
try {
const response = await this.openai.createChatCompletion({
const chatGptResponse = await this.chatGpt.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
messages: [{ role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: prompt }],

Check failure on line 47 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `{·role:·'system',·content:·'You·are·a·helpful·assistant.'·},·{·role:·'user',·content:·prompt·}` with `⏎↹↹↹↹↹↹{·role:·'system',·content:·'You·are·a·helpful·assistant.'·},⏎↹↹↹↹↹↹{·role:·'user',·content:·prompt·},⏎↹↹↹↹↹`
});
responseContent = response.data.choices[0].message.content;
console.log(responseContent);
chatGptResponseContent = chatGptResponse.data.choices[0].message.content;

Check failure on line 49 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `·` with `⏎↹↹↹↹↹`
console.log(chatGptResponseContent);
} catch (error) {
if (error.response && error.response.status === 429) {
const retryAfter =
parseInt(error.response.headers['retry-after']) || 1;
const retryAfter = parseInt(error.response.headers['retry-after']) || 1;

Check failure on line 53 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `·` with `⏎↹↹↹↹↹↹`
console.log(`Rate limited. Retrying after ${retryAfter} seconds...`);
} else if (error.response) {
console.error('API call error:', error.message);
const retryAfter =
parseInt(error.response.headers['retry-after']) || 1;
console.error('ChatGPT API call error:', error.message);
const retryAfter = parseInt(error.response.headers['retry-after']) || 1;

Check failure on line 57 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `·` with `⏎↹↹↹↹↹↹`
console.log(`Rate limited. Retrying after ${retryAfter} seconds...`);
} else {
console.warn(error);
}
}
this.modelLock = false;
return responseContent;
return chatGptResponseContent;
}
// Promise.resolve((resolve) => setTimeout(resolve, 1000))
return 'Already processing some other request! Try later';
}

askBard(prompt) {
// Input Type : prompt:string containing the string
// Output Type : string containing the output, null if any error
// TODO
// Call Bard using API calls, don't push API keys in code
return 'Response of Prompt from Bard based on prompt';
async askBard(prompt) {
if (this.modelLock === false) {
this.modelLock = true;
let bardResponseContent = null;
try {

Check failure on line 73 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Delete `·`
// BARD API Call, Replace 'BARD_API_KEY' with your actual Bard API key
const bardApiResponse = await fetch('https://api.bardengine.com/endpoint', {

Check failure on line 75 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Replace `'https://api.bardengine.com/endpoint',·` with `⏎↹↹↹↹↹'https://api.bardengine.com/endpoint',⏎↹↹↹↹↹`
method: 'POST',

Check failure on line 76 in src/filler/engines/gpt-engine.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Insert `↹`
headers: {
'Content-Type': 'application/json',
'Authorization': 'BARD_API_KEY',
},
body: JSON.stringify({ prompt }),
});

bardResponseContent = await bardApiResponse.text();
console.log(bardResponseContent);
} catch (error) {
console.error('Bard API call error:', error);
}
this.modelLock = false;
return bardResponseContent;
}
return 'Already processing some other request! Try later';
}
}
Loading