Skip to content

Commit

Permalink
feat: 增加 provider 資訊、模型
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakevin committed Apr 25, 2024
1 parent 3e9992c commit 4c84c34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ 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;
26 changes: 17 additions & 9 deletions services/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ 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: {
'Accept-Encoding': 'gzip, deflate, compress',
Expand All @@ -29,7 +30,7 @@ const client = axios.create({
});

client.interceptors.request.use((c) => {
c.headers.Authorization = `Bearer ${config.OPENAI_API_KEY}`;
c.headers.Authorization = `Bearer ${config.PROVIDER_BASE_TOKEN}`;
return handleRequest(c);
});

Expand All @@ -41,13 +42,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('/chat/completions', {
}) => client.post(BASE_URL + '/chat/completions', {
model,
messages,
temperature,
Expand All @@ -57,14 +58,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('/completions', {
}) => client.post(BASE_URL + '/completions', {
model,
prompt,
temperature,
Expand All @@ -78,10 +79,14 @@ const createImage = ({
prompt,
n = 1,
size = IMAGE_SIZE_256,
}) => client.post('/images/generations', {
}) => client.post(config.OPENAI_BASE_URL + '/images/generations', {
prompt,
n,
size,
}, {
headers: {
Authorization: `Bearer ${config.OPENAI_API_KEY}`
},
});

const createAudioTranscriptions = ({
Expand All @@ -92,8 +97,11 @@ const createAudioTranscriptions = ({
const formData = new FormData();
formData.append('file', buffer, file);
formData.append('model', model);
return client.post('/audio/transcriptions', formData.getBuffer(), {
headers: formData.getHeaders(),
var headers = formData.getHeaders();
headers['Authorization'] = `Bearer ${config.OPENAI_API_KEY}`;

return client.post(config.OPENAI_BASE_URL + '/audio/transcriptions', formData.getBuffer(), {
headers: headers,
});
};

Expand Down

0 comments on commit 4c84c34

Please sign in to comment.