Skip to content

Commit

Permalink
feat: custom api url
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 15, 2023
1 parent 118b47c commit f67f861
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 219 deletions.
8 changes: 5 additions & 3 deletions src/background/apis/chatgpt-web.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { fetchSSE } from '../../utils/fetch-sse'
import { isEmpty } from 'lodash-es'
import { chatgptWebModelKeys, Models } from '../../config'
import { chatgptWebModelKeys, getUserConfig, Models } from '../../config'

async function request(token, method, path, data) {
const response = await fetch(`https://chat.openai.com/backend-api${path}`, {
const apiUrl = (await getUserConfig()).customChatGptWebApiUrl
const response = await fetch(`${apiUrl}/backend-api${path}`, {
method,
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -59,9 +60,10 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
})

const models = await getModels(accessToken).catch(() => {})
const config = await getUserConfig()

let answer = ''
await fetchSSE('https://chat.openai.com/backend-api/conversation', {
await fetchSSE(`${config.customChatGptWebApiUrl}${config.customChatGptWebApiPath}`, {
method: 'POST',
signal: controller.signal,
headers: {
Expand Down
8 changes: 5 additions & 3 deletions src/background/apis/openai-api.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// api version

import { maxResponseTokenLength, Models } from '../../config'
import { maxResponseTokenLength, Models, getUserConfig } from '../../config'
import { fetchSSE } from '../../utils/fetch-sse'
import { getConversationPairs } from '../../utils/get-conversation-pairs'
import { isEmpty } from 'lodash-es'
Expand Down Expand Up @@ -44,9 +44,10 @@ export async function generateAnswersWithGptCompletionApi(
(await getGptPromptBase()) +
getConversationPairs(session.conversationRecords, false) +
`Human:${question}\nAI:`
const apiUrl = (await getUserConfig()).customOpenAiApiUrl

let answer = ''
await fetchSSE('https://api.openai.com/v1/completions', {
await fetchSSE(`${apiUrl}/v1/completions`, {
method: 'POST',
signal: controller.signal,
headers: {
Expand Down Expand Up @@ -106,9 +107,10 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
const prompt = getConversationPairs(session.conversationRecords, true)
prompt.unshift({ role: 'system', content: await getChatgptPromptBase() })
prompt.push({ role: 'user', content: question })
const apiUrl = (await getUserConfig()).customOpenAiApiUrl

let answer = ''
await fetchSSE('https://api.openai.com/v1/chat/completions', {
await fetchSSE(`${apiUrl}/v1/chat/completions`, {
method: 'POST',
signal: controller.signal,
headers: {
Expand Down
4 changes: 3 additions & 1 deletion src/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export const defaultConfig = {
tokenSavedOn: 0,
preferredLanguage: navigator.language.substring(0, 2),
userLanguage: navigator.language.substring(0, 2), // unchangeable
customApiUrl: '',
customChatGptWebApiUrl: 'https://chat.openai.com',
customChatGptWebApiPath: '/backend-api/conversation',
customOpenAiApiUrl: 'https://api.openai.com',
}

export async function getUserLanguage() {
Expand Down
Loading

0 comments on commit f67f861

Please sign in to comment.