Commit 3264b48f by renyizhao

对接api_hub

parent 27d150ad
/**
* 新 NewAPI 模型使用方式 - Profile 配置
*
* 移植自 newapi/web/src/features/pricing/lib/api-example-profiles.ts
* 适配 Vue3 + JavaScript
*
* 负责:
* 1) profile 名称 → 中文标签映射
* 2) profile → 支持的场景列表(basic / image-reference / video-reference)
* 3) profile + endpoint + scenario → 专属请求体
* 4) profile + endpoint + scenario → 专属参数补充
*/
/** 场景类型 */
export const SCENARIO_LABELS = {
basic: '基础示例',
'image-reference': '图片参考',
'video-reference': '视频参考',
}
/** Profile 标签映射(与新 NewAPI 保持一致) */
export const PROFILE_LABELS = {
standard: '基础示例',
'ctyun-h3': 'MiniMax H3',
seedance: 'Seedance',
seedance2: 'Seedance 2.0',
'ctyun-wan': 'Wan / HappyHorse',
'ctyun-wan-image': 'Wan / HappyHorse I2V',
'ctyun-wan3': 'Wan 3.0',
'ctyun-wan-reference': 'Wan 2.7 R2V',
'ctyun-happyhorse-reference': 'HappyHorse R2V',
'ctyun-seedream': 'Seedream',
'ctyun-seedream-pro': 'Seedream 5.0 pro',
'ctyun-message-image': 'Qwen / Wan Image',
'ctyun-message-image-edit': 'Qwen / Wan Image Edit',
'ctyun-qwen-rerank': 'Qwen Rerank',
'ctyun-gte-rerank': 'GTE Rerank',
'ctyun-vl-embedding': 'Qwen VL Embedding',
}
/**
* 合并同 endpoint 的多个 api_examples 条目(按 profile 去重,providers/groups 取并集)
*/
export function mergeAPIExamples(examples, endpoint) {
const merged = new Map()
for (const example of examples || []) {
if (example.endpoint !== endpoint) continue
const previous = merged.get(example.profile)
merged.set(example.profile, {
...example,
providers: [
...new Set([...(previous?.providers ?? []), ...(example.providers ?? [])]),
].sort(),
groups: [
...new Set([...(previous?.groups ?? []), ...(example.groups ?? [])]),
].sort(),
})
}
return [...merged.values()]
}
/**
* 返回某个 profile 支持的场景列表
*/
export function exampleScenarios(profile) {
if (['ctyun-wan-reference'].includes(profile)) {
return ['image-reference', 'video-reference']
}
if (
[
'ctyun-happyhorse-reference',
'ctyun-wan-image',
'ctyun-message-image-edit',
].includes(profile)
) {
return ['image-reference']
}
if (['ctyun-h3', 'seedance2', 'ctyun-wan3'].includes(profile)) {
return ['basic', 'image-reference', 'video-reference']
}
if (
[
'seedance',
'ctyun-seedream',
'ctyun-seedream-pro',
'ctyun-message-image',
].includes(profile)
) {
return ['basic', 'image-reference']
}
return ['basic']
}
/**
* 根据 (model, endpoint, profile, scenario) 生成专属请求体
* 如果不需要专属请求体(例如 standard),返回 undefined,由默认 builder 处理
*
* @returns {object|undefined}
*/
export function profileRequest(model, endpoint, profile, scenario) {
if (!PROFILE_LABELS[profile] || profile === 'standard') return undefined
if (endpoint === 'openai-video') {
const wan = profile.includes('wan') || profile.includes('happyhorse')
const body = {
model,
prompt: 'A calm lake at sunset.',
seconds: '5',
resolution: profile === 'ctyun-h3' ? '768P' : '1080P',
}
const seedance = profile.startsWith('seedance')
if (seedance) {
delete body.resolution
body.metadata = { resolution: '1080p' }
}
if (profile === 'ctyun-h3') body.ratio = '16:9'
if (scenario !== 'basic') {
const video = scenario === 'video-reference'
const url = video
? 'https://example.com/reference.mp4'
: 'https://example.com/reference.jpg'
if (wan) {
if (profile === 'ctyun-wan-image') {
body.image = url
} else {
body.metadata = {
input: {
media: [
{ type: video ? 'reference_video' : 'reference_image', url },
],
},
}
}
} else {
const type = video ? 'video_url' : 'image_url'
body.metadata = {
...(seedance ? { resolution: '1080p' } : {}),
content: [
{
type,
[type]: { url },
role: video ? 'reference_video' : 'first_frame',
},
],
}
}
}
return body
}
if (endpoint === 'image-generation') {
return {
model,
prompt: 'A calm lake at sunset.',
size: profile.includes('seedream') ? '2K' : '1024x1024',
n: 1,
stream: false,
...(scenario === 'image-reference'
? { image: 'https://example.com/reference.jpg' }
: {}),
}
}
if (endpoint === 'jina-rerank') {
return {
model,
query: 'What is the capital of China?',
documents: [
'Beijing is the capital of China.',
'Shanghai is a major city.',
],
top_n: 1,
...(profile === 'ctyun-qwen-rerank'
? { instruct: 'Rank documents by relevance to the query.' }
: { return_documents: true }),
}
}
if (endpoint === 'embeddings' && profile === 'ctyun-vl-embedding') {
return {
model,
input: {
contents: [
{ text: 'A calm lake.' },
{ image: 'https://example.com/reference.jpg' },
],
},
encoding_format: 'float',
dimensions: 1024,
}
}
return undefined
}
/**
* 在基础参数表上叠加 profile 专属参数
*
* @param {Array} base - 基础参数列表(来自 buildSupportedParameters)
* @param {string} endpoint
* @param {string} profile
* @param {string} scenario
* @returns {Array}
*/
export function profileParameters(base, endpoint, profile, scenario) {
if (!PROFILE_LABELS[profile] || profile === 'standard') return base
let params = base.map((p) => ({ ...p }))
if (endpoint === 'openai-video') {
params = params.filter((p) => p.name !== 'size')
params.push({
name: profile.startsWith('seedance')
? 'metadata.resolution'
: 'resolution',
type: 'enum',
enumValues:
profile === 'ctyun-h3'
? ['480P', '768P', '2K']
: ['480P', '720P', '1080P'],
descriptionKey: '输出分辨率',
})
if (profile === 'ctyun-h3') {
params.push({
name: 'ratio',
type: 'string',
descriptionKey: '输出宽高比。H3 帧输入自适应;纯文本输入需显式指定 ratio。',
})
}
if (scenario !== 'basic') {
const wan = profile.includes('wan') || profile.includes('happyhorse')
if (profile === 'ctyun-wan-image') {
params.push({
name: 'image',
type: 'string',
required: true,
descriptionKey: '输入图片 URL 或图片 URL 数组,用于图片编辑。',
})
} else {
params.push({
name: wan ? 'metadata.input.media' : 'metadata.content',
type: 'array',
required: true,
descriptionKey: wan
? '高级视频输入:input.media 包含类型化媒体 URL;不要混用帧输入与参考输入。'
: '高级视频输入:content 包含 text、image_url、video_url 或 audio_url 项,可指定 role。',
})
}
}
}
if (endpoint === 'image-generation') {
if (scenario === 'image-reference') {
params.push({
name: 'image',
type: 'string',
required: true,
descriptionKey: '输入图片 URL 或图片 URL 数组,用于图片编辑。',
})
}
params.push({
name: 'stream',
type: 'boolean',
defaultValue: false,
descriptionKey: '此适配器中 Qwen/万相图像和 Seedream 5.0 pro 必须使用 stream=false。',
})
const size = params.find((p) => p.name === 'size')
if (size) {
size.descriptionKey = profile.includes('seedream')
? '使用模型支持的图像尺寸,例如 Seedream 推荐 2K。'
: '使用 WIDTHxHEIGHT(每边 512~2048);Wan 同时支持 1K、2K、4K。原生参数 size 使用 WIDTH*HEIGHT。'
}
if (profile.includes('message-image')) {
params.push({
name: 'parameters',
type: 'object',
descriptionKey: '原生图像参数覆盖顶层的 size 和 watermark;需要时可在此填 size、n、seed。',
})
}
}
if (endpoint === 'jina-rerank') {
params.push(
profile === 'ctyun-qwen-rerank'
? {
name: 'instruct',
type: 'string',
descriptionKey: '可选的重排序指令',
}
: {
name: 'return_documents',
type: 'boolean',
descriptionKey: '是否在重排结果中包含文档原文',
}
)
}
if (profile === 'ctyun-vl-embedding') {
params = params.map((p) =>
p.name === 'input'
? {
...p,
type: 'object',
descriptionKey:
'使用 input.contents;每项恰好包含一个非空的 text、image 或 video 字符串。',
}
: p
)
const encoding = params.find((p) => p.name === 'encoding_format')
if (encoding) encoding.enumValues = ['float']
}
return params
}
\ No newline at end of file
/**
* 新 NewAPI 模型使用方式 - 统一导出
*
* 在 puhui 客户端复用 NewAPI 前端代码模板与参数定义
* 来源:newapi/web/src/features/pricing/lib/
*/
// 代码模板
export { buildSample } from './code-samples.js'
// Profile 配置
export {
SCENARIO_LABELS,
PROFILE_LABELS,
mergeAPIExamples,
exampleScenarios,
profileRequest,
profileParameters,
} from './example-profiles.js'
// 参数定义
export {
COMMON_CHAT_PARAMS,
REASONING_PARAMS,
EMBEDDING_PARAMS,
IMAGE_PARAMS,
VIDEO_PARAMS,
buildSchemaParams,
buildEndpointParameters,
} from './api-parameters.js'
\ No newline at end of file
<template> <template>
<div class="model-card" @click="$emit('click', model)"> <div class="model-card" @click="$emit('click', model)">
<!-- 右上角:按量/按次收费 --> <!-- 右上角:按量/按张/视频分档 -->
<div class="tag-top">{{ billingTagText }}</div> <div class="tag-top">{{ billingTagText }}</div>
<!-- 头部:图标 + 模型名 --> <!-- 头部:图标 + 模型名 -->
...@@ -35,14 +35,18 @@ ...@@ -35,14 +35,18 @@
<!-- 底部:价格展示 --> <!-- 底部:价格展示 -->
<div class="card-footer"> <div class="card-footer">
<div class="price-text"> <div class="price-text">
<!-- 按次计费:单次价格 --> <!-- 视频分档计费:按分辨率档位展示单价 -->
<template v-if="isPerCall"> <template v-if="isPerVideo">
<span>每次调用<b>¥{{ formatPrice(model.inputPrice) }}</b></span> <span>480p<b>¥{{ formatPrice(model.videoPrice480p) }}</b></span>
<span class="divider"></span>
<span>720p<b>¥{{ formatPrice(model.videoPrice720p) }}</b></span>
<span class="divider"></span>
<span>1080p<b>¥{{ formatPrice(model.videoPrice1080p) }}</b></span>
<span class="unit"> / 秒</span>
</template> </template>
<!-- 分档计费:显示价格范围(与 NewAPI 卡片一致:¥X ~ ¥Y / 单位) --> <!-- 按张计费:图片单价 -->
<template v-else-if="hasTieredPrice"> <template v-else-if="isPerImage">
<span>动态价格:<b>{{ formatDynamicRange(model.tiers) }}</b></span> <span>每张图片<b>¥{{ formatPrice(model.imagePrice) }}</b></span>
<span class="unit"> / {{ dynamicPriceUnit }}</span>
</template> </template>
<!-- 按量计费:输入/输出 --> <!-- 按量计费:输入/输出 -->
<template v-else> <template v-else>
...@@ -53,13 +57,15 @@ ...@@ -53,13 +57,15 @@
<span class="unit"> / 1M</span> <span class="unit"> / 1M</span>
</template> </template>
</div> </div>
<!-- 缓存价格另起一行 --> <!-- 缓存价格另起一行(仅按量计费模型有) -->
<div v-if="hasCachePrice" class="price-text cache-price-row"> <div v-if="hasCachePrice" class="price-text cache-price-row">
<span>缓存读取价格:<b>¥{{ formatPrice(model.cacheReadPrice) }}</b></span> <span>缓存读取价格:<b>¥{{ formatPrice(model.cacheReadPrice) }}</b></span>
<span class="unit"> / 1M</span> <span class="unit"> / 1M</span>
<template v-if="model.cacheCreatePrice > 0">
<span class="divider"></span> <span class="divider"></span>
<span>缓存写入价格:<b>¥{{ formatPrice(model.cacheCreatePrice) }}</b></span> <span>缓存写入价格:<b>¥{{ formatPrice(model.cacheCreatePrice) }}</b></span>
<span class="unit"> / 1M</span> <span class="unit"> / 1M</span>
</template>
</div> </div>
</div> </div>
</div> </div>
...@@ -90,49 +96,33 @@ const iconInfo = computed(() => { ...@@ -90,49 +96,33 @@ const iconInfo = computed(() => {
return { name: '', variant: 'Color' } return { name: '', variant: 'Color' }
}) })
// 是否按次计费(quotaType === 1) // 价格类型:metered=按量(token 计费)、per_image=按张、per_video=视频分档
const isPerCall = computed(() => Number(props.model?.quotaType) === 1) const priceType = computed(() => {
if (props.model?.priceType) return props.model.priceType
const quotaType = Number(props.model?.quotaType)
if (quotaType === 2) return 'per_video'
if (quotaType === 1) return 'per_image'
return 'metered'
})
// 是否按张计费(图片模型)
const isPerImage = computed(() => priceType.value === 'per_image')
// 是否视频分档计费
const isPerVideo = computed(() => priceType.value === 'per_video')
// 右上角 tag 文案 // 右上角 tag 文案
const billingTagText = computed(() => { const billingTagText = computed(() => {
if (isPerCall.value) return '按次收费' if (isPerVideo.value) return '视频分档'
if (isTiered.value) return '动态计费' if (isPerImage.value) return '按张收费'
return '按量收费' return '按量收费'
}) })
// 是否分档计费
const isTiered = computed(() => props.model?.billingMode === 'tiered_expr')
// 底部 tag 文案
const priceTagText = computed(() => (isPerCall.value ? '单次价格' : '收费价格'))
// 是否有缓存价格(缓存读取价格和缓存创建价格都大于0才显示) // 是否有缓存价格(缓存读取价格和缓存创建价格都大于0才显示)
const hasCachePrice = computed(() => { const hasCachePrice = computed(() => {
return props.model?.cacheReadPrice > 0 || props.model?.cacheCreatePrice > 0 return props.model?.cacheReadPrice > 0 || props.model?.cacheCreatePrice > 0
}) })
// 是否有分档价格
const hasTieredPrice = computed(() => {
return isTiered.value && props.model?.tiers && props.model.tiers.length > 0
})
// 动态计费单位文案。
// token → 1M(每百万 token,后端 multiplier 已带 /1000000 语义)
// second → 秒
// count → 次
// 其他 / 未配置 → 1M(兜底,与 NewAPI 一致)
const dynamicPriceUnit = computed(() => {
const tiers = props.model?.tiers
if (!Array.isArray(tiers)) return '1M'
const unitTier = tiers.find(t => t && t.tokensMultiplier != null && Number(t.tokensMultiplier) > 0)
if (!unitTier) return '1M'
const unit = unitTier.usageUnit
if (unit === 'second') return '秒'
if (unit === 'count') return '次'
if (unit === 'token') return '1M'
return '1M'
})
// 模型标签列表 // 模型标签列表
const modelTags = computed(() => { const modelTags = computed(() => {
if (!props.model?.tags) return [] if (!props.model?.tags) return []
...@@ -148,35 +138,6 @@ function formatPrice(price) { ...@@ -148,35 +138,6 @@ function formatPrice(price) {
if (Number.isNaN(num)) return '-' if (Number.isNaN(num)) return '-'
return num.toFixed(4).replace(/\.?0+$/, '') return num.toFixed(4).replace(/\.?0+$/, '')
} }
// 动态计费模型汇总价格范围:聚合所有 tiers 中有值的价格字段(按 tokensMultiplier > inputRatio > outputRatio 优先级),
// 返回 { min, max, same }。same=true 表示所有档价格一致,可退化为单值展示。
function getDynamicPriceRange(tiers) {
if (!Array.isArray(tiers) || tiers.length === 0) return null
let min = Infinity
let max = -Infinity
const candidateFields = ['tokensMultiplier', 'inputRatio', 'outputRatio']
for (const tier of tiers) {
if (!tier) continue
for (const field of candidateFields) {
const v = Number(tier[field])
if (Number.isFinite(v) && v > 0) {
if (v < min) min = v
if (v > max) max = v
}
}
}
if (min === Infinity || max === -Infinity) return null
return { min, max, same: min === max }
}
// 渲染动态计费模型的价格范围文案:单档 ¥X,多档 ¥X ~ ¥Y。
function formatDynamicRange(tiers) {
const range = getDynamicPriceRange(tiers)
if (!range) return '¥-'
if (range.same) return `¥${formatPrice(range.min)}`
return `¥${formatPrice(range.min)} ~ ¥${formatPrice(range.max)}`
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
......
...@@ -317,11 +317,9 @@ function loadTokenInfo() { ...@@ -317,11 +317,9 @@ function loadTokenInfo() {
hasToken.value = true; hasToken.value = true;
apiKey.value = response.apiKey; apiKey.value = response.apiKey;
maskedKey.value = maskKey(response.apiKey); maskedKey.value = maskKey(response.apiKey);
// 显示真实 New API 余额 // 显示 api_hub 真实余额;余额查询失败(api_hub 不可达)时展示 "--",不报错
if (response.balance) { balance.value = response.balance || "--";
balance.value = response.balance; // 显示 api_hub 服务地址
}
// 显示 New API 服务地址
if (response.baseUrl) { if (response.baseUrl) {
baseUrl.value = response.baseUrl; baseUrl.value = response.baseUrl;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment