feat: Add option to forward user info as headers to OpenAI API

This commit is contained in:
Duc Nguyen 2024-10-30 11:02:56 -04:00
parent 09935d191f
commit 3f0b3ea90e
4 changed files with 42 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from open_webui.config import (
AUDIO_TTS_VOICE, AUDIO_TTS_VOICE,
AUDIO_TTS_AZURE_SPEECH_REGION, AUDIO_TTS_AZURE_SPEECH_REGION,
AUDIO_TTS_AZURE_SPEECH_OUTPUT_FORMAT, AUDIO_TTS_AZURE_SPEECH_OUTPUT_FORMAT,
OPENAI_FORWARD_USER_INFO_AS_HEADERS,
CACHE_DIR, CACHE_DIR,
CORS_ALLOW_ORIGIN, CORS_ALLOW_ORIGIN,
WHISPER_MODEL, WHISPER_MODEL,
@ -248,6 +249,12 @@ async def speech(request: Request, user=Depends(get_verified_user)):
headers["Authorization"] = f"Bearer {app.state.config.TTS_OPENAI_API_KEY}" headers["Authorization"] = f"Bearer {app.state.config.TTS_OPENAI_API_KEY}"
headers["Content-Type"] = "application/json" headers["Content-Type"] = "application/json"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
try: try:
body = body.decode("utf-8") body = body.decode("utf-8")
body = json.loads(body) body = json.loads(body)

View File

@ -25,6 +25,7 @@ from open_webui.config import (
COMFYUI_WORKFLOW, COMFYUI_WORKFLOW,
COMFYUI_WORKFLOW_NODES, COMFYUI_WORKFLOW_NODES,
CORS_ALLOW_ORIGIN, CORS_ALLOW_ORIGIN,
OPENAI_FORWARD_USER_INFO_AS_HEADERS,
ENABLE_IMAGE_GENERATION, ENABLE_IMAGE_GENERATION,
IMAGE_GENERATION_ENGINE, IMAGE_GENERATION_ENGINE,
IMAGE_GENERATION_MODEL, IMAGE_GENERATION_MODEL,
@ -456,6 +457,12 @@ async def image_generations(
headers["Authorization"] = f"Bearer {app.state.config.OPENAI_API_KEY}" headers["Authorization"] = f"Bearer {app.state.config.OPENAI_API_KEY}"
headers["Content-Type"] = "application/json" headers["Content-Type"] = "application/json"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
data = { data = {
"model": ( "model": (
app.state.config.MODEL app.state.config.MODEL

View File

@ -16,6 +16,7 @@ from open_webui.config import (
MODEL_FILTER_LIST, MODEL_FILTER_LIST,
OPENAI_API_BASE_URLS, OPENAI_API_BASE_URLS,
OPENAI_API_KEYS, OPENAI_API_KEYS,
OPENAI_FORWARD_USER_INFO_AS_HEADERS,
AppConfig, AppConfig,
) )
from open_webui.env import ( from open_webui.env import (
@ -140,6 +141,11 @@ async def speech(request: Request, user=Depends(get_verified_user)):
if "openrouter.ai" in app.state.config.OPENAI_API_BASE_URLS[idx]: if "openrouter.ai" in app.state.config.OPENAI_API_BASE_URLS[idx]:
headers["HTTP-Referer"] = "https://openwebui.com/" headers["HTTP-Referer"] = "https://openwebui.com/"
headers["X-Title"] = "Open WebUI" headers["X-Title"] = "Open WebUI"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
r = None r = None
try: try:
r = requests.post( r = requests.post(
@ -325,6 +331,11 @@ async def get_models(url_idx: Optional[int] = None, user=Depends(get_verified_us
headers = {} headers = {}
headers["Authorization"] = f"Bearer {key}" headers["Authorization"] = f"Bearer {key}"
headers["Content-Type"] = "application/json" headers["Content-Type"] = "application/json"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
r = None r = None
@ -437,6 +448,11 @@ async def generate_chat_completion(
if "openrouter.ai" in app.state.config.OPENAI_API_BASE_URLS[idx]: if "openrouter.ai" in app.state.config.OPENAI_API_BASE_URLS[idx]:
headers["HTTP-Referer"] = "https://openwebui.com/" headers["HTTP-Referer"] = "https://openwebui.com/"
headers["X-Title"] = "Open WebUI" headers["X-Title"] = "Open WebUI"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
r = None r = None
session = None session = None
@ -505,6 +521,11 @@ async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
headers = {} headers = {}
headers["Authorization"] = f"Bearer {key}" headers["Authorization"] = f"Bearer {key}"
headers["Content-Type"] = "application/json" headers["Content-Type"] = "application/json"
if OPENAI_FORWARD_USER_INFO_AS_HEADERS:
headers["X-OpenWebUI-User-Name"] = user.name
headers["X-OpenWebUI-User-Id"] = user.id
headers["X-OpenWebUI-User-Email"] = user.email
headers["X-OpenWebUI-User-Role"] = user.role
r = None r = None
session = None session = None

View File

@ -422,6 +422,7 @@ OAUTH_ADMIN_ROLES = PersistentConfig(
) )
def load_oauth_providers(): def load_oauth_providers():
OAUTH_PROVIDERS.clear() OAUTH_PROVIDERS.clear()
if GOOGLE_CLIENT_ID.value and GOOGLE_CLIENT_SECRET.value: if GOOGLE_CLIENT_ID.value and GOOGLE_CLIENT_SECRET.value:
@ -657,6 +658,12 @@ except Exception:
OPENAI_API_BASE_URL = "https://api.openai.com/v1" OPENAI_API_BASE_URL = "https://api.openai.com/v1"
OPENAI_FORWARD_USER_INFO_AS_HEADERS = PersistentConfig(
"OPENAI_FORWARD_USER_INFO_AS_HEADERS",
"oauth.OPENAI_FORWARD_USER_INFO_AS_HEADERS",
os.environ.get("OPENAI_FORWARD_USER_INFO_AS_HEADERS", "False").lower() == "true",
)
#################################### ####################################
# WEBUI # WEBUI
#################################### ####################################