mirror of
https://github.com/Wx-2025/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 09:15:50 +00:00
Compare commits
6 Commits
3ba4e39193
...
40c3d6c735
| Author | SHA1 | Date | |
|---|---|---|---|
| 40c3d6c735 | |||
| 91e85aec94 | |||
| 70224a6b61 | |||
| 53ff9bd307 | |||
| 4c61837b93 | |||
| 1136707f1f |
@@ -1,5 +1,5 @@
|
|||||||
import { getRequestHeaders } from "/script.js";
|
import { getRequestHeaders } from "/script.js";
|
||||||
import { getContext } from "/scripts/extensions.js";
|
import { getContext, extension_settings } from "/scripts/extensions.js";
|
||||||
import { amilyHelper } from '../../../core/tavern-helper/main.js';
|
import { amilyHelper } from '../../../core/tavern-helper/main.js';
|
||||||
import Options from './Options.js';
|
import Options from './Options.js';
|
||||||
import RequestBody from './RequestBody.js';
|
import RequestBody from './RequestBody.js';
|
||||||
@@ -116,41 +116,17 @@ export default class ModelCaller {
|
|||||||
const url = '/api/backends/chat-completions/generate';
|
const url = '/api/backends/chat-completions/generate';
|
||||||
|
|
||||||
// [修复]: 手动合并 Profile 中的关键参数,否则后端不会自动应用预设配置
|
// [修复]: 手动合并 Profile 中的关键参数,否则后端不会自动应用预设配置
|
||||||
// 我们需要模拟 ST 前端发送请求时的行为,把预设参数填进去
|
// 提取逻辑已封装至 _buildProfilePayload
|
||||||
const profilePayload = {
|
const profilePayload = this._buildProfilePayload(targetProfile);
|
||||||
// 基础模型参数
|
|
||||||
model: targetProfile.openai_model || targetProfile.model,
|
|
||||||
temperature: targetProfile.temperature,
|
|
||||||
frequency_penalty: targetProfile.frequency_penalty,
|
|
||||||
presence_penalty: targetProfile.presence_penalty,
|
|
||||||
top_p: targetProfile.top_p,
|
|
||||||
top_k: targetProfile.top_k,
|
|
||||||
min_p: targetProfile.min_p,
|
|
||||||
repetition_penalty: targetProfile.repetition_penalty,
|
|
||||||
|
|
||||||
// 关键:OpenAI 源标记
|
|
||||||
chat_completion_source: targetProfile.chat_completion_source || 'openai',
|
|
||||||
|
|
||||||
// 代理设置 (如果预设里有)
|
|
||||||
reverse_proxy: targetProfile.reverse_proxy,
|
|
||||||
proxy_password: targetProfile.proxy_password,
|
|
||||||
|
|
||||||
// 其他可能影响生成的参数
|
|
||||||
custom_prompt_post_processing: targetProfile.custom_prompt_post_processing ?? 'strict',
|
|
||||||
};
|
|
||||||
|
|
||||||
// 合并顺序:基础Payload(msg) < Profile预设 < 显式Params覆盖
|
// 合并顺序:基础Payload(msg) < Profile预设 < 显式Params覆盖
|
||||||
// toMinimalPayload 包含: messages, stream, max_tokens, ...params
|
// toMinimalPayload 包含: messages, stream, max_tokens, ...params
|
||||||
// 我们需要把 profilePayload 塞在中间,被 params 覆盖
|
|
||||||
const minimal = requestBody.toMinimalPayload();
|
const minimal = requestBody.toMinimalPayload();
|
||||||
|
|
||||||
// 剔除 minimal 中可能已经存在的 undefined 属性,避免覆盖 profile 的有效值
|
|
||||||
// 但实际上 minimal 中的 ...params 是用户强指定的,应该覆盖 profile
|
|
||||||
|
|
||||||
const finalPayload = {
|
const finalPayload = {
|
||||||
...profilePayload,
|
...profilePayload,
|
||||||
...minimal, // 包含 messages, stream, max_tokens
|
...minimal,
|
||||||
...options.params // 再次确保显式参数优先级最高 (minimal里其实已经含了,这里双保险)
|
...options.params
|
||||||
};
|
};
|
||||||
|
|
||||||
const fetchOpts = {
|
const fetchOpts = {
|
||||||
@@ -286,5 +262,62 @@ export default class ModelCaller {
|
|||||||
// Fallback
|
// Fallback
|
||||||
return typeof data === 'object' ? JSON.stringify(data) : data;
|
return typeof data === 'object' ? JSON.stringify(data) : data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 辅助方法:从 Profile 对象中提取标准生成参数
|
||||||
|
* 严格复刻 SillyTavern 原始 Payload 逻辑
|
||||||
|
*/
|
||||||
|
_buildProfilePayload(targetProfile) {
|
||||||
|
const context = getContext();
|
||||||
|
|
||||||
|
// 1. 基础克隆
|
||||||
|
const payload = { ...targetProfile };
|
||||||
|
|
||||||
|
// 2. 注入运行时元数据 (这是旧版能通的关键,包含用户/角色名等)
|
||||||
|
payload.user_name = context.name1 || 'User';
|
||||||
|
payload.char_name = context.name2 || 'AI';
|
||||||
|
payload.group_names = []; // 暂不处理群组
|
||||||
|
payload.use_sysprompt = true;
|
||||||
|
payload.type = 'quiet';
|
||||||
|
payload.custom_prompt_post_processing = payload.custom_prompt_post_processing || 'strict';
|
||||||
|
|
||||||
|
// 3. 规范化模型字段
|
||||||
|
if (!payload.model) {
|
||||||
|
payload.model = payload.openai_model || payload.claude_model || payload.mistral_model || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 精准对齐 URL 映射 (解决 403/400 错误的核心)
|
||||||
|
const rawUrl = payload['api-url'] || payload['api_url'] || payload.custom_url || payload.url;
|
||||||
|
if (rawUrl) {
|
||||||
|
// 如果 Source 是 custom,严格遵循旧版:custom_url 有值,reverse_proxy 为空
|
||||||
|
if (payload.chat_completion_source === 'custom') {
|
||||||
|
payload.custom_url = rawUrl;
|
||||||
|
payload.reverse_proxy = payload.reverse_proxy || '';
|
||||||
|
} else {
|
||||||
|
// 如果是 openai,则填充 reverse_proxy
|
||||||
|
payload.reverse_proxy = rawUrl;
|
||||||
|
payload.custom_url = rawUrl;
|
||||||
|
}
|
||||||
|
// 兼容性修补
|
||||||
|
payload.zai_endpoint = rawUrl;
|
||||||
|
payload.vertexai_region = rawUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. 补全采样参数 (严格对齐 UI 当前状态)
|
||||||
|
const globalGenSettings = extension_settings.text_generation || {};
|
||||||
|
const fields = ['temperature', 'max_tokens', 'top_p', 'top_k', 'min_p', 'frequency_penalty', 'presence_penalty', 'repetition_penalty'];
|
||||||
|
fields.forEach(field => {
|
||||||
|
if (payload[field] === undefined) {
|
||||||
|
payload[field] = globalGenSettings[field] ?? (field === 'temperature' ? 1 : 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 6. 确保 Source 存在且不被错误覆盖
|
||||||
|
if (!payload.chat_completion_source) {
|
||||||
|
payload.chat_completion_source = 'openai';
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ export function getNccsApiSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
// 核心调用入口 (Hybrid Mode: Bus First -> Fallback Legacy)
|
// 核心调用入口 (Legacy Mode with Stream Support)
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
|
|
||||||
export async function callNccsAI(messages, options = {}) {
|
export async function callNccsAI(messages, options = {}) {
|
||||||
@@ -61,160 +61,97 @@ export async function callNccsAI(messages, options = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const settings = getNccsApiSettings();
|
const settings = getNccsApiSettings();
|
||||||
|
const finalOptions = {
|
||||||
|
...settings,
|
||||||
|
...options,
|
||||||
|
// 显式合并流式开关
|
||||||
|
stream: options.useFakeStream ?? settings.useFakeStream ?? false
|
||||||
|
};
|
||||||
|
|
||||||
// 0. 全局开关检查
|
if (finalOptions.apiMode !== 'sillytavern_preset') {
|
||||||
if (settings.nccsEnabled === false) {
|
if (!finalOptions.apiUrl || !finalOptions.model || !finalOptions.apiKey) {
|
||||||
// 暂不阻断,仅作为配置读取,保持兼容性
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. 基础配置确定 (options 覆盖 settings)
|
|
||||||
const activeMode = options.apiMode || settings.apiMode;
|
|
||||||
const activeUrl = options.apiUrl || settings.apiUrl;
|
|
||||||
const activeKey = options.apiKey || settings.apiKey;
|
|
||||||
const activeModel = options.model || settings.model;
|
|
||||||
const activeProfile = options.tavernProfile || settings.tavernProfile;
|
|
||||||
const activeMaxTokens = options.maxTokens ?? settings.maxTokens;
|
|
||||||
const activeTemperature = options.temperature ?? settings.temperature;
|
|
||||||
const activeFakeStream = options.useFakeStream ?? settings.useFakeStream;
|
|
||||||
|
|
||||||
if (activeMode !== 'sillytavern_preset') {
|
|
||||||
if (!activeUrl || !activeModel || !activeKey) {
|
|
||||||
console.warn("[Amily2-Nccs外交部] API配置不完整,无法调用AI");
|
console.warn("[Amily2-Nccs外交部] API配置不完整,无法调用AI");
|
||||||
toastr.error("API配置不完整,请检查URL、Key和模型配置。", "Nccs-外交部");
|
toastr.error("API配置不完整,请检查URL、Key和模型配置。", "Nccs-外交部");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// [兼容性修复] 自动收集 options 中的额外参数到 params,防止 ModelCaller 丢失 top_p 等参数
|
|
||||||
const standardKeys = [
|
|
||||||
'apiMode', 'apiUrl', 'apiKey', 'model',
|
|
||||||
'maxTokens', 'temperature', 'tavernProfile', 'useFakeStream',
|
|
||||||
'params'
|
|
||||||
];
|
|
||||||
const extraParams = {};
|
|
||||||
Object.keys(options).forEach(key => {
|
|
||||||
if (!standardKeys.includes(key)) {
|
|
||||||
extraParams[key] = options[key];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 合并显式的 options.params 和 收集到的 extraParams
|
|
||||||
const finalParams = { ...extraParams, ...(options.params || {}) };
|
|
||||||
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 尝试路径 A: 新版 Amily2Bus ModelCaller (支持 FakeStream)
|
|
||||||
// ============================================================
|
|
||||||
if (nccsCtx && nccsCtx.model) {
|
|
||||||
try {
|
try {
|
||||||
nccsCtx.log('Main', 'info', `[v2] 尝试通过 ModelCaller 调用 (${activeFakeStream ? 'FakeStream' : 'Standard'})...`);
|
|
||||||
|
|
||||||
const builder = nccsCtx.model.Options.builder()
|
|
||||||
.setFakeStream(activeFakeStream)
|
|
||||||
.setMaxTokens(activeMaxTokens)
|
|
||||||
.setTemperature(activeTemperature)
|
|
||||||
.setParams(finalParams);
|
|
||||||
|
|
||||||
if (activeMode === 'sillytavern_preset') {
|
|
||||||
builder.setMode('preset')
|
|
||||||
.setPresetId(activeProfile)
|
|
||||||
.setModel(activeModel);
|
|
||||||
} else {
|
|
||||||
builder.setMode('direct')
|
|
||||||
.setApiUrl(activeUrl)
|
|
||||||
.setApiKey(activeKey)
|
|
||||||
.setModel(activeModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 发起请求
|
|
||||||
const response = await nccsCtx.model.call(messages, builder.build());
|
|
||||||
|
|
||||||
// 校验结果
|
|
||||||
if (response) {
|
|
||||||
nccsCtx.log('Main', 'info', `[v2] ModelCaller 调用成功。`);
|
|
||||||
return response;
|
|
||||||
} else {
|
|
||||||
throw new Error("ModelCaller 返回了空响应");
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (busError) {
|
|
||||||
const errorMsg = `[v2] ModelCaller 调用失败,准备回退到旧版逻辑。原因: ${busError.message}`;
|
|
||||||
// 记录错误但阻断抛出,以便执行下方代码
|
|
||||||
if (nccsCtx) nccsCtx.log('Main', 'warn', errorMsg);
|
|
||||||
else console.warn(errorMsg);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.warn("[Amily2-Nccs] Bus 未连接,直接使用旧版逻辑。");
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================
|
|
||||||
// 尝试路径 B: 旧版 Legacy 方法 (Fallback)
|
|
||||||
// ============================================================
|
|
||||||
// 构建 Legacy 兼容对象
|
|
||||||
const legacyOptions = {
|
|
||||||
apiMode: activeMode,
|
|
||||||
apiUrl: activeUrl,
|
|
||||||
apiKey: activeKey,
|
|
||||||
model: activeModel,
|
|
||||||
tavernProfile: activeProfile,
|
|
||||||
maxTokens: activeMaxTokens,
|
|
||||||
temperature: activeTemperature,
|
|
||||||
useFakeStream: activeFakeStream,
|
|
||||||
...finalParams // 将额外参数直接展平回 legacyOptions 根目录
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.groupCollapsed(`[Amily2-Nccs] 降级使用 Legacy API 调用`);
|
|
||||||
console.log("Fallback Mode Active");
|
|
||||||
|
|
||||||
let responseContent;
|
let responseContent;
|
||||||
|
switch (finalOptions.apiMode) {
|
||||||
switch (activeMode) {
|
|
||||||
case 'openai_test':
|
case 'openai_test':
|
||||||
responseContent = await callNccsOpenAITest(messages, legacyOptions);
|
responseContent = await callNccsOpenAITest(messages, finalOptions);
|
||||||
break;
|
break;
|
||||||
case 'sillytavern_preset':
|
case 'sillytavern_preset':
|
||||||
responseContent = await callNccsSillyTavernPreset(messages, legacyOptions);
|
responseContent = await callNccsSillyTavernPreset(messages, finalOptions);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error(`未支持的 API 模式: ${activeMode}`);
|
console.error(`未支持的 API 模式: ${finalOptions.apiMode}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Legacy Response:", responseContent);
|
|
||||||
console.groupEnd();
|
|
||||||
|
|
||||||
return responseContent;
|
return responseContent;
|
||||||
|
} catch (error) {
|
||||||
} catch (legacyError) {
|
console.error(`[Amily2-Nccs] API 调用失败:`, error);
|
||||||
console.groupEnd();
|
toastr.error(`调用失败: ${error.message}`, "Nccs API Error");
|
||||||
console.error(`[Amily2-Nccs] Legacy API 调用也失败了:`, legacyError);
|
|
||||||
|
|
||||||
// 统一错误提示
|
|
||||||
const msg = legacyError.message;
|
|
||||||
if (msg.includes('401')) toastr.error("API认证失败 (401)", "Nccs API Error");
|
|
||||||
else if (msg.includes('403')) toastr.error("权限拒绝 (403)", "Nccs API Error");
|
|
||||||
else if (msg.includes('500')) toastr.error("服务器错误 (500)", "Nccs API Error");
|
|
||||||
else toastr.error(`调用失败: ${msg}`, "Nccs API Error");
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchFakeStream(url, opts) {
|
||||||
|
const res = await fetch(url, opts);
|
||||||
|
if (!res.ok) throw new Error(`Stream HTTP ${res.status}: ${await res.text()}`);
|
||||||
|
|
||||||
|
const reader = res.body.getReader();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
let fullContent = "";
|
||||||
|
let buffer = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
|
||||||
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
const lines = buffer.split('\n');
|
||||||
|
buffer = lines.pop();
|
||||||
|
|
||||||
|
for (const line of lines) {
|
||||||
|
const trimmed = line.trim();
|
||||||
|
if (!trimmed || trimmed === 'data: [DONE]') continue;
|
||||||
|
if (trimmed.startsWith('data: ')) {
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(trimmed.substring(6));
|
||||||
|
const delta = json.choices?.[0]?.delta?.content;
|
||||||
|
if (delta) fullContent += delta;
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[NccsApi] SSE Parse Error:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
reader.releaseLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fullContent && buffer) {
|
||||||
|
try { return JSON.parse(buffer).choices?.[0]?.message?.content || buffer; }
|
||||||
|
catch { return buffer; }
|
||||||
|
}
|
||||||
|
return fullContent;
|
||||||
|
}
|
||||||
|
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
// Legacy Implementations (保留旧代码以供降级使用)
|
// Legacy Implementations (直接增加流式适配)
|
||||||
// =================================================================================================
|
// =================================================================================================
|
||||||
|
|
||||||
function normalizeApiResponse(responseData) {
|
function normalizeApiResponse(responseData) {
|
||||||
let data = responseData;
|
let data = responseData;
|
||||||
if (typeof data === 'string') {
|
if (typeof data === 'string') {
|
||||||
try { data = JSON.parse(data); } catch (e) { return { error: { message: 'Invalid JSON' } }; }
|
try { data = JSON.parse(data); } catch (e) { return data; }
|
||||||
}
|
}
|
||||||
if (data?.data?.data) data = data.data; // Unpack nested data
|
if (data?.choices?.[0]?.message?.content) return data.choices[0].message.content.trim();
|
||||||
if (data?.choices?.[0]?.message?.content) return { content: data.choices[0].message.content.trim() };
|
if (data?.content) return data.content.trim();
|
||||||
if (data?.content) return { content: data.content.trim() };
|
return typeof data === 'object' ? JSON.stringify(data) : data;
|
||||||
if (data?.data) return { data: data.data };
|
|
||||||
if (data?.error) return { error: data.error };
|
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callNccsOpenAITest(messages, options) {
|
async function callNccsOpenAITest(messages, options) {
|
||||||
@@ -225,7 +162,7 @@ async function callNccsOpenAITest(messages, options) {
|
|||||||
model: options.model,
|
model: options.model,
|
||||||
reverse_proxy: options.apiUrl,
|
reverse_proxy: options.apiUrl,
|
||||||
proxy_password: options.apiKey,
|
proxy_password: options.apiKey,
|
||||||
stream: false, // 旧版不支持 FakeStream
|
stream: !!options.stream,
|
||||||
max_tokens: options.maxTokens || 4000,
|
max_tokens: options.maxTokens || 4000,
|
||||||
temperature: options.temperature || 1,
|
temperature: options.temperature || 1,
|
||||||
top_p: options.top_p || 1,
|
top_p: options.top_p || 1,
|
||||||
@@ -234,25 +171,23 @@ async function callNccsOpenAITest(messages, options) {
|
|||||||
if (!isGoogleApi) {
|
if (!isGoogleApi) {
|
||||||
Object.assign(body, {
|
Object.assign(body, {
|
||||||
custom_prompt_post_processing: 'strict',
|
custom_prompt_post_processing: 'strict',
|
||||||
enable_web_search: false,
|
|
||||||
frequency_penalty: 0,
|
|
||||||
presence_penalty: 0.12,
|
presence_penalty: 0.12,
|
||||||
request_images: false,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch('/api/backends/chat-completions/generate', {
|
const fetchOpts = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { ...getRequestHeaders(), 'Content-Type': 'application/json' },
|
headers: { ...getRequestHeaders(), 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(body)
|
body: JSON.stringify(body)
|
||||||
});
|
};
|
||||||
|
|
||||||
if (!response.ok) {
|
if (options.stream) {
|
||||||
throw new Error(`Legacy HTTP ${response.status}: ${await response.text()}`);
|
return await fetchFakeStream('/api/backends/chat-completions/generate', fetchOpts);
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseData = await response.json();
|
const response = await fetch('/api/backends/chat-completions/generate', fetchOpts);
|
||||||
return responseData?.choices?.[0]?.message?.content;
|
if (!response.ok) throw new Error(`HTTP ${response.status}: ${await response.text()}`);
|
||||||
|
return normalizeApiResponse(await response.json());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callNccsSillyTavernPreset(messages, options) {
|
async function callNccsSillyTavernPreset(messages, options) {
|
||||||
@@ -269,24 +204,40 @@ async function callNccsSillyTavernPreset(messages, options) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (originalProfile !== targetProfile.name) {
|
if (originalProfile !== targetProfile.name) {
|
||||||
console.log(`[Legacy Switching profile: ${originalProfile} -> ${targetProfile.name}`);
|
|
||||||
await amilyHelper.triggerSlash(`/profile await=true "${targetProfile.name.replace(/"/g, '\\"')}"`);
|
await amilyHelper.triggerSlash(`/profile await=true "${targetProfile.name.replace(/"/g, '\\"')}"`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.stream) {
|
||||||
|
// [关键复刻] 完全继承 Profile 属性并注入运行时元数据
|
||||||
|
const body = {
|
||||||
|
...targetProfile,
|
||||||
|
messages: messages,
|
||||||
|
stream: true,
|
||||||
|
max_tokens: options.maxTokens || targetProfile.max_tokens || 2000,
|
||||||
|
user_name: context.name1 || 'User',
|
||||||
|
char_name: context.name2 || 'AI',
|
||||||
|
};
|
||||||
|
|
||||||
|
// 补全 URL 映射 (ST 后端对不同 source 字段名要求不同)
|
||||||
|
const rawUrl = body['api-url'] || body['api_url'] || body.custom_url || body.url;
|
||||||
|
if (rawUrl) {
|
||||||
|
if (body.chat_completion_source === 'custom') body.custom_url = rawUrl;
|
||||||
|
else body.reverse_proxy = rawUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchOpts = {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { ...getRequestHeaders(), 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(body)
|
||||||
|
};
|
||||||
|
return await fetchFakeStream('/api/backends/chat-completions/generate', fetchOpts);
|
||||||
|
}
|
||||||
|
|
||||||
if (!context.ConnectionManagerRequestService) throw new Error('ConnectionManagerRequestService unavailable');
|
if (!context.ConnectionManagerRequestService) throw new Error('ConnectionManagerRequestService unavailable');
|
||||||
|
const result = await context.ConnectionManagerRequestService.sendRequest(targetProfile.id, messages, options.maxTokens || 4000);
|
||||||
const result = await context.ConnectionManagerRequestService.sendRequest(
|
return normalizeApiResponse(result);
|
||||||
targetProfile.id,
|
|
||||||
messages,
|
|
||||||
options.maxTokens || 4000
|
|
||||||
);
|
|
||||||
|
|
||||||
const normalized = normalizeApiResponse(result);
|
|
||||||
if (normalized.error) throw new Error(normalized.error.message);
|
|
||||||
return normalized.content;
|
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// Restore profile
|
|
||||||
const current = await amilyHelper.triggerSlash('/profile');
|
const current = await amilyHelper.triggerSlash('/profile');
|
||||||
if (originalProfile && originalProfile !== current) {
|
if (originalProfile && originalProfile !== current) {
|
||||||
await amilyHelper.triggerSlash(`/profile await=true "${originalProfile.replace(/"/g, '\\"')}"`);
|
await amilyHelper.triggerSlash(`/profile await=true "${originalProfile.replace(/"/g, '\\"')}"`);
|
||||||
|
|||||||
Reference in New Issue
Block a user