Skip to content

Commit 0d7d735

Browse files
jialeicuijosStorer
authored andcommitted
support moonshot
1 parent 6b494c0 commit 0d7d735

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-14
lines changed

src/background/index.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
gptApiModelKeys,
3131
poeWebModelKeys,
3232
setUserConfig,
33+
moonshotApiModelKeys,
3334
} from '../config/index.mjs'
3435
import '../_locales/i18n'
3536
import { openUrl } from '../utils/open-url'
@@ -44,6 +45,7 @@ import { refreshMenu } from './menus.mjs'
4445
import { registerCommands } from './commands.mjs'
4546
import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs'
4647
import { generateAnswersWithClaudeWebApi } from '../services/apis/claude-web.mjs'
48+
import { generateAnswersWithMoonshotCompletionApi } from '../services/apis/moonshot-api.mjs'
4749

4850
function setPortProxy(port, proxyTabId) {
4951
port.proxy = Browser.tabs.connect(proxyTabId)
@@ -151,6 +153,14 @@ async function executeApi(session, port, config) {
151153
sessionKey,
152154
session.modelName,
153155
)
156+
} else if (moonshotApiModelKeys.includes(session.modelName)) {
157+
await generateAnswersWithMoonshotCompletionApi(
158+
port,
159+
session.question,
160+
session,
161+
config.apiKey,
162+
session.modelName,
163+
)
154164
}
155165
}
156166

src/config/index.mjs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ export const poeWebModelKeys = [
6565
'poeAiWeb_Llama_2_13b',
6666
'poeAiWeb_Llama_2_70b',
6767
]
68+
export const moonshotApiModelKeys = ['moonshot_v1_8k', 'moonshot_v1_32k', 'moonshot_v1_128k']
69+
const moonshotApiKeyGenerateUrl = 'https://platform.moonshot.cn/console/api-keys'
6870

