1 line
6.2 KiB
JSON
1 line
6.2 KiB
JSON
{"repo": "chatlunalab/chatluna", "files": {"packages/adapter-hunyuan/src/requester.ts": {"214237d96380a690fb7a7ecfa8428fc8791ac1c2": {"t": 1785768895.7309916, "n": 5972, "h": "877c4618fdaaafad", "c": "import { ChatGenerationChunk } from '@langchain/core/outputs'\nimport {\n buildChatCompletionParams,\n createEmbeddings,\n createRequestContext,\n processStreamResponse,\n supportImageInput\n} from '@chatluna/v1-shared-adapter'\nimport { Context } from 'koishi'\nimport {\n EmbeddingsRequester,\n EmbeddingsRequestParams,\n EmbeddingsResult,\n ModelRequester,\n ModelRequestParams\n} from 'koishi-plugin-chatluna/llm-core/platform/api'\nimport {\n ClientConfig,\n ClientConfigPool\n} from 'koishi-plugin-chatluna/llm-core/platform/config'\nimport { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'\nimport {\n ChatLunaError,\n ChatLunaErrorCode\n} from 'koishi-plugin-chatluna/utils/error'\nimport { SSEEvent, sseIterable } from 'koishi-plugin-chatluna/utils/sse'\nimport { Config, logger as pluginLogger } from '.'\nimport { ChatCompletionResponse, HunyuanChatRequest } from './types'\nimport {\n formatToolsToHunyuanTools,\n langchainMessageToHunyuanMessage\n} from './utils'\n\n// eslint-disable-next-line generator-star-spacing\nasync function* validateHunyuanStream(\n iterator: AsyncGenerator<SSEEvent, string, unknown>\n) {\n for await (const event of iterator) {\n const chunk = event.data\n\n if (\n chunk === '[DONE]' ||\n chunk == null ||\n chunk === '' ||\n chunk === 'undefined'\n ) {\n yield event\n continue\n }\n\n try {\n const data = JSON.parse(chunk) as ChatCompletionResponse & {\n Response?: {\n Error?: {\n Code?: string\n Message?: string\n }\n }\n Error?: {\n code?: string\n Code?: string\n message?: string\n Message?: string\n }\n }\n const err = data.Response?.Error ?? data.Error\n\n if (err != null) {\n const code = err.Code ?? err['code'] ?? ''\n const msg = err.Message ?? err['message'] ?? ''\n\n if (\n code.includes('IllegalDetected') ||\n msg.includes('IllegalDetected')\n ) {\n throw new ChatLunaError(\n ChatLunaErrorCode.API_UNSAFE_CONTENT,\n new Error(\n 'Unsafe content detected, please try again.' + chunk\n )\n )\n }\n\n throw new ChatLunaError(\n ChatLunaErrorCode.API_REQUEST_FAILED,\n new Error(\n 'error when calling Hunyuan completion, Result: ' +\n chunk\n )\n )\n }\n } catch (e) {\n if (e instanceof ChatLunaError) {\n throw e\n }\n }\n\n yield event\n }\n\n return ''\n}\n\nexport class HunyuanRequester\n extends ModelRequester<ClientConfig>\n implements EmbeddingsRequester\n{\n constructor(\n ctx: Context,\n _configPool: ClientConfigPool<ClientConfig>,\n public _pluginConfig: Config,\n _plugin: ChatLunaPlugin\n ) {\n super(ctx, _configPool, _pluginConfig, _plugin)\n }\n\n async *completionStreamInternal(\n params: ModelRequestParams\n ): AsyncGenerator<ChatGenerationChunk> {\n const requestContext = createRequestContext(\n this.ctx,\n this._config.value,\n this._pluginConfig,\n this._plugin,\n this\n )\n const isVisionModel = supportImageInput(params.model)\n const request = (await buildChatCompletionParams(\n {\n ...params,\n tools: isVisionModel ? undefined : params.tools\n },\n this._plugin,\n false,\n false\n )) as HunyuanChatRequest\n\n request.messages = await langchainMessageToHunyuanMessage(\n params.input,\n this._plugin,\n params.model\n )\n request.tools =\n params.tools != null && !isVisionModel\n ? formatToolsToHunyuanTools(params.tools)\n : undefined\n request.enable_enhancement = isVisionModel\n ? undefined\n : this._pluginConfig.enableSearch\n\n delete request.stop\n delete request.max_tokens\n delete request.presence_penalty\n delete request.frequency_penalty\n delete request.n\n delete request.prompt_cache_key\n delete request.prompt_cache_retention\n delete request.prediction\n delete request.reasoning_effort\n delete request.response_format\n delete request.safety_identifier\n delete request.service_tier\n delete request.logit_bias\n delete request.stream_options\n\n try {\n const response = await this.post('chat/completions', request, {\n signal: params.signal\n })\n\n yield* processStreamResponse(\n requestContext,\n validateHunyuanStream(sseIterable(response))\n )\n } catch (e) {\n if (e instanceof ChatLunaError) {\n throw e\n }\n\n throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e)\n }\n }\n\n async embeddings(\n params: EmbeddingsRequestParams\n ): Promise<EmbeddingsResult> {\n const requestContext = createRequestContext(\n this.ctx,\n this._config.value,\n this._pluginConfig,\n this._plugin,\n this\n )\n\n return await createEmbeddings(requestContext, params)\n }\n\n concatUrl(url: string): string {\n return 'https://api.hunyuan.cloud.tencent.com/v1/' + url\n }\n\n get logger() {\n return pluginLogger\n }\n}\n"}}}} |