feat: distinguish model vendors and reuse playground keys
This commit is contained in:
1 parent
0a44a0802b
commit
281849ceb8
13 files changed
+222
-46
No files matched your search
@@ -19,6 +19,7 @@ export interface CatalogModel {
|
||||
logicalModelId: string
|
||||
providerId: string
|
||||
modelKey?: string
|
||||
vendorName?: string
|
||||
sourceLabel?: string
|
||||
publicName: string
|
||||
upstreamModel: string
|
||||
@@ -38,6 +39,7 @@ export interface LogicalModel {
|
||||
id: string
|
||||
publicName: string
|
||||
modelKey?: string
|
||||
vendorName?: string
|
||||
type: CatalogModelType
|
||||
mode: CatalogCallMode
|
||||
description: string
|
||||
@@ -298,6 +300,14 @@ class HttpCatalogClient {
|
||||
})
|
||||
}
|
||||
|
||||
async playgroundChatCompletion(keyId: string, input: { model: string; prompt: string; maxTokens?: number }) {
|
||||
return this.request<ChatCompletionResult>(`/api/playground/keys/${encodeURIComponent(keyId)}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: { 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], max_tokens: input.maxTokens || 4096 }),
|
||||
})
|
||||
}
|
||||
|
||||
async streamChat(apiKey: string, input: { model: string; prompt: string; maxTokens: number }, onDelta: (content: string, reasoning: string) => void, signal: AbortSignal) {
|
||||
const response = await fetch(`${this.baseUrl}/v1/chat/completions`, {
|
||||
method: 'POST', credentials: 'include', signal,
|
||||
@@ -313,6 +323,21 @@ class HttpCatalogClient {
|
||||
await readChatStream(response.body, onDelta)
|
||||
}
|
||||
|
||||
async playgroundStreamChat(keyId: string, input: { model: string; prompt: string; maxTokens: number }, onDelta: (content: string, reasoning: string) => void, signal: AbortSignal) {
|
||||
const response = await fetch(`${this.baseUrl}/api/playground/keys/${encodeURIComponent(keyId)}/chat/completions`, {
|
||||
method: 'POST', credentials: 'include', signal,
|
||||
headers: { 'Content-Type': 'application/json', 'X-Moyi-Source': 'playground', 'Idempotency-Key': crypto.randomUUID() },
|
||||
body: JSON.stringify({ model: input.model, messages: [{ role: 'user', content: input.prompt }], stream: true, max_tokens: input.maxTokens }),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.error?.message || `调用失败 (${response.status})`)
|
||||
}
|
||||
if (!response.body || !response.headers.get('content-type')?.includes('text/event-stream')) throw new Error('服务未返回流式响应')
|
||||
const { readChatStream } = await import('./chat-stream')
|
||||
await readChatStream(response.body, onDelta)
|
||||
}
|
||||
|
||||
async createMediaTask(apiKey: string, input: { model: string; prompt: string; mediaType: '3d' | 'video' }) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(input.mediaType === 'video' ? '/v1/video/tasks' : '/v1/3d/tasks', {
|
||||
method: 'POST',
|
||||
@@ -322,6 +347,13 @@ class HttpCatalogClient {
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async playgroundCreateMediaTask(keyId: string, input: { model: string; prompt: string; mediaType: '3d' | 'video' }) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(`/api/playground/keys/${encodeURIComponent(keyId)}/${input.mediaType === 'video' ? 'video' : '3d'}/tasks`, {
|
||||
method: 'POST', headers: { 'X-Moyi-Source': 'playground' }, body: JSON.stringify({ model: input.model, prompt: input.prompt }),
|
||||
})
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async getTask(apiKey: string, taskId: string) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(`/v1/tasks/${encodeURIComponent(taskId)}`, {
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
@@ -329,6 +361,11 @@ class HttpCatalogClient {
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async playgroundGetTask(keyId: string, taskId: string) {
|
||||
const payload = await this.request<{ task: AsyncTaskResult }>(`/api/playground/keys/${encodeURIComponent(keyId)}/tasks/${encodeURIComponent(taskId)}`, { headers: { 'X-Moyi-Source': 'playground' } })
|
||||
return payload.task
|
||||
}
|
||||
|
||||
async listTasks(apiKey: string, limit = 50) {
|
||||
const payload = await this.request<{ tasks: AsyncTaskResult[] }>(`/v1/tasks?limit=${Math.max(1, Math.min(limit, 100))}`, {
|
||||
headers: { Authorization: `Bearer ${apiKey}` },
|
||||
|
||||
Reference in new issue
Block a user