6971
/**
7072
* @typedef {object} Model
7173
* @property {string} value
7274
* @property {string} desc
75+
* @property {string} [keyGenerateUrl]
7376
*/
7477
/**
7578
* @type {Object.<string,Model>}
@@ -136,6 +139,22 @@ export const Models = {
136139
poeAiWebChatGpt: { value: 'chatgpt', desc: 'Poe AI (Web, ChatGPT)' },
137140
poeAiWebChatGpt_16k: { value: 'chatgpt-16k', desc: 'Poe AI (Web, ChatGPT-16k)' },
138141
poeAiWebCustom: { value: '', desc: 'Poe AI (Web, Custom)' },
142+
143+
moonshot_v1_8k: {
144+
value: 'moonshot-v1-8k',
145+
desc: 'Moonshot (8k)',
146+
keyGenerateUrl: moonshotApiKeyGenerateUrl,
147+
},
148+
moonshot_v1_32k: {
149+
value: 'moonshot-v1-32k',
150+
desc: 'Moonshot (32k)',
151+
keyGenerateUrl: moonshotApiKeyGenerateUrl,
152+
},
153+
moonshot_v1_128k: {
154+
value: 'moonshot-v1-128k',
155+
desc: 'Moonshot (128k)',
156+
keyGenerateUrl: moonshotApiKeyGenerateUrl,
157+
},
139158
}
140159

141160
for (const modelName in Models) {
@@ -293,7 +312,8 @@ export function getNavigatorLanguage() {
293312
export function isUsingApiKey(configOrSession) {
294313
return (
295314
gptApiModelKeys.includes(configOrSession.modelName) ||
296-
chatgptApiModelKeys.includes(configOrSession.modelName)
315+
chatgptApiModelKeys.includes(configOrSession.modelName) ||
316+
moonshotApiModelKeys.includes(configOrSession.modelName)
297317
)
298318
}
299319

@@ -324,6 +344,14 @@ export function isUsingGithubThirdPartyApi(configOrSession) {
324344
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
325345
}
326346

347+
export function isSupportBalance(configOrSession) {
348+
return (
349+
isUsingAzureOpenAi(configOrSession) ||
350+
gptApiModelKeys.includes(configOrSession.modelName) ||
351+
chatgptApiModelKeys.includes(configOrSession.modelName)
352+
)
353+
}
354+
327355
export async function getPreferredLanguageKey() {
328356
const config = await getUserConfig()
329357
if (config.preferredLanguage === 'auto') return config.userLanguage

src/popup/sections/GeneralPart.jsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useTranslation } from 'react-i18next'
2-
import { useState } from 'react'
2+
import { useMemo, useState } from 'react'
33
import { openUrl } from '../../utils/index.mjs'
44
import {
5+
isSupportBalance,
56
isUsingApiKey,
67
isUsingAzureOpenAi,
78
isUsingChatGLMApi,
@@ -81,6 +82,10 @@ async function checkBilling(apiKey, apiUrl) {
8182
export function GeneralPart({ config, updateConfig }) {
8283
const { t, i18n } = useTranslation()
8384
const [balance, setBalance] = useState(null)
85+
const [currentModel, setCurrentModel] = useState(null)
86+
const showBalance = useMemo(() => {
87+
return isSupportBalance(config)
88+
}, [config])
8489

8590
const getBalance = async () => {
8691
const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, {
@@ -153,6 +158,7 @@ export function GeneralPart({ config, updateConfig }) {
153158
onChange={(e) => {
154159
const modelName = e.target.value
155160
updateConfig({ modelName: modelName })
161+
setCurrentModel(Models[modelName])
156162
}}
157163
>
158164
{config.activeApiModes.map((modelName) => {
@@ -207,23 +213,29 @@ export function GeneralPart({ config, updateConfig }) {
207213
/>
208214
{config.apiKey.length === 0 ? (
209215
<a
210-
href="https://platform.openai.com/account/api-keys"
216+
href={
217+
'keyGenerateUrl' in currentModel
218+
? currentModel.keyGenerateUrl
219+
: 'https://platform.openai.com/account/api-keys'
220+
}
211221
target="_blank"
212222
rel="nofollow noopener noreferrer"
213223
>
214224
<button style="white-space: nowrap;" type="button">
215225
{t('Get')}
216226
</button>
217227
</a>
218-
) : balance ? (
219-
<button type="button" onClick={getBalance}>
220-
{balance}
221-
</button>
222-
) : (
223-
<button type="button" onClick={getBalance}>
224-
{t('Balance')}
225-
</button>
226-
)}
228+
) : showBalance ? (
229+
balance ? (
230+
<button type="button" onClick={getBalance}>
231+
{balance}
232+
</button>
233+
) : (
234+
<button type="button" onClick={getBalance}>
235+
{t('Balance')}
236+
</button>
237+
)
238+
) : null}
227239
</span>
228240
)}
229241
{isUsingCustomModel(config) && (

src/services/apis/moonshot-api.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { generateAnswersWithChatgptApiCompat } from './openai-api.mjs'
2+
3+
/**
4+
* @param {Browser.Runtime.Port} port
5+
* @param {string} question
6+
* @param {Session} session
7+
* @param {string} apiKey
8+
* @param {string} modelName
9+
*/
10+
export async function generateAnswersWithMoonshotCompletionApi(
11+
port,
12+
question,
13+
session,
14+
apiKey,
15+
modelName,
16+
) {
17+
const baseUrl = 'https://api.moonshot.cn'
18+
return generateAnswersWithChatgptApiCompat(baseUrl, port, question, session, apiKey, modelName)
19+
}

src/services/apis/openai-api.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,25 @@ export async function generateAnswersWithGptCompletionApi(
9595
* @param {string} modelName
9696
*/
9797
export async function generateAnswersWithChatgptApi(port, question, session, apiKey, modelName) {
98+
const config = await getUserConfig()
99+
return generateAnswersWithChatgptApiCompat(
100+
config.customOpenAiApiUrl,
101+
port,
102+
question,
103+
session,
104+
apiKey,
105+
modelName,
106+
)
107+
}
108+
109+
export async function generateAnswersWithChatgptApiCompat(
110+
baseUrl,
111+
port,
112+
question,
113+
session,
114+
apiKey,
115+
modelName,
116+
) {
98117
const { controller, messageListener, disconnectListener } = setAbortController(port)
99118

100119
const config = await getUserConfig()
@@ -104,10 +123,9 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
104123
)
105124
prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() })
106125
prompt.push({ role: 'user', content: question })
107-
const apiUrl = config.customOpenAiApiUrl
108126

109127
let answer = ''
110-
await fetchSSE(`${apiUrl}/v1/chat/completions`, {
128+
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
111129
method: 'POST',
112130
signal: controller.signal,
113131
headers: {

0 commit comments

Comments
 (0)