Skip to content

Commit

Permalink
Merge pull request #330 from memochou1993/revert-327-feature/add-othe…
Browse files Browse the repository at this point in the history
…r-ai-provider

Revert "Feature/add other ai provider"
  • Loading branch information
memochou1993 authored May 3, 2024
2 parents 8287028 + 24cbd1a commit cc486f1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 35 deletions.
5 changes: 1 addition & 4 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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/v1',
OPENAI_BASE_URL: env.OPENAI_BASE_URL || 'https://api.openai.com',
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,
Expand All @@ -48,9 +48,6 @@ const config = Object.freeze({
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;
34 changes: 9 additions & 25 deletions services/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,16 @@ 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) => {
if (c.headers.Provieder === 'openai') {
c.headers.Authorization = `Bearer ${config.OPENAI_API_KEY}`;

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

}
c.headers.Authorization = `Bearer ${config.OPENAI_API_KEY}`;
return handleRequest(c);
});

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

const createChatCompletion = ({
model = config.PROVIDER_BASE_MODEL,
model = config.OPENAI_COMPLETION_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(BASE_URL + '/chat/completions', {
}) => client.post('/v1/chat/completions', {
model,
messages,
temperature,
Expand All @@ -65,14 +55,14 @@ const createChatCompletion = ({
});

const createTextCompletion = ({
model = config.PROVIDER_BASE_MODEL,
model = config.OPENAI_COMPLETION_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(BASE_URL + '/completions', {
}) => client.post('/v1/completions', {
model,
prompt,
temperature,
Expand All @@ -86,14 +76,10 @@ const createImage = ({
prompt,
n = 1,
size = IMAGE_SIZE_256,
}) => client.post(config.OPENAI_BASE_URL + '/images/generations', {
}) => client.post('/v1/images/generations', {
prompt,
n,
size,
}, {
headers: {
Provieder: 'openai',
},
});

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

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

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

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

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

0 comments on commit cc486f1

Please sign in to comment.