-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from jucasoliveira/80-npm-g-install-terminalgp…
…t-failed-on-mac feature/new fix for terminalGPT
- Loading branch information
Showing
7 changed files
with
3,773 additions
and
3,654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
//const fs = require("fs"); | ||
|
||
//const contextFile = `${__dirname}/../data/context-terminal-gpt.txt`; | ||
|
||
import {ChatCompletionRequestMessage} from "openai"; | ||
let context: any[] = []; | ||
|
||
let context: ChatCompletionRequestMessage[] = []; | ||
|
||
export function addContext(text: ChatCompletionRequestMessage) { | ||
context = [...context, text]; | ||
export function addContext(text: any) { | ||
context = [...context, text]; | ||
} | ||
|
||
export const getContext = () => context | ||
export const getContext = () => context; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,85 @@ | ||
import chalk from "chalk"; | ||
|
||
import {Configuration, OpenAIApi} from "openai"; | ||
import OpenAI from "openai"; | ||
|
||
import {addContext, getContext} from "./context"; | ||
|
||
import {loadWithRocketGradient} from "./gradient"; | ||
import { addContext, getContext } from "./context"; | ||
|
||
import { loadWithRocketGradient } from "./gradient"; | ||
|
||
export default async ( | ||
apiKey: string | Promise<string>, | ||
prompt: string, | ||
opts: { | ||
engine: string; | ||
temperature: unknown; | ||
}, | ||
url: string | undefined | ||
apiKey: string | Promise<string>, | ||
prompt: string, | ||
opts: { | ||
engine: string; | ||
temperature: unknown; | ||
} | ||
) => { | ||
const configuration = new Configuration({ | ||
apiKey, | ||
basePath: url | ||
}); | ||
|
||
const openai = new OpenAIApi(configuration); | ||
const spinner = loadWithRocketGradient("Thinking...").start(); | ||
const apiKeyValue = await apiKey; | ||
const openai = new OpenAI({ apiKey: apiKeyValue }); | ||
const spinner = loadWithRocketGradient("Thinking...").start(); | ||
|
||
addContext({ | ||
"role": "system", | ||
"content": "Read the context, when returning the answer , always wrapping block of code exactly within triple backticks " | ||
}); | ||
addContext({"role": "user", "content": prompt}); | ||
addContext({ | ||
role: "system", | ||
content: | ||
"Read the context, when returning the answer , always wrapping block of code exactly within triple backticks ", | ||
}); | ||
addContext({ role: "user", content: prompt }); | ||
|
||
const request = await openai.createChatCompletion({ | ||
model: opts.engine || "gpt-3.5-turbo", | ||
messages: getContext(), | ||
temperature: opts.temperature ? Number(opts.temperature) : 1 | ||
const request = await openai.chat.completions | ||
.create({ | ||
model: opts.engine || "gpt-4-1106-preview", | ||
messages: getContext(), | ||
temperature: opts.temperature ? Number(opts.temperature) : 1, | ||
}) | ||
.then((res) => { | ||
if (typeof res.choices[0].message !== "undefined") { | ||
addContext(res.choices[0].message); | ||
spinner.stop(); | ||
return res.choices[0].message; | ||
} else { | ||
throw new Error("Undefined messages received"); | ||
} | ||
}) | ||
.then((res) => { | ||
if (typeof res.data.choices[0].message !== 'undefined') { | ||
addContext(res.data.choices[0].message); | ||
spinner.stop(); | ||
return res.data.choices[0].message | ||
} else { | ||
throw new Error("Undefined messages received") | ||
} | ||
}) | ||
.catch((err) => { | ||
spinner.stop(); | ||
switch (err["response"]["status"]) { | ||
case 404: | ||
throw new Error( | ||
`${chalk.red( | ||
"Not Found: Model not found. Please check the model name." | ||
)}` | ||
); | ||
case 429: | ||
throw new Error( | ||
`${chalk.red( | ||
"API Rate Limit Exceeded: ChatGPT is getting too many requests from the user in a short period of time. Please wait a while before sending another message." | ||
)}` | ||
) | ||
case 400: | ||
throw new Error( | ||
`${chalk.red( | ||
"Bad Request: Prompt provided is empty or too long. Prompt should be between 1 and 4096 tokens." | ||
)}` | ||
); | ||
case 402: | ||
throw new Error( | ||
`${chalk.red( | ||
"Payment Required: ChatGPT quota exceeded. Please check you chatGPT account." | ||
)}` | ||
); | ||
case 503: | ||
throw new Error( | ||
`${chalk.red( | ||
"Service Unavailable: ChatGPT is currently unavailable, possibly due to maintenance or high traffic. Please try again later." | ||
)}` | ||
); | ||
default: | ||
throw new Error(`${err}`); | ||
} | ||
}) | ||
if (request === undefined || !request?.content) { | ||
throw new Error("Undefined request or content"); | ||
} | ||
.catch((err) => { | ||
spinner.stop(); | ||
switch (err["response"]["status"]) { | ||
case 404: | ||
throw new Error( | ||
`${chalk.red( | ||
"Not Found: Model not found. Please check the model name." | ||
)}` | ||
); | ||
case 429: | ||
throw new Error( | ||
`${chalk.red( | ||
"API Rate Limit Exceeded: ChatGPT is getting too many requests from the user in a short period of time. Please wait a while before sending another message." | ||
)}` | ||
); | ||
case 400: | ||
throw new Error( | ||
`${chalk.red( | ||
"Bad Request: Prompt provided is empty or too long. Prompt should be between 1 and 4096 tokens." | ||
)}` | ||
); | ||
case 402: | ||
throw new Error( | ||
`${chalk.red( | ||
"Payment Required: ChatGPT quota exceeded. Please check you chatGPT account." | ||
)}` | ||
); | ||
case 503: | ||
throw new Error( | ||
`${chalk.red( | ||
"Service Unavailable: ChatGPT is currently unavailable, possibly due to maintenance or high traffic. Please try again later." | ||
)}` | ||
); | ||
default: | ||
throw new Error(`${err}`); | ||
} | ||
}); | ||
if (request === undefined || !request?.content) { | ||
throw new Error("Undefined request or content"); | ||
} | ||
|
||
return request | ||
} | ||
return request; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.