Added proxy support to ChatCompletionService

4736f533a5b75dc2f612b5fa9fe81f96c2465555

bmen25124 <bmen25124@gmail.com>

2 files changed, +12 -4Showing whitespace changes
public/scripts/custom-request.js+7 -3
@@ -43,10 +43,12 @@ import EventSourceStream from './sse-stream.js';
4343 * @property {boolean?} [stream=false] - Whether to stream the response
4444 * @property {ChatCompletionMessage[]} messages - Array of chat messages
4545 * @property {string} [model] - Optional model name to use for completion
4646 * @property {string} chat_completion_source - Source provider for chat completion
4747 * @property {number} max_tokens - Maximum number of tokens to generate
4848 * @property {number} [temperature] - Optional temperature parameter for response randomness
4949 * @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
5052 */
5153
5254/** @typedef {Record<string, any> & ChatCompletionPayloadBase} ChatCompletionPayload */
@@ -387,7 +389,7 @@ export class ChatCompletionService {
387389 * @param {ChatCompletionPayload} custom
388390 * @returns {ChatCompletionPayload}
389391 */
390392 static createRequestData({ stream = false, messages, model, chat_completion_source, max_tokens, temperature, custom_url, reverse_proxy, proxy_password, ...props }) {
391393 const payload = {
392394 ...props,
393395 stream,
@@ -397,6 +399,8 @@ export class ChatCompletionService {
397399 max_tokens,
398400 temperature,
399401 custom_url,
402+ reverse_proxy,
403+ proxy_password,
400404 };
401405
402406 // Remove undefined values to avoid API errors
public/scripts/extensions/shared.js+5 -1
@@ -1,7 +1,7 @@
11import { CONNECT_API_MAP, getRequestHeaders } from '../../script.js';
22import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js';
33import { t } from '../i18n.js';
44import { oai_settings, proxies } from '../openai.js';
55import { SECRET_KEYS, secret_state } from '../secrets.js';
66import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js';
77import { getTokenCountAsync } from '../tokenizers.js';
@@ -326,6 +326,8 @@ export class ConnectionManagerRequestService {
326326 throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`);
327327 }
328328
329+ const proxyPreset = proxies.find((p) => p.name === profile.proxy);
330+
329331 const messages = Array.isArray(prompt) ? prompt : [{ role: 'user', content: prompt }];
330332 return await context.ChatCompletionService.processRequest({
331333 stream,
@@ -334,6 +336,8 @@ export class ConnectionManagerRequestService {
334336 model: profile.model,
335337 chat_completion_source: selectedApiMap.source,
336338 custom_url: profile['api-url'],
339+ reverse_proxy: proxyPreset?.url,
340+ proxy_password: proxyPreset?.password,
337341 }, {
338342 presetName: includePreset ? profile.preset : undefined,
339343 }, extractData, signal);