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

Feature/add image generate model #328

Open
wants to merge 8 commits into
base: staging
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ OPENAI_COMPLETION_MAX_TOKENS=
OPENAI_COMPLETION_FREQUENCY_PENALTY=
OPENAI_COMPLETION_PRESENCE_PENALTY=
OPENAI_IMAGE_GENERATION_SIZE=
OPENAI_IMAGE_GENERATION_MODEL=
OPENAI_IMAGE_GENERATION_QUALITY=

LINE_TIMEOUT=
LINE_CHANNEL_ACCESS_TOKEN=
Expand All @@ -43,3 +45,8 @@ LINE_CHANNEL_SECRET=
SERPAPI_TIMEOUT=
SERPAPI_API_KEY=
SERPAPI_LOCATION=

PROVIDER_BASE_URL=
PROVIDER_BASE_TOKEN=
PROVIDER_BASE_MODEL=

7 changes: 6 additions & 1 deletion config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,25 @@ const config = Object.freeze({
VERCEL_DEPLOY_HOOK_URL: env.VERCEL_DEPLOY_HOOK_URL || null,
OPENAI_TIMEOUT: env.OPENAI_TIMEOUT || env.APP_API_TIMEOUT,
OPENAI_API_KEY: env.OPENAI_API_KEY || null,
OPENAI_BASE_URL: env.OPENAI_BASE_URL || 'https://api.openai.com',
OPENAI_BASE_URL: env.OPENAI_BASE_URL || 'https://api.openai.com/v1',
OPENAI_COMPLETION_MODEL: env.OPENAI_COMPLETION_MODEL || 'gpt-3.5-turbo',
OPENAI_COMPLETION_TEMPERATURE: Number(env.OPENAI_COMPLETION_TEMPERATURE) || 1,
OPENAI_COMPLETION_MAX_TOKENS: Number(env.OPENAI_COMPLETION_MAX_TOKENS) || 64,
OPENAI_COMPLETION_FREQUENCY_PENALTY: Number(env.OPENAI_COMPLETION_FREQUENCY_PENALTY) || 0,
OPENAI_COMPLETION_PRESENCE_PENALTY: Number(env.OPENAI_COMPLETION_PRESENCE_PENALTY) || 0.6,
OPENAI_COMPLETION_STOP_SEQUENCES: env.OPENAI_COMPLETION_STOP_SEQUENCES ? String(env.OPENAI_COMPLETION_STOP_SEQUENCES).split(',') : [' assistant:', ' user:'],
OPENAI_IMAGE_GENERATION_SIZE: env.OPENAI_IMAGE_GENERATION_SIZE || '256x256',
OPENAI_IMAGE_GENERATION_MODEL: env.OPENAI_IMAGE_GENERATION_MODEL || 'dall-e-2',
OPENAI_IMAGE_GENERATION_QUALITY: env.OPENAI_IMAGE_GENERATION_QUALITY || 'standard',
LINE_TIMEOUT: env.LINE_TIMEOUT || env.APP_API_TIMEOUT,
LINE_CHANNEL_ACCESS_TOKEN: env.LINE_CHANNEL_ACCESS_TOKEN || null,
LINE_CHANNEL_SECRET: env.LINE_CHANNEL_SECRET || null,
SERPAPI_TIMEOUT: env.SERPAPI_TIMEOUT || env.APP_API_TIMEOUT,
SERPAPI_API_KEY: env.SERPAPI_API_KEY || null,
SERPAPI_LOCATION: env.SERPAPI_LOCATION || 'tw',
PROVIDER_BASE_URL: env.PROVIDER_BASE_URL || 'https://api.openai.com/v1',
PROVIDER_BASE_TOKEN: env.PROVIDER_BASE_TOKEN || null,
PROVIDER_BASE_MODEL: env.PROVIDER_BASE_MODEL || 'gpt-3.5-turbo',
});

export default config;
52 changes: 39 additions & 13 deletions services/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@ export const MODEL_GPT_3_5_TURBO = 'gpt-3.5-turbo';
export const MODEL_GPT_4 = 'gpt-4';
export const MODEL_WHISPER_1 = 'whisper-1';

const BASE_URL = config.PROVIDER_BASE_URL;

