Merge pull request #3838 from bmen25124/custom_req_proxy Added proxy support to ChatCompletionService
Signed| @@ -43,10 +43,12 @@ import EventSourceStream from './sse-stream.js'; | ||
| 43 | 43 | * @property {boolean?} [stream=false] - Whether to stream the response |
| 44 | 44 | * @property {ChatCompletionMessage[]} messages - Array of chat messages |
| 45 | 45 | * @property {string} [model] - Optional model name to use for completion |
| 46 | 46 | * @property {string} chat_completion_source - Source provider for chat completion |
| 47 | 47 | * @property {number} max_tokens - Maximum number of tokens to generate |
| 48 | 48 | * @property {number} [temperature] - Optional temperature parameter for response randomness |
| 49 | 49 | * @property {string} [custom_url] - Optional custom URL for chat completion |
| 50 | + * @property {string} [reverse_proxy] - Optional reverse proxy URL | |
| 51 | + * @property {string} [proxy_password] - Optional proxy password | |
| 50 | 52 | */ |
| 51 | 53 | |
| 52 | 54 | /** @typedef {Record<string, any> & ChatCompletionPayloadBase} ChatCompletionPayload */ |
| @@ -80,7 +82,6 @@ export class TextCompletionService { | ||
| 80 | 82 | */ |
| 81 | 83 | static createRequestData({ stream = false, prompt, max_tokens, model, api_type, api_server, temperature, min_p, ...props }) { |
| 82 | 84 | const payload = { |
| 83 | - ...props, | |
| 84 | 85 | stream, |
| 85 | 86 | prompt, |
| 86 | 87 | max_tokens, |
| @@ -90,6 +91,7 @@ export class TextCompletionService { | ||
| 90 | 91 | api_server: api_server ?? getTextGenServer(api_type), |
| 91 | 92 | temperature, |
| 92 | 93 | min_p, |
| 94 | + ...props, | |
| 93 | 95 | }; |
| 94 | 96 | |
| 95 | 97 | // Remove undefined values to avoid API errors |
| @@ -387,9 +389,8 @@ export class ChatCompletionService { | ||
| 387 | 389 | * @param {ChatCompletionPayload} custom |
| 388 | 390 | * @returns {ChatCompletionPayload} |
| 389 | 391 | */ |
| 390 | 392 | static createRequestData({ stream = false, messages, model, chat_completion_source, max_tokens, temperature, custom_url, reverse_proxy, proxy_password, ...props }) { |
| 391 | 393 | const payload = { |
| 392 | - ...props, | |
| 393 | 394 | stream, |
| 394 | 395 | messages, |
| 395 | 396 | model, |
| @@ -397,6 +398,11 @@ export class ChatCompletionService { | ||
| 397 | 398 | max_tokens, |
| 398 | 399 | temperature, |
| 399 | 400 | custom_url, |
| 401 | + reverse_proxy, | |
| 402 | + proxy_password, | |
| 403 | + use_makersuite_sysprompt: true, | |
| 404 | + claude_use_sysprompt: true, | |
| 405 | + ...props, | |
| 400 | 406 | }; |
| 401 | 407 | |
| 402 | 408 | // Remove undefined values to avoid API errors |
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { CONNECT_API_MAP, getRequestHeaders } from '../../script.js'; |
| 2 | 2 | import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js'; |
| 3 | 3 | import { t } from '../i18n.js'; |
| 4 | 4 | import { oai_settings, proxies } from '../openai.js'; |
| 5 | 5 | import { SECRET_KEYS, secret_state } from '../secrets.js'; |
| 6 | 6 | import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js'; |
| 7 | 7 | import { getTokenCountAsync } from '../tokenizers.js'; |
| @@ -306,9 +306,10 @@ export class ConnectionManagerRequestService { | ||
| 306 | 306 | * @param {boolean?} [custom.includePreset=true] |
| 307 | 307 | * @param {boolean?} [custom.includeInstruct=true] |
| 308 | 308 | * @param {Partial<InstructSettings>?} [custom.instructSettings] Override instruct settings |
| 309 | + * @param {Record<string, any>} [overridePayload] - Override payload for the request | |
| 309 | 310 | * @returns {Promise<import('../custom-request.js').ExtractedData | (() => AsyncGenerator<import('../custom-request.js').StreamResponse>)>} If not streaming, returns extracted data; if streaming, returns a function that creates an AsyncGenerator |
| 310 | 311 | */ |
| 311 | 312 | static async sendRequest(profileId, prompt, maxTokens, custom = this.defaultSendRequestParams, overridePayload = {}) { |
| 312 | 313 | const { stream, signal, extractData, includePreset, includeInstruct, instructSettings } = { ...this.defaultSendRequestParams, ...custom }; |
| 313 | 314 | |
| 314 | 315 | const context = SillyTavern.getContext(); |
| @@ -326,6 +327,8 @@ export class ConnectionManagerRequestService { | ||
| 326 | 327 | throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`); |
| 327 | 328 | } |
| 328 | 329 | |
| 330 | + const proxyPreset = proxies.find((p) => p.name === profile.proxy); | |
| 331 | + | |
| 329 | 332 | const messages = Array.isArray(prompt) ? prompt : [{ role: 'user', content: prompt }]; |
| 330 | 333 | return await context.ChatCompletionService.processRequest({ |
| 331 | 334 | stream, |
| @@ -334,6 +337,9 @@ export class ConnectionManagerRequestService { | ||
| 334 | 337 | model: profile.model, |
| 335 | 338 | chat_completion_source: selectedApiMap.source, |
| 336 | 339 | custom_url: profile['api-url'], |
| 340 | + reverse_proxy: proxyPreset?.url, | |
| 341 | + proxy_password: proxyPreset?.password, | |
| 342 | + ...overridePayload, | |
| 337 | 343 | }, { |
| 338 | 344 | presetName: includePreset ? profile.preset : undefined, |
| 339 | 345 | }, extractData, signal); |
| @@ -350,6 +356,7 @@ export class ConnectionManagerRequestService { | ||
| 350 | 356 | model: profile.model, |
| 351 | 357 | api_type: selectedApiMap.type, |
| 352 | 358 | api_server: profile['api-url'], |
| 359 | + ...overridePayload, | |
| 353 | 360 | }, { |
| 354 | 361 | instructName: includeInstruct ? profile.instruct : undefined, |
| 355 | 362 | presetName: includePreset ? profile.preset : undefined, |