Skip to content

Commit

Permalink
[FIX] Update to OpenAI 4, enabling GPT-4 and other models from the si…
Browse files Browse the repository at this point in the history
…te. Fix prompts and tests (#344)

* [FIX] Update to OpenAI 4, enabling GPT-4 and other models from the site. Fix prompts and tests

* 0.14.4
  • Loading branch information
earnestangel authored Mar 19, 2024
1 parent c02aee8 commit 853b552
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 47 deletions.
4 changes: 2 additions & 2 deletions config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ module.exports = {
// Provider Token
AIToken: "",

// Must be one of: davinci or gpt35.
GPTModel: "gpt35",
// Use any model in this page https://platform.openai.com/docs/models/overview
GPTModel: "gpt-4",

// URL of the local server for Ririko AI. Leave this empty if you don't have a local server.
// Example: http://localhost:5000/api/v1/ask
Expand Down
177 changes: 164 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ririko",
"version": "0.14.3",
"version": "0.14.4",
"description": "Ririko - A powerful AI-powered general Discord bot that you can call your companion",
"author": "Earnest Angel",
"email": "[email protected]",
Expand Down Expand Up @@ -85,7 +85,7 @@
"node-fetch": "^3.3.1",
"node-sd-webui": "^0.0.8",
"npm-run-all": "^4.1.5",
"openai": "^3.2.1",
"openai": "^4.29.1",
"parsec": "^2.0.2",
"pino": "^8.14.1",
"pino-pretty": "^10.0.0",
Expand Down
30 changes: 7 additions & 23 deletions src/app/Providers/AI/OpenAIProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { AIProviderBase } = require("app/Providers/AIProviderBase");
const config = require("config");
const getconfig = require("helpers/getconfig");
const openai = require("openai");
const { OpenAIApi, Configuration } = require("openai");
const OpenAIApi = require("openai");

class OpenAIProvider extends AIProviderBase {
constructor() {
Expand All @@ -14,11 +14,9 @@ class OpenAIProvider extends AIProviderBase {
return;
}

this.configuration = new Configuration({
this.openAiClient = new OpenAIApi({
apiKey: this.token,
});

this.openAiClient = new OpenAIApi(this.configuration);
}

getClient() {
Expand All @@ -37,7 +35,7 @@ class OpenAIProvider extends AIProviderBase {
const model = config.AI.GPTModel; // davinci or gpt35
const prompt = `${context}${history.join("\n")}\nHuman: ${messageText}\n`;

if (model === "gpt35") {
if (model) {
const convertedChatHistory = history.map((message) => {
const [role, content] = message.split(": ");
return {
Expand All @@ -49,10 +47,11 @@ class OpenAIProvider extends AIProviderBase {
const newPrompt = [
{ role: "system", content: context },
...convertedChatHistory,
{ role: "user", content: messageText }
];

const response = await this.openAiClient.createChatCompletion({
model: "gpt-3.5-turbo",
const response = await this.openAiClient.chat.completions.create({
model: model,
messages: newPrompt,
temperature: 1,
max_tokens: 2000,
Expand All @@ -61,22 +60,7 @@ class OpenAIProvider extends AIProviderBase {
presence_penalty: 0.9,
});

return response.data.choices[0].message.content; // Uncomment for GPT-3.5-turbo
} else if (model === "davinci") {
// Send request to OpenAI for text-davinci-002
// NOTE: text-davinci-003 is now removed from OpenAI API
const response = await this.openAiClient.createCompletion({
model: "text-davinci-002",
prompt,
temperature: 1,
max_tokens: 2000,
top_p: 0.2,
frequency_penalty: 0.9,
presence_penalty: 1.9,
stop: ["Human:"],
});

return response?.data.choices[0].text.split("Friend:")[1]?.trim();
return response.choices[0].message.content;
} else {
throw new Error("Invalid GPT model. Check config.js");
}
Expand Down
Loading

0 comments on commit 853b552

Please sign in to comment.