improve moonshot api support (#623)

This commit is contained in:
josc146
2024-03-22 16:55:37 +08:00
parent eb88fc2053
commit c00b8ff9ab
3 changed files with 49 additions and 43 deletions
+1 -1
View File
@@ -158,7 +158,7 @@ async function executeApi(session, port, config) {
port, port,
session.question, session.question,
session, session,
config.apiKey, config.moonshotApiKey,
session.modelName, session.modelName,
) )
} }
+7 -15
View File
@@ -66,13 +66,11 @@ export const poeWebModelKeys = [
'poeAiWeb_Llama_2_70b', 'poeAiWeb_Llama_2_70b',
] ]
export const moonshotApiModelKeys = ['moonshot_v1_8k', 'moonshot_v1_32k', 'moonshot_v1_128k'] 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>}
@@ -143,17 +141,14 @@ export const Models = {
moonshot_v1_8k: { moonshot_v1_8k: {
value: 'moonshot-v1-8k', value: 'moonshot-v1-8k',
desc: 'Moonshot (8k)', desc: 'Moonshot (8k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
}, },
moonshot_v1_32k: { moonshot_v1_32k: {
value: 'moonshot-v1-32k', value: 'moonshot-v1-32k',
desc: 'Moonshot (32k)', desc: 'Moonshot (32k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
}, },
moonshot_v1_128k: { moonshot_v1_128k: {
value: 'moonshot-v1-128k', value: 'moonshot-v1-128k',
desc: 'Moonshot (128k)', desc: 'Moonshot (128k)',
keyGenerateUrl: moonshotApiKeyGenerateUrl,
}, },
} }
@@ -199,6 +194,7 @@ export const defaultConfig = {
claudeApiKey: '', claudeApiKey: '',
chatglmApiKey: '', chatglmApiKey: '',
moonshotApiKey: '',
customApiKey: '', customApiKey: '',
@@ -309,11 +305,10 @@ export function getNavigatorLanguage() {
return navigator.language.substring(0, 2) return navigator.language.substring(0, 2)
} }
export function isUsingApiKey(configOrSession) { export function isUsingOpenAiApiKey(configOrSession) {
return ( return (
gptApiModelKeys.includes(configOrSession.modelName) || gptApiModelKeys.includes(configOrSession.modelName) ||
chatgptApiModelKeys.includes(configOrSession.modelName) || chatgptApiModelKeys.includes(configOrSession.modelName)
moonshotApiModelKeys.includes(configOrSession.modelName)
) )
} }
@@ -329,6 +324,10 @@ export function isUsingChatGLMApi(configOrSession) {
return chatglmApiModelKeys.includes(configOrSession.modelName) return chatglmApiModelKeys.includes(configOrSession.modelName)
} }
export function isUsingMoonshotApi(configOrSession) {
return moonshotApiModelKeys.includes(configOrSession.modelName)
}
export function isUsingCustomNameOnlyModel(configOrSession) { export function isUsingCustomNameOnlyModel(configOrSession) {
return configOrSession.modelName === 'poeAiWebCustom' return configOrSession.modelName === 'poeAiWebCustom'
} }
@@ -344,13 +343,6 @@ export function isUsingGithubThirdPartyApi(configOrSession) {
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName) return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
} }
export function isSupportBalance(configOrSession) {
return (
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
+41 -27
View File
@@ -1,9 +1,8 @@
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useMemo, useState } from 'react' import { useState } from 'react'
import { openUrl } from '../../utils/index.mjs' import { openUrl } from '../../utils/index.mjs'
import { import {
isSupportBalance, isUsingOpenAiApiKey,
isUsingApiKey,
isUsingAzureOpenAi, isUsingAzureOpenAi,
isUsingChatGLMApi, isUsingChatGLMApi,
isUsingClaude2Api, isUsingClaude2Api,
@@ -15,6 +14,7 @@ import {
Models, Models,
ThemeMode, ThemeMode,
TriggerMode, TriggerMode,
isUsingMoonshotApi,
} from '../../config/index.mjs' } from '../../config/index.mjs'
import Browser from 'webextension-polyfill' import Browser from 'webextension-polyfill'
import { languageList } from '../../config/language.mjs' import { languageList } from '../../config/language.mjs'
@@ -82,10 +82,6 @@ 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`, {
@@ -145,12 +141,13 @@ export function GeneralPart({ config, updateConfig }) {
<span style="display: flex; gap: 15px;"> <span style="display: flex; gap: 15px;">
<select <select
style={ style={
isUsingApiKey(config) || isUsingOpenAiApiKey(config) ||
isUsingMultiModeModel(config) || isUsingMultiModeModel(config) ||
isUsingCustomModel(config) || isUsingCustomModel(config) ||
isUsingAzureOpenAi(config) || isUsingAzureOpenAi(config) ||
isUsingClaude2Api(config) || isUsingClaude2Api(config) ||
isUsingCustomNameOnlyModel(config) isUsingCustomNameOnlyModel(config) ||
isUsingMoonshotApi(config)
? 'width: 50%;' ? 'width: 50%;'
: undefined : undefined
} }
@@ -158,7 +155,6 @@ 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) => {
@@ -200,7 +196,7 @@ export function GeneralPart({ config, updateConfig }) {
})} })}
</select> </select>
)} )}
{isUsingApiKey(config) && ( {isUsingOpenAiApiKey(config) && (
<span style="width: 50%; display: flex; gap: 5px;"> <span style="width: 50%; display: flex; gap: 5px;">
<input <input
type="password" type="password"
@@ -213,11 +209,7 @@ export function GeneralPart({ config, updateConfig }) {
/> />
{config.apiKey.length === 0 ? ( {config.apiKey.length === 0 ? (
<a <a
href={ href="https://platform.openai.com/account/api-keys"
currentModel && 'keyGenerateUrl' in currentModel
? currentModel.keyGenerateUrl
: 'https://platform.openai.com/account/api-keys'
}
target="_blank" target="_blank"
rel="nofollow noopener noreferrer" rel="nofollow noopener noreferrer"
> >
@@ -225,17 +217,15 @@ export function GeneralPart({ config, updateConfig }) {
{t('Get')} {t('Get')}
</button> </button>
</a> </a>
) : showBalance ? ( ) : balance ? (
balance ? ( <button type="button" onClick={getBalance}>
<button type="button" onClick={getBalance}> {balance}
{balance} </button>
</button> ) : (
) : ( <button type="button" onClick={getBalance}>
<button type="button" onClick={getBalance}> {t('Balance')}
{t('Balance')} </button>
</button> )}
)
) : null}
</span> </span>
)} )}
{isUsingCustomModel(config) && ( {isUsingCustomModel(config) && (
@@ -298,6 +288,30 @@ export function GeneralPart({ config, updateConfig }) {
}} }}
/> />
)} )}
{isUsingMoonshotApi(config) && (
<span style="width: 50%; display: flex; gap: 5px;">
<input
type="password"
value={config.moonshotApiKey}
placeholder={t('Moonshot API Key')}
onChange={(e) => {
const apiKey = e.target.value
updateConfig({ moonshotApiKey: apiKey })
}}
/>
{config.moonshotApiKey.length === 0 && (
<a
href="https://platform.moonshot.cn/console/api-keys"
target="_blank"
rel="nofollow noopener noreferrer"
>
<button style="white-space: nowrap;" type="button">
{t('Get')}
</button>
</a>
)}
</span>
)}
</span> </span>
{isUsingCustomModel(config) && ( {isUsingCustomModel(config) && (
<input <input