mirror of
https://github.com/Mintplex-Labs/anything-llm
synced 2026-04-25 17:15:37 +02:00
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:
@@ -88,6 +88,7 @@ GID='1000'
|
||||
# GENERIC_OPEN_AI_MODEL_PREF='gpt-3.5-turbo'
|
||||
# GENERIC_OPEN_AI_MODEL_TOKEN_LIMIT=4096
|
||||
# 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'
|
||||
# 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_TOKEN_LIMIT=4096
|
||||
# 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'
|
||||
# LITE_LLM_MODEL_PREF='gpt-3.5-turbo'
|
||||
|
||||
@@ -25,6 +25,7 @@ class GenericOpenAiLLM {
|
||||
apiKey: process.env.GENERIC_OPEN_AI_API_KEY ?? null,
|
||||
defaultHeaders: {
|
||||
"User-Agent": getAnythingLLMUserAgent(),
|
||||
...GenericOpenAiLLM.parseCustomHeaders(),
|
||||
},
|
||||
});
|
||||
this.model =
|
||||
@@ -49,6 +50,31 @@ class GenericOpenAiLLM {
|
||||
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 = []) {
|
||||
if (!contextTexts || !contextTexts.length) return "";
|
||||
return (
|
||||
|
||||
@@ -4,6 +4,7 @@ const InheritMultiple = require("./helpers/classes.js");
|
||||
const UnTooled = require("./helpers/untooled.js");
|
||||
const { toValidNumber } = require("../../../http/index.js");
|
||||
const { getAnythingLLMUserAgent } = require("../../../../endpoints/utils");
|
||||
const { GenericOpenAiLLM } = require("../../../AiProviders/genericOpenAi");
|
||||
|
||||
/**
|
||||
* The agent provider for the Generic OpenAI provider.
|
||||
@@ -23,6 +24,7 @@ class GenericOpenAiProvider extends InheritMultiple([Provider, UnTooled]) {
|
||||
maxRetries: 3,
|
||||
defaultHeaders: {
|
||||
"User-Agent": getAnythingLLMUserAgent(),
|
||||
...GenericOpenAiLLM.parseCustomHeaders(),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1289,6 +1289,8 @@ function dumpENV() {
|
||||
|
||||
// Allow disabling of streaming for generic openai
|
||||
"GENERIC_OPENAI_STREAMING_DISABLED",
|
||||
// Custom headers for Generic OpenAI
|
||||
"GENERIC_OPEN_AI_CUSTOM_HEADERS",
|
||||
|
||||
// Specify Chromium args for collector
|
||||
"ANYTHINGLLM_CHROMIUM_ARGS",
|
||||
|
||||
Reference in New Issue
Block a user