support moonshot

This commit is contained in:
Jialei
2024-02-07 12:05:37 +08:00
committed by josc146
parent 6b494c03b7
commit 0d7d7354d8
5 changed files with 101 additions and 14 deletions
+10
View File
@@ -30,6 +30,7 @@ import {
gptApiModelKeys, gptApiModelKeys,
poeWebModelKeys, poeWebModelKeys,
setUserConfig, setUserConfig,
moonshotApiModelKeys,
} from '../config/index.mjs' } from '../config/index.mjs'
import '../_locales/i18n' import '../_locales/i18n'
import { openUrl } from '../utils/open-url' import { openUrl } from '../utils/open-url'
@@ -44,6 +45,7 @@ import { refreshMenu } from './menus.mjs'
import { registerCommands } from './commands.mjs' import { registerCommands } from './commands.mjs'
import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs' import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs'
import { generateAnswersWithClaudeWebApi } from '../services/apis/claude-web.mjs' import { generateAnswersWithClaudeWebApi } from '../services/apis/claude-web.mjs'
import { generateAnswersWithMoonshotCompletionApi } from '../services/apis/moonshot-api.mjs'
function setPortProxy(port, proxyTabId) { function setPortProxy(port, proxyTabId) {
port.proxy = Browser.tabs.connect(proxyTabId) port.proxy = Browser.tabs.connect(proxyTabId)
@@ -151,6 +153,14 @@ async function executeApi(session, port, config) {
sessionKey, sessionKey,
session.modelName, session.modelName,
) )
} else if (moonshotApiModelKeys.includes(session.modelName)) {
await generateAnswersWithMoonshotCompletionApi(
port,
session.question,
session,
config.apiKey,
session.modelName,
)
} }
} }
+29 -1
View File
@@ -65,11 +65,14 @@ export const poeWebModelKeys = [
'poeAiWeb_Llama_2_13b', 'poeAiWeb_Llama_2_13b',
'poeAiWeb_Llama_2_70b', 'poeAiWeb_Llama_2_70b',
] ]
export const moonshotApiModelKeys = ['moonshot_v1_8k', 'moonshot_v1_32k', 'moonshot_v1_128k']
const moonshotApiKeyGenerateUrl = 'https://platform.moonshot.cn/console/api-keys'
/** /**
* @typedef {object} Model * @typedef {object} Model
* @property {string} value * @property {string} value
* @property {string} desc * @property {string} desc
* @property {string} [keyGenerateUrl]
*/ */
/** /**
* @type {Object.<string,Model>} * @type {Object.<string,Model>}
@@ -136,6 +139,22 @@ export const Models = {
poeAiWebChatGpt: { value: 'chatgpt', desc: 'Poe AI (Web, ChatGPT)' }, poeAiWebChatGpt: { value: 'chatgpt', desc: 'Poe AI (Web, ChatGPT)' },
poeAiWebChatGpt_16k: { value: 'chatgpt-16k', desc: 'Poe AI (Web, ChatGPT-16k)' }, poeAiWebChatGpt_16k: { value: 'chatgpt-16k', desc: 'Poe AI (Web, ChatGPT-16k)' },
poeAiWebCustom: { value: '', desc: 'Poe AI (Web, Custom)' }, poeAiWebCustom: { value: '', desc: 'Poe AI (Web, Custom)' },
moonshot_v1_8k: {
value: 'moonshot-v1-8k',
desc: 'Moonshot (8k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
},
moonshot_v1_32k: {
value: 'moonshot-v1-32k',
desc: 'Moonshot (32k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
},
moonshot_v1_128k: {
value: 'moonshot-v1-128k',
desc: 'Moonshot (128k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
},
} }
for (const modelName in Models) { for (const modelName in Models) {
@@ -293,7 +312,8 @@ export function getNavigatorLanguage() {
export function isUsingApiKey(configOrSession) { export function isUsingApiKey(configOrSession) {
return ( return (
gptApiModelKeys.includes(configOrSession.modelName) || gptApiModelKeys.includes(configOrSession.modelName) ||
chatgptApiModelKeys.includes(configOrSession.modelName) chatgptApiModelKeys.includes(configOrSession.modelName) ||
moonshotApiModelKeys.includes(configOrSession.modelName)
) )
} }
@@ -324,6 +344,14 @@ export function isUsingGithubThirdPartyApi(configOrSession) {
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName) return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
} }
export function isSupportBalance(configOrSession) {
return (
isUsingAzureOpenAi(configOrSession) ||
gptApiModelKeys.includes(configOrSession.modelName) ||
chatgptApiModelKeys.includes(configOrSession.modelName)
)
}
export async function getPreferredLanguageKey() { export async function getPreferredLanguageKey() {
const config = await getUserConfig() const config = await getUserConfig()
if (config.preferredLanguage === 'auto') return config.userLanguage if (config.preferredLanguage === 'auto') return config.userLanguage
+23 -11
View File
@@ -1,7 +1,8 @@
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useState } from 'react' import { useMemo, useState } from 'react'
import { openUrl } from '../../utils/index.mjs' import { openUrl } from '../../utils/index.mjs'
import { import {
isSupportBalance,
isUsingApiKey, isUsingApiKey,
isUsingAzureOpenAi, isUsingAzureOpenAi,
isUsingChatGLMApi, isUsingChatGLMApi,
@@ -81,6 +82,10 @@ async function checkBilling(apiKey, apiUrl) {
export function GeneralPart({ config, updateConfig }) { export function GeneralPart({ config, updateConfig }) {
const { t, i18n } = useTranslation() const { t, i18n } = useTranslation()
const [balance, setBalance] = useState(null) const [balance, setBalance] = useState(null)
const [currentModel, setCurrentModel] = useState(null)
const showBalance = useMemo(() => {
return isSupportBalance(config)
}, [config])
const getBalance = async () => { const getBalance = async () => {
const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, { const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, {
@@ -153,6 +158,7 @@ export function GeneralPart({ config, updateConfig }) {
onChange={(e) => { onChange={(e) => {
const modelName = e.target.value const modelName = e.target.value
updateConfig({ modelName: modelName }) updateConfig({ modelName: modelName })
setCurrentModel(Models[modelName])
}} }}
> >
{config.activeApiModes.map((modelName) => { {config.activeApiModes.map((modelName) => {
@@ -207,7 +213,11 @@ export function GeneralPart({ config, updateConfig }) {
/> />
{config.apiKey.length === 0 ? ( {config.apiKey.length === 0 ? (
<a <a
href="https://platform.openai.com/account/api-keys" href={
'keyGenerateUrl' in currentModel
? currentModel.keyGenerateUrl
: 'https://platform.openai.com/account/api-keys'
}
target="_blank" target="_blank"
rel="nofollow noopener noreferrer" rel="nofollow noopener noreferrer"
> >
@@ -215,15 +225,17 @@ export function GeneralPart({ config, updateConfig }) {
{t('Get')} {t('Get')}
</button> </button>
</a> </a>
) : balance ? ( ) : showBalance ? (
<button type="button" onClick={getBalance}> balance ? (
{balance} <button type="button" onClick={getBalance}>
</button> {balance}
) : ( </button>
<button type="button" onClick={getBalance}> ) : (
{t('Balance')} <button type="button" onClick={getBalance}>
</button> {t('Balance')}
)} </button>
)
) : null}
</span> </span>
)} )}
{isUsingCustomModel(config) && ( {isUsingCustomModel(config) && (
+19
View File
@@ -0,0 +1,19 @@
import { generateAnswersWithChatgptApiCompat } from './openai-api.mjs'
/**
* @param {Browser.Runtime.Port} port
* @param {string} question
* @param {Session} session
* @param {string} apiKey
* @param {string} modelName
*/
export async function generateAnswersWithMoonshotCompletionApi(
port,
question,
session,
apiKey,
modelName,
) {
const baseUrl = 'https://api.moonshot.cn'
return generateAnswersWithChatgptApiCompat(baseUrl, port, question, session, apiKey, modelName)
}
+20 -2
View File
@@ -95,6 +95,25 @@ export async function generateAnswersWithGptCompletionApi(
* @param {string} modelName * @param {string} modelName
*/ */
export async function generateAnswersWithChatgptApi(port, question, session, apiKey, modelName) { export async function generateAnswersWithChatgptApi(port, question, session, apiKey, modelName) {
const config = await getUserConfig()
return generateAnswersWithChatgptApiCompat(
config.customOpenAiApiUrl,
port,
question,
session,
apiKey,
modelName,
)
}
export async function generateAnswersWithChatgptApiCompat(
baseUrl,
port,
question,
session,
apiKey,
modelName,
) {
const { controller, messageListener, disconnectListener } = setAbortController(port) const { controller, messageListener, disconnectListener } = setAbortController(port)
const config = await getUserConfig() const config = await getUserConfig()
@@ -104,10 +123,9 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
) )
prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() }) prompt.unshift({ role: 'system', content: await getChatSystemPromptBase() })
prompt.push({ role: 'user', content: question }) prompt.push({ role: 'user', content: question })
const apiUrl = config.customOpenAiApiUrl
let answer = '' let answer = ''
await fetchSSE(`${apiUrl}/v1/chat/completions`, { await fetchSSE(`${baseUrl}/v1/chat/completions`, {
method: 'POST', method: 'POST',
signal: controller.signal, signal: controller.signal,
headers: { headers: {