Skip to content

Commit

Permalink
improve moonshot api support (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Mar 22, 2024
1 parent eb88fc2 commit c00b8ff
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async function executeApi(session, port, config) {
port,
session.question,
session,
config.apiKey,
config.moonshotApiKey,
session.modelName,
)
}
Expand Down
22 changes: 7 additions & 15 deletions src/config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ export const poeWebModelKeys = [
'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
* @property {string} value
* @property {string} desc
* @property {string} [keyGenerateUrl]
*/
/**
* @type {Object.<string,Model>}
Expand Down Expand Up @@ -143,17 +141,14 @@ export const Models = {
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,
},
}

Expand Down Expand Up @@ -199,6 +194,7 @@ export const defaultConfig = {

claudeApiKey: '',
chatglmApiKey: '',
moonshotApiKey: '',

customApiKey: '',

Expand Down Expand Up @@ -309,11 +305,10 @@ export function getNavigatorLanguage() {
return navigator.language.substring(0, 2)
}

export function isUsingApiKey(configOrSession) {
export function isUsingOpenAiApiKey(configOrSession) {
return (
gptApiModelKeys.includes(configOrSession.modelName) ||
chatgptApiModelKeys.includes(configOrSession.modelName) ||
moonshotApiModelKeys.includes(configOrSession.modelName)
chatgptApiModelKeys.includes(configOrSession.modelName)
)
}

Expand All @@ -329,6 +324,10 @@ export function isUsingChatGLMApi(configOrSession) {
return chatglmApiModelKeys.includes(configOrSession.modelName)
}

export function isUsingMoonshotApi(configOrSession) {
return moonshotApiModelKeys.includes(configOrSession.modelName)
}

export function isUsingCustomNameOnlyModel(configOrSession) {
return configOrSession.modelName === 'poeAiWebCustom'
}
Expand All @@ -344,13 +343,6 @@ export function isUsingGithubThirdPartyApi(configOrSession) {
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
}

export function isSupportBalance(configOrSession) {
return (
gptApiModelKeys.includes(configOrSession.modelName) ||
chatgptApiModelKeys.includes(configOrSession.modelName)
)
}

export async function getPreferredLanguageKey() {
const config = await getUserConfig()
if (config.preferredLanguage === 'auto') return config.userLanguage
Expand Down
68 changes: 41 additions & 27 deletions src/popup/sections/GeneralPart.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useTranslation } from 'react-i18next'
import { useMemo, useState } from 'react'
import { useState } from 'react'
import { openUrl } from '../../utils/index.mjs'
import {
isSupportBalance,
isUsingApiKey,
isUsingOpenAiApiKey,
isUsingAzureOpenAi,
isUsingChatGLMApi,
isUsingClaude2Api,
Expand All @@ -15,6 +14,7 @@ import {
Models,
ThemeMode,
TriggerMode,
isUsingMoonshotApi,
} from '../../config/index.mjs'
import Browser from 'webextension-polyfill'
import { languageList } from '../../config/language.mjs'
Expand Down Expand Up @@ -82,10 +82,6 @@ async function checkBilling(apiKey, apiUrl) {
export function GeneralPart({ config, updateConfig }) {
const { t, i18n } = useTranslation()
const [balance, setBalance] = useState(null)
const [currentModel, setCurrentModel] = useState(null)
const showBalance = useMemo(() => {
return isSupportBalance(config)
}, [config])

const getBalance = async () => {
const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, {
Expand Down Expand Up @@ -145,20 +141,20 @@ export function GeneralPart({ config, updateConfig }) {
<span style="display: flex; gap: 15px;">
<select
style={
isUsingApiKey(config) ||
isUsingOpenAiApiKey(config) ||
isUsingMultiModeModel(config) ||
isUsingCustomModel(config) ||
isUsingAzureOpenAi(config) ||
isUsingClaude2Api(config) ||
isUsingCustomNameOnlyModel(config)
isUsingCustomNameOnlyModel(config) ||
isUsingMoonshotApi(config)
? 'width: 50%;'
: undefined
}
required
onChange={(e) => {
const modelName = e.target.value
updateConfig({ modelName: modelName })
setCurrentModel(Models[modelName])
}}
>
{config.activeApiModes.map((modelName) => {
Expand Down Expand Up @@ -200,7 +196,7 @@ export function GeneralPart({ config, updateConfig }) {
})}
</select>
)}
{isUsingApiKey(config) && (
{isUsingOpenAiApiKey(config) && (
<span style="width: 50%; display: flex; gap: 5px;">
<input
type="password"
Expand All @@ -213,29 +209,23 @@ export function GeneralPart({ config, updateConfig }) {
/>
{config.apiKey.length === 0 ? (
<a
href={
currentModel && 'keyGenerateUrl' in currentModel
? currentModel.keyGenerateUrl
: 'https://platform.openai.com/account/api-keys'
}
href="https://platform.openai.com/account/api-keys"
target="_blank"
rel="nofollow noopener noreferrer"
>
<button style="white-space: nowrap;" type="button">
{t('Get')}
</button>
</a>
) : showBalance ? (
balance ? (
<button type="button" onClick={getBalance}>
{balance}
</button>
) : (
<button type="button" onClick={getBalance}>
{t('Balance')}
</button>
)
) : null}
) : balance ? (
<button type="button" onClick={getBalance}>
{balance}
</button>
) : (
<button type="button" onClick={getBalance}>
{t('Balance')}
</button>
)}
</span>
)}
{isUsingCustomModel(config) && (
Expand Down Expand Up @@ -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>
{isUsingCustomModel(config) && (
<input
Expand Down

0 comments on commit c00b8ff

Please sign in to comment.