Skip to content

Commit d308fad

Browse files
committed
feat: better custom model support
1 parent c275152 commit d308fad

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/background/apis/custom-api.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// and it has not yet had a negative impact on maintenance.
66
// If necessary, I will refactor.
77

8-
import { getUserConfig, maxResponseTokenLength, Models } from '../../config/index.mjs'
8+
import { getUserConfig, maxResponseTokenLength } from '../../config/index.mjs'
99
import { fetchSSE } from '../../utils/fetch-sse'
1010
import { getConversationPairs } from '../../utils/get-conversation-pairs'
1111
import { isEmpty } from 'lodash-es'
@@ -52,7 +52,7 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
5252
},
5353
body: JSON.stringify({
5454
messages: prompt,
55-
model: Models[modelName].value,
55+
model: modelName,
5656
stream: true,
5757
max_tokens: maxResponseTokenLength,
5858
}),

src/background/index.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
defaultConfig,
1616
getUserConfig,
1717
gptApiModelKeys,
18+
isUsingCustomModel,
1819
Models,
1920
} from '../config/index.mjs'
2021
import { isSafari } from '../utils/is-safari'
@@ -60,7 +61,10 @@ Browser.runtime.onConnect.addListener((port) => {
6061
if (!session) return
6162
const config = await getUserConfig()
6263
if (!session.modelName) session.modelName = config.modelName
63-
if (!session.aiName) session.aiName = Models[session.modelName].desc
64+
if (!session.aiName)
65+
session.aiName =
66+
Models[session.modelName].desc +
67+
(isUsingCustomModel(config) ? ` (${config.customModelName})` : '')
6468
port.postMessage({ session })
6569

6670
try {
@@ -101,8 +105,8 @@ Browser.runtime.onConnect.addListener((port) => {
101105
port,
102106
session.question,
103107
session,
104-
config.apiKey,
105-
session.modelName,
108+
'',
109+
config.customModelName,
106110
)
107111
}
108112
} catch (err) {

src/config/index.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export const Models = {
1818
chatgptApi4_8k: { value: 'gpt-4', desc: 'ChatGPT (GPT-4-8k)' },
1919
chatgptApi4_32k: { value: 'gpt-4-32k', desc: 'ChatGPT (GPT-4-32k)' },
2020
gptApiDavinci: { value: 'text-davinci-003', desc: 'GPT-3.5' },
21-
chatglm6bInt4: { value: 'chatglm-6b-int4', desc: 'ChatGLM-6B-Int4' },
21+
customModel: { value: '', desc: 'Custom Model' },
2222
}
2323

2424
export const chatgptWebModelKeys = ['chatgptFree35', 'chatgptPlus4']
2525
export const bingWebModelKeys = ['bingFree4']
2626
export const gptApiModelKeys = ['gptApiDavinci']
2727
export const chatgptApiModelKeys = ['chatgptApi35', 'chatgptApi4_8k', 'chatgptApi4_32k']
28-
export const customApiModelKeys = ['chatglm6bInt4']
28+
export const customApiModelKeys = ['customModel']
2929

3030
export const TriggerMode = {
3131
always: 'Always',
@@ -66,13 +66,14 @@ export const defaultConfig = {
6666
preferredLanguage: navigator.language.substring(0, 2),
6767
insertAtTop: isMobile(),
6868
lockWhenAnswer: false,
69+
customModelApiUrl: 'http://localhost:8000/chat/completions',
70+
customModelName: 'chatglm-6b-int4',
6971

7072
// advanced
7173

7274
customChatGptWebApiUrl: 'https://chat.openai.com',
7375
customChatGptWebApiPath: '/backend-api/conversation',
7476
customOpenAiApiUrl: 'https://api.openai.com',
75-
customModelApiUrl: 'http://localhost:8000/chat/completions',
7677
siteRegex: 'match nothing',
7778
userSiteRegexOnly: false,
7879
inputQuery: '',

src/popup/Popup.jsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ function GeneralPart({ config, updateConfig }) {
8181
<span style="display: flex; gap: 15px;">
8282
<select
8383
style={
84-
isUsingApiKey(config) || isUsingMultiModeModel(config) ? 'width: 50%;' : undefined
84+
isUsingApiKey(config) || isUsingMultiModeModel(config) || isUsingCustomModel(config)
85+
? 'width: 50%;'
86+
: undefined
8587
}
8688
required
8789
onChange={(e) => {
@@ -146,6 +148,19 @@ function GeneralPart({ config, updateConfig }) {
146148
)}
147149
</span>
148150
)}
151+
{isUsingCustomModel(config) && (
152+
<span style="width: 50%; display: flex; gap: 5px;">
153+
<input
154+
type="text"
155+
value={config.customModelName}
156+
placeholder="Model Name"
157+
onChange={(e) => {
158+
const customModelName = e.target.value
159+
updateConfig({ customModelName: customModelName })
160+
}}
161+
/>
162+
</span>
163+
)}
149164
</span>
150165
{isUsingCustomModel(config) && (
151166
<input

0 commit comments

Comments
 (0)