const client = axios.create({
baseURL: config.OPENAI_BASE_URL,
timeout: config.OPENAI_TIMEOUT,
headers: {
'Provieder': '',
'Accept-Encoding': 'gzip, deflate, compress',
"HTTP-Referer": `https://line.me`, // Optional, for including your app on openrouter.ai rankings.
"X-Title": `LINE Chatbot`, // Optional, for including your app on openrouter.ai rankings.
},
});

client.interceptors.request.use((c) => {
c.headers.Authorization = `Bearer ${config.OPENAI_API_KEY}`;
if (c.headers.Provieder === 'openai') {
c.headers.Authorization = `Bearer ${config.OPENAI_API_KEY}`;

} else {
c.headers.Authorization = `Bearer ${config.PROVIDER_BASE_TOKEN}`;

}
return handleRequest(c);
});

Expand All @@ -39,13 +49,13 @@ client.interceptors.response.use(handleFulfilled, (err) => {
});

const createChatCompletion = ({
model = config.OPENAI_COMPLETION_MODEL,
model = config.PROVIDER_BASE_MODEL,
messages,
temperature = config.OPENAI_COMPLETION_TEMPERATURE,
maxTokens = config.OPENAI_COMPLETION_MAX_TOKENS,
frequencyPenalty = config.OPENAI_COMPLETION_FREQUENCY_PENALTY,
presencePenalty = config.OPENAI_COMPLETION_PRESENCE_PENALTY,
}) => client.post('/v1/chat/completions', {
}) => client.post(BASE_URL + '/chat/completions', {
model,
messages,
temperature,
Expand All @@ -55,14 +65,14 @@ const createChatCompletion = ({
});

const createTextCompletion = ({
model = config.OPENAI_COMPLETION_MODEL,
model = config.PROVIDER_BASE_MODEL,
prompt,
temperature = config.OPENAI_COMPLETION_TEMPERATURE,
maxTokens = config.OPENAI_COMPLETION_MAX_TOKENS,
frequencyPenalty = config.OPENAI_COMPLETION_FREQUENCY_PENALTY,
presencePenalty = config.OPENAI_COMPLETION_PRESENCE_PENALTY,
stop = config.OPENAI_COMPLETION_STOP_SEQUENCES,
}) => client.post('/v1/completions', {
}) => client.post(BASE_URL + '/completions', {
model,
prompt,
temperature,
Expand All @@ -76,11 +86,25 @@ const createImage = ({
prompt,
n = 1,
size = IMAGE_SIZE_256,
}) => client.post('/v1/images/generations', {
prompt,
n,
size,
});
}) => {

// DALL-E 3 only supports 1024x1024 images.
if (config.OPENAI_IMAGE_GENERATION_MODEL === 'dall-e-3' && (size === IMAGE_SIZE_256 || size === IMAGE_SIZE_512)) {
size = IMAGE_SIZE_1024;
}

return client.post(config.OPENAI_BASE_URL + '/images/generations', {
"model": config.OPENAI_IMAGE_GENERATION_MODEL,
"quality": config.OPENAI_IMAGE_GENERATION_QUALITY,
prompt,
n,
size,
}, {
headers: {
Provieder: 'openai',
},
})
};

const createAudioTranscriptions = ({
buffer,
Expand All @@ -90,8 +114,10 @@ const createAudioTranscriptions = ({
const formData = new FormData();
formData.append('file', buffer, file);
formData.append('model', model);
return client.post('/v1/audio/transcriptions', formData.getBuffer(), {
headers: formData.getHeaders(),
var headers = formData.getHeaders();
headers['Provieder'] = 'openai';
return client.post(config.OPENAI_BASE_URL + '/audio/transcriptions', formData.getBuffer(), {
headers: headers,
});
};

Expand Down
10 changes: 6 additions & 4 deletions utils/generate-completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class Completion {
}
}

const isChatCompletionModel = (model) => (
String(model).startsWith('ft:gpt')
|| String(model).startsWith('gpt')
);
// const isChatCompletionModel = (model) => (
// String(model).startsWith('ft:gpt')
// || String(model).startsWith('gpt')
// );

const isChatCompletionModel = (model) => (true);

/**
* @param {Object} param
Expand Down