Added proxy support to ChatCompletionService
| @@ -43,10 +43,12 @@ import EventSourceStream from './sse-stream.js'; | |||
| 43 | * @property {boolean?} [stream=false] - Whether to stream the response | 43 | * @property {boolean?} [stream=false] - Whether to stream the response |
| 44 | * @property {ChatCompletionMessage[]} messages - Array of chat messages | 44 | * @property {ChatCompletionMessage[]} messages - Array of chat messages |
| 45 | * @property {string} [model] - Optional model name to use for completion | 45 | * @property {string} [model] - Optional model name to use for completion |
| 46 | * @property {string} chat_completion_source - Source provider for chat completion | 46 | * @property {string} chat_completion_source - Source provider |
| 47 | * @property {number} max_tokens - Maximum number of tokens to generate | 47 | * @property {number} max_tokens - Maximum number of tokens to generate |
| 48 | * @property {number} [temperature] - Optional temperature parameter for response randomness | 48 | * @property {number} [temperature] - Optional temperature parameter for response randomness |
| 49 | * @property {string} [custom_url] - Optional custom URL for chat completion | 49 | * @property {string} [custom_url] - Optional custom URL |
| 50 | * @property {string} [reverse_proxy] - Optional reverse proxy URL | ||
| 51 | * @property {string} [proxy_password] - Optional proxy password | ||
| 50 | */ | 52 | */ |
| 51 | 53 | ||
| 52 | /** @typedef {Record<string, any> & ChatCompletionPayloadBase} ChatCompletionPayload */ | 54 | /** @typedef {Record<string, any> & ChatCompletionPayloadBase} ChatCompletionPayload */ |
| @@ -387,7 +389,7 @@ export class ChatCompletionService { | |||
| 387 | * @param {ChatCompletionPayload} custom | 389 | * @param {ChatCompletionPayload} custom |
| 388 | * @returns {ChatCompletionPayload} | 390 | * @returns {ChatCompletionPayload} |
| 389 | */ | 391 | */ |
| 390 | static createRequestData({ stream = false, messages, model, chat_completion_source, max_tokens, temperature, custom_url, ...props }) { | 392 | static createRequestData({ stream = false, messages, model, chat_completion_source, max_tokens, temperature, custom_url, reverse_proxy, proxy_password, ...props }) { |
| 391 | const payload = { | 393 | const payload = { |
| 392 | ...props, | 394 | ...props, |
| 393 | stream, | 395 | stream, |
| @@ -397,6 +399,8 @@ export class ChatCompletionService { | |||
| 397 | max_tokens, | 399 | max_tokens, |
| 398 | temperature, | 400 | temperature, |
| 399 | custom_url, | 401 | custom_url, |
| 402 | reverse_proxy, | ||
| 403 | proxy_password, | ||
| 400 | }; | 404 | }; |
| 401 | 405 | ||
| 402 | // Remove undefined values to avoid API errors | 406 | // Remove undefined values to avoid API errors |
| @@ -1,7 +1,7 @@ | |||
| 1 | import { CONNECT_API_MAP, getRequestHeaders } from '../../script.js'; | 1 | import { CONNECT_API_MAP, getRequestHeaders } from '../../script.js'; |
| 2 | import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js'; | 2 | import { extension_settings, openThirdPartyExtensionMenu } from '../extensions.js'; |
| 3 | import { t } from '../i18n.js'; | 3 | import { t } from '../i18n.js'; |
| 4 | import { oai_settings } from '../openai.js'; | 4 | import { oai_settings, proxies } from '../openai.js'; |
| 5 | import { SECRET_KEYS, secret_state } from '../secrets.js'; | 5 | import { SECRET_KEYS, secret_state } from '../secrets.js'; |
| 6 | import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js'; | 6 | import { textgen_types, textgenerationwebui_settings } from '../textgen-settings.js'; |
| 7 | import { getTokenCountAsync } from '../tokenizers.js'; | 7 | import { getTokenCountAsync } from '../tokenizers.js'; |
| @@ -326,6 +326,8 @@ export class ConnectionManagerRequestService { | |||
| 326 | throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`); | 326 | throw new Error(`API type ${selectedApiMap.selected} does not support chat completions`); |
| 327 | } | 327 | } |
| 328 | 328 | ||
| 329 | const proxyPreset = proxies.find((p) => p.name === profile.proxy); | ||
| 330 | |||
| 329 | const messages = Array.isArray(prompt) ? prompt : [{ role: 'user', content: prompt }]; | 331 | const messages = Array.isArray(prompt) ? prompt : [{ role: 'user', content: prompt }]; |
| 330 | return await context.ChatCompletionService.processRequest({ | 332 | return await context.ChatCompletionService.processRequest({ |
| 331 | stream, | 333 | stream, |
| @@ -334,6 +336,8 @@ export class ConnectionManagerRequestService { | |||
| 334 | model: profile.model, | 336 | model: profile.model, |
| 335 | chat_completion_source: selectedApiMap.source, | 337 | chat_completion_source: selectedApiMap.source, |
| 336 | custom_url: profile['api-url'], | 338 | custom_url: profile['api-url'], |
| 339 | reverse_proxy: proxyPreset?.url, | ||
| 340 | proxy_password: proxyPreset?.password, | ||
| 337 | }, { | 341 | }, { |
| 338 | presetName: includePreset ? profile.preset : undefined, | 342 | presetName: includePreset ? profile.preset : undefined, |
| 339 | }, extractData, signal); | 343 | }, extractData, signal); |