add support for custom headers for LLM Generic OpenAI (#4999)
* add support for custom headers for LLM Generic OpenAI * add env
This commit is contained in:
parent
1ccf468158
commit
dba1be0600
@ -88,6 +88,7 @@ GID='1000'
|
|||||||
# GENERIC_OPEN_AI_MODEL_PREF='gpt-3.5-turbo'
|
# GENERIC_OPEN_AI_MODEL_PREF='gpt-3.5-turbo'
|
||||||
# GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=4096
|
# GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=4096
|
||||||
# GENERIC_OPEN_AI_API_KEY=sk-123abc
|
# GENERIC_OPEN_AI_API_KEY=sk-123abc
|
||||||
|
# GENERIC_OPEN_AI_CUSTOM_HEADERS="X-Custom-Auth:my-secret-key,X-Custom-Header:my-value" (useful if using a proxy that requires authentication or other headers)
|
||||||
|
|
||||||
# LLM_PROVIDER='litellm'
|
# LLM_PROVIDER='litellm'
|
||||||
# LITE_LLM_MODEL_PREF='gpt-3.5-turbo'
|
# LITE_LLM_MODEL_PREF='gpt-3.5-turbo'
|
||||||
|
|||||||
@ -94,6 +94,7 @@ SIG_SALT='salt' # Please generate random string at least 32 chars long.
|
|||||||
# GENERIC_OPEN_AI_MODEL_PREF='gpt-3.5-turbo'
|
# GENERIC_OPEN_AI_MODEL_PREF='gpt-3.5-turbo'
|
||||||
# GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=4096
|
# GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=4096
|
||||||
# GENERIC_OPEN_AI_API_KEY=sk-123abc
|
# GENERIC_OPEN_AI_API_KEY=sk-123abc
|
||||||
|
# GENERIC_OPEN_AI_CUSTOM_HEADERS="X-Custom-Auth:my-secret-key,X-Custom-Header:my-value" (useful if using a proxy that requires authentication or other headers)
|
||||||
|
|
||||||
# LLM_PROVIDER='litellm'
|
# LLM_PROVIDER='litellm'
|
||||||
# LITE_LLM_MODEL_PREF='gpt-3.5-turbo'
|
# LITE_LLM_MODEL_PREF='gpt-3.5-turbo'
|
||||||
|
|||||||
@ -25,6 +25,7 @@ class GenericOpenAiLLM {
|
|||||||
apiKey: process.env.GENERIC_OPEN_AI_API_KEY ?? null,
|
apiKey: process.env.GENERIC_OPEN_AI_API_KEY ?? null,
|
||||||
defaultHeaders: {
|
defaultHeaders: {
|
||||||
"User-Agent": getAnythingLLMUserAgent(),
|
"User-Agent": getAnythingLLMUserAgent(),
|
||||||
|
...GenericOpenAiLLM.parseCustomHeaders(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
this.model =
|
this.model =
|
||||||
@ -49,6 +50,31 @@ class GenericOpenAiLLM {
|
|||||||
console.log(`\x1b[36m[${this.className}]\x1b[0m ${text}`, ...args);
|
console.log(`\x1b[36m[${this.className}]\x1b[0m ${text}`, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses custom headers from a CSV-formatted environment variable.
|
||||||
|
* Format: "Header-Name:value,Another-Header:value2"
|
||||||
|
* @returns {Object} Object with header key-value pairs
|
||||||
|
*/
|
||||||
|
static parseCustomHeaders() {
|
||||||
|
const customHeadersEnv = process.env.GENERIC_OPEN_AI_CUSTOM_HEADERS;
|
||||||
|
if (!customHeadersEnv) return {};
|
||||||
|
|
||||||
|
const headers = {};
|
||||||
|
const pairs = customHeadersEnv.split(",");
|
||||||
|
|
||||||
|
for (const pair of pairs) {
|
||||||
|
const colonIndex = pair.indexOf(":"); // only split on first colon for key/value separation
|
||||||
|
if (colonIndex === -1) continue;
|
||||||
|
|
||||||
|
const key = pair.substring(0, colonIndex).trim();
|
||||||
|
const value = pair.substring(colonIndex + 1).trim();
|
||||||
|
|
||||||
|
if (key && value) headers[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
||||||
#appendContext(contextTexts = []) {
|
#appendContext(contextTexts = []) {
|
||||||
if (!contextTexts || !contextTexts.length) return "";
|
if (!contextTexts || !contextTexts.length) return "";
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -4,6 +4,7 @@ const InheritMultiple = require("./helpers/classes.js");
|
|||||||
const UnTooled = require("./helpers/untooled.js");
|
const UnTooled = require("./helpers/untooled.js");
|
||||||
const { toValidNumber } = require("../../../http/index.js");
|
const { toValidNumber } = require("../../../http/index.js");
|
||||||
const { getAnythingLLMUserAgent } = require("../../../../endpoints/utils");
|
const { getAnythingLLMUserAgent } = require("../../../../endpoints/utils");
|
||||||
|
const { GenericOpenAiLLM } = require("../../../AiProviders/genericOpenAi");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The agent provider for the Generic OpenAI provider.
|
* The agent provider for the Generic OpenAI provider.
|
||||||
@ -23,6 +24,7 @@ class GenericOpenAiProvider extends InheritMultiple([Provider, UnTooled]) {
|
|||||||
maxRetries: 3,
|
maxRetries: 3,
|
||||||
defaultHeaders: {
|
defaultHeaders: {
|
||||||
"User-Agent": getAnythingLLMUserAgent(),
|
"User-Agent": getAnythingLLMUserAgent(),
|
||||||
|
...GenericOpenAiLLM.parseCustomHeaders(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1289,6 +1289,8 @@ function dumpENV() {
|
|||||||
|
|
||||||
// Allow disabling of streaming for generic openai
|
// Allow disabling of streaming for generic openai
|
||||||
"GENERIC_OPENAI_STREAMING_DISABLED",
|
"GENERIC_OPENAI_STREAMING_DISABLED",
|
||||||
|
// Custom headers for Generic OpenAI
|
||||||
|
"GENERIC_OPEN_AI_CUSTOM_HEADERS",
|
||||||
|
|
||||||
// Specify Chromium args for collector
|
// Specify Chromium args for collector
|
||||||
"ANYTHINGLLM_CHROMIUM_ARGS",
|
"ANYTHINGLLM_CHROMIUM_ARGS",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user