Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(llm): forward OpenAI user on embedding and parse requests (#19065)

Öndery38230c45d3f9
diff --git a/api/apps/restful_apis/chunk_api.py b/api/apps/restful_apis/chunk_api.py
index e1bebc3a2c..757b3dae81 100644
--- a/api/apps/restful_apis/chunk_api.py
+++ b/api/apps/restful_apis/chunk_api.py
@@ -38,6 +38,7 @@ from api.db.services.knowledgebase_service import KnowledgebaseService, validate
from api.db.services.llm_service import LLMBundle
from api.db.services.task_service import TaskService, cancel_all_task_of
from api.db.services.tenant_llm_service import TenantLLMService
+from common.llm_request_context import normalize_llm_user_id, reset_llm_request_context, set_llm_request_context
from api.utils.api_utils import (
add_tenant_id_to_kwargs,
check_duplicate_ids,
@@ -64,6 +65,15 @@ from rag.app.tag import label_question
from rag.nlp import search
from rag.prompts.generator import cross_languages, keyword_extraction
+
+def _encode_with_request_user(embd_mdl, texts, req):
+ token = set_llm_request_context(user_id=normalize_llm_user_id(req.get("user_id")))
+ try:
+ return embd_mdl.encode(texts)
+ finally:
+ reset_llm_request_context(token)
+
+
DOC_STOP_PARSING_INVALID_STATE_MESSAGE = "Can't stop parsing document that has not started or already completed"
DOC_STOP_PARSING_INVALID_STATE_ERROR_CODE = "DOC_STOP_PARSING_INVALID_STATE"
@@ -206,6 +216,7 @@ async def parse(tenant_id, dataset_id):
req = await get_request_json()
if not req.get("document_ids"):
return get_error_data_result("`document_ids` is required")
+ llm_user_id = normalize_llm_user_id(req.get("user_id"))
doc_list = req.get("document_ids")
unique_doc_ids, duplicate_messages = check_duplicate_ids(doc_list, "document")
doc_list = unique_doc_ids
@@ -247,7 +258,7 @@ async def parse(tenant_id, dataset_id):
TaskService.filter_delete([Task.doc_id == id])
e, doc = DocumentService.get_by_id(id)
doc = doc.to_dict()
- DocumentService.run(tenant_id, doc, kb_table_num_map)
+ DocumentService.run(tenant_id, doc, kb_table_num_map, user_id=llm_user_id)
success_count += 1
if not_found:
return get_result(message=f"Documents not found: {not_found}", code=RetCode.DATA_ERROR)
@@ -1084,7 +1095,11 @@ async def add_chunk(tenant_id, dataset_id, document_id):
embd_id = DocumentService.get_embd_id(document_id)
model_config = resolve_model_config(dataset_tenant_id, LLMType.EMBEDDING.value, embd_id)
embd_mdl = TenantLLMService.model_instance(model_config)
- v, c = embd_mdl.encode([doc.name, req["content"] if not d["question_kwd"] else "\n".join(d["question_kwd"])])
+ v, c = _encode_with_request_user(
+ embd_mdl,
+ [doc.name, req["content"] if not d["question_kwd"] else "\n".join(d["question_kwd"])],
+ req,
+ )
v = 0.1 * v[0] + 0.9 * v[1]
d[f"q_{len(v)}_vec"] = v.tolist()
settings.docStoreConn.insert([d], search.index_name(dataset_tenant_id), dataset_id)
@@ -1238,11 +1253,13 @@ async def update_chunk(tenant_id, dataset_id, document_id, chunk_id):
q, a = rmPrefix(arr[0]), rmPrefix(arr[1])
d = beAdoc(d, arr[0], arr[1], not any([rag_tokenizer.is_chinese(t) for t in q + a]))
- v, _ = embd_mdl.encode(
+ v, _ = _encode_with_request_user(
+ embd_mdl,
[
doc.name,
d["content_with_weight"] if not d.get("question_kwd") else "\n".join(d["question_kwd"]),
- ]
+ ],
+ req,
)
v = 0.1 * v[0] + 0.9 * v[1] if doc.parser_id != ParserType.QA else v[1]
d[f"q_{len(v)}_vec"] = v.tolist()
diff --git a/api/apps/restful_apis/document_api.py b/api/apps/restful_apis/document_api.py
index dff073bf73..7428312e2e 100644
--- a/api/apps/restful_apis/document_api.py
+++ b/api/apps/restful_apis/document_api.py
@@ -72,6 +72,7 @@ from api.utils.validation_utils import (
from common import settings
from common.constants import ParserType, RetCode, TaskStatus, SANDBOX_ARTIFACT_BUCKET
+from common.llm_request_context import normalize_llm_user_id
from common.metadata_utils import convert_conditions, meta_filter, turn2jsonschema
from common.misc_utils import get_uuid, thread_pool_exec, thread_pool_exec_long_time
from api.utils.file_utils import filename_type, thumbnail
@@ -1532,7 +1533,7 @@ def _run_sync(user_id: str, req):
doc.parser_config["metadata"] = kb.parser_config.get("metadata", {})
DocumentService.update_parser_config(doc.id, doc.parser_config)
doc_dict = doc.to_dict()
- DocumentService.run(doc_tenant_id, doc_dict, kb_table_num_map)
+ DocumentService.run(doc_tenant_id, doc_dict, kb_table_num_map, user_id=normalize_llm_user_id(req.get("user_id")))
return None, None
@@ -1571,6 +1572,9 @@ async def parse_documents(tenant_id, dataset_id):
items:
type: string
description: List of document IDs to parse.
+ user_id:
+ type: string
+ description: Optional end-user identifier forwarded as the OpenAI user field on embedding requests.
responses:
200:
description: Successful operation.
@@ -1587,6 +1591,7 @@ async def parse_documents(tenant_id, dataset_id):
return get_error_data_result(message="`document_ids` is required")
if len(document_ids) == 0:
return get_error_data_result(message="`document_ids` is required")
+ llm_user_id = normalize_llm_user_id(req.get("user_id"))
# Check for duplicate document IDs
unique_doc_ids, duplicate_messages = check_duplicate_ids(document_ids, "document")
@@ -1636,7 +1641,7 @@ async def parse_documents(tenant_id, dataset_id):
settings.docStoreConn.delete({"doc_id": doc_id}, search.index_name(tenant_id), doc.kb_id)
doc_dict = doc.to_dict()
- DocumentService.run(tenant_id, doc_dict, kb_table_num_map)
+ DocumentService.run(tenant_id, doc_dict, kb_table_num_map, user_id=llm_user_id)
success_count += 1
result = {"success_count": success_count}
diff --git a/api/db/services/document_service.py b/api/db/services/document_service.py
index b5f866fc34..8dfc706085 100644
--- a/api/db/services/document_service.py
+++ b/api/db/services/document_service.py
@@ -1223,11 +1223,12 @@ class DocumentService(CommonService):
return {"processing": int(row["processing"]), "finished": int(row["finished"]), "failed": int(row["failed"]), "cancelled": int(cancelled), "downloaded": int(downloaded)}
@classmethod
- def run(cls, tenant_id: str, doc: dict, kb_table_num_map: dict):
+ def run(cls, tenant_id: str, doc: dict, kb_table_num_map: dict, user_id: str | None = None):
from api.db.services.task_service import queue_dataflow, queue_tasks
from api.db.services.file2document_service import File2DocumentService
doc["tenant_id"] = tenant_id
+ llm_user_id = user_id or doc.get("llm_user_id")
doc_parser = doc.get("parser_id", ParserType.NAIVE)
if doc_parser == ParserType.TABLE:
kb_id = doc.get("kb_id")
@@ -1239,10 +1240,10 @@ class DocumentService(CommonService):
if kb_table_num_map[kb_id] <= 0:
KnowledgebaseService.delete_field_map(kb_id)
if doc.get("pipeline_id", ""):
- queue_dataflow(tenant_id, flow_id=doc["pipeline_id"], task_id=get_uuid(), doc_id=doc["id"])
+ queue_dataflow(tenant_id, flow_id=doc["pipeline_id"], task_id=get_uuid(), doc_id=doc["id"], user_id=llm_user_id)
else:
bucket, name = File2DocumentService.get_storage_address(doc_id=doc["id"])
- queue_tasks(doc, bucket, name, 0)
+ queue_tasks(doc, bucket, name, 0, user_id=llm_user_id)
def queue_raptor_o_graphrag_tasks(sample_doc, ty, priority, fake_doc_id="", doc_ids=None):
diff --git a/api/db/services/task_service.py b/api/db/services/task_service.py
index f723d9c719..8132657d86 100644
--- a/api/db/services/task_service.py
+++ b/api/db/services/task_service.py
@@ -31,6 +31,7 @@ from api.db.services.document_service import DocumentService
from common.misc_utils import get_uuid
from common.time_utils import current_timestamp, get_format_time
from common.constants import StatusEnum, TaskStatus, MAXIMUM_PAGE_NUMBER, MAXIMUM_TASK_PAGE_NUMBER
+from common.llm_request_context import normalize_llm_user_id
from deepdoc.parser.excel_parser import RAGFlowExcelParser
from rag.utils.redis_conn import REDIS_CONN
from common import settings
@@ -440,7 +441,7 @@ class TaskService(CommonService):
return cls.model.delete().where(cls.model.doc_id.in_(doc_ids)).execute()
-def queue_tasks(doc: dict, bucket: str, name: str, priority: int):
+def queue_tasks(doc: dict, bucket: str, name: str, priority: int, user_id: str | None = None):
"""Create and queue document processing tasks.
This function creates processing tasks for a document based on its type and configuration.
@@ -453,6 +454,9 @@ def queue_tasks(doc: dict, bucket: str, name: str, priority: int):
bucket (str): Storage bucket name where the document is stored.
name (str): File name of the document.
priority (int, optional): Priority level for task queueing (default is 0).
+ user_id (str, optional): End-user identifier forwarded on embedding HTTP
+ calls as the OpenAI ``user`` field. Stored on the Redis message only
+ (Task rows have no ``user_id`` column).
Note:
- For PDF documents, tasks are created per page range based on configuration
@@ -563,8 +567,15 @@ def queue_tasks(doc: dict, bucket: str, name: str, priority: int):
if chunking_n > 0:
assert seed_doc_chunking_counter(doc["id"], chunking_n), "Can't access Redis. Please check the Redis' status."
try:
+ llm_user_id = normalize_llm_user_id(user_id) or normalize_llm_user_id(doc.get("llm_user_id"))
for unfinished_task in unfinished_task_array:
- assert REDIS_CONN.queue_product(settings.get_svr_queue_name(priority, suffix), message=unfinished_task), "Can't access Redis. Please check the Redis' status."
+ message = dict(unfinished_task)
+ if llm_user_id:
+ # Redis-only: Task rows have no user_id column. The worker copies
+ # this onto the in-memory task and installs it as LLM request context
+ # so embedding HTTP calls can forward OpenAI ``user``.
+ message["user_id"] = llm_user_id
+ assert REDIS_CONN.queue_product(settings.get_svr_queue_name(priority, suffix), message=message), "Can't access Redis. Please check the Redis' status."
except Exception:
abort_doc_chunking_counter(doc["id"])
raise
@@ -634,7 +645,16 @@ def has_canceled(task_id):
return False
-def queue_dataflow(tenant_id: str, flow_id: str, task_id: str, doc_id: str = CANVAS_DEBUG_DOC_ID, file: dict = None, priority: int = 0, rerun: bool = False) -> tuple[bool, str]:
+def queue_dataflow(
+ tenant_id: str,
+ flow_id: str,
+ task_id: str,
+ doc_id: str = CANVAS_DEBUG_DOC_ID,
+ file: dict = None,
+ priority: int = 0,
+ rerun: bool = False,
+ user_id: str | None = None,
+) -> tuple[bool, str]:
task = dict(
id=task_id,
@@ -654,6 +674,9 @@ def queue_dataflow(tenant_id: str, flow_id: str, task_id: str, doc_id: str = CAN
task["tenant_id"] = tenant_id
task["dataflow_id"] = flow_id
task["file"] = file
+ llm_user_id = normalize_llm_user_id(user_id)
+ if llm_user_id:
+ task["user_id"] = llm_user_id
if not REDIS_CONN.queue_product(settings.get_svr_queue_name(priority, "common"), message=task):
return False, "Can't access Redis. Please check the Redis' status."
diff --git a/common/llm_request_context.py b/common/llm_request_context.py
index bfc95f2d43..d7a5494f53 100644
--- a/common/llm_request_context.py
+++ b/common/llm_request_context.py
@@ -16,11 +16,16 @@
"""Per-request identifiers forwarded to upstream LLM providers.
-An agent run (or any LLM-issuing flow) installs the originating ``session_id`` /
-``user_id`` here. The chat model layer reads it and forwards an end-user
-identifier as the OpenAI-standard ``user`` request field, which providers such as
-OpenAI and OpenRouter include in the request body. This lets upstream activity be
-correlated back to the session/user that produced it.
+An agent run, chat turn, or document parse/ingest flow installs the originating
+``session_id`` / ``user_id`` here. Chat completions and OpenAI-compatible embedding
+calls read it and forward an end-user identifier as the OpenAI-standard ``user``
+request field. Providers such as OpenAI and OpenRouter include it in the request
+body so upstream activity can be correlated back to the session or user that
+produced it.
+
+Parse workers receive ``user_id`` on the Redis task payload (not the Task table)
+and install it here without a ``session_id``, so ``current_llm_user()`` falls
+back to that value.
The value is a small dict (or ``None`` when no request context is active), e.g.
``{"session_id": "...", "user_id": "..."}``.
@@ -32,6 +37,16 @@ import logging
llm_request_context: contextvars.ContextVar = contextvars.ContextVar("ragflow_llm_request_context", default=None)
+def normalize_llm_user_id(value) -> str | None:
+ """Sanitize an optional end-user id from an API body or Redis task payload."""
+ if not isinstance(value, str):
+ return None
+ trimmed = value.strip()
+ if not trimmed:
+ return None
+ return trimmed[:128]
+
+
def set_llm_request_context(session_id: str | None = None, user_id: str | None = None):
"""Install the current request identifiers and return the reset token.
@@ -41,8 +56,9 @@ def set_llm_request_context(session_id: str | None = None, user_id: str | None =
ctx = {}
if session_id:
ctx["session_id"] = str(session_id)[:128]
+ user_id = normalize_llm_user_id(user_id)
if user_id:
- ctx["user_id"] = str(user_id)[:128]
+ ctx["user_id"] = user_id
# Log only presence flags, never the raw identifiers.
logging.debug("Installing LLM request context (session=%s, user=%s)", bool(session_id), bool(user_id))
return llm_request_context.set(ctx or None)
@@ -69,3 +85,13 @@ def current_llm_user() -> str | None:
if not ctx:
return None
return ctx.get("session_id") or ctx.get("user_id") or None
+
+
+def openai_user_kwargs() -> dict:
+ """OpenAI ``user`` kwargs when LLM request context is active; otherwise empty.
+
+ Chat completions already forward this field. Embedding calls should use the
+ same helper so upstream gateways can attribute encoding the same way.
+ """
+ user = current_llm_user()
+ return {"user": user} if user else {}
diff --git a/docs/references/http_api_reference.md b/docs/references/http_api_reference.md
index cecf18917c..b543e40873 100644
--- a/docs/references/http_api_reference.md
+++ b/docs/references/http_api_reference.md
@@ -1562,6 +1562,7 @@ This endpoint only supports datasets that use the built-in chunking pipeline. Fo
- `'Authorization: Bearer <YOUR_API_KEY>'`
- Body:
- `"document_ids"`: `list[string]`
+ - `"user_id"`: `string` (optional)
##### Request example
@@ -1572,7 +1573,8 @@ curl --request POST \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--data '
{
- "document_ids": ["97a5f1c2759811efaa500242ac120004","97ad64b6759811ef9fc30242ac120004"]
+ "document_ids": ["97a5f1c2759811efaa500242ac120004","97ad64b6759811ef9fc30242ac120004"],
+ "user_id": "end-user-123"
}'
```
@@ -1582,6 +1584,8 @@ curl --request POST \
The dataset ID.
- `"document_ids"`: (*Body parameter*), `list[string]`, *Required*
The IDs of the documents to parse.
+- `"user_id"`: (*Body parameter*), `string`, *Optional*
+ End-user identifier forwarded as the OpenAI `user` field on embedding requests for this parse job. Omitted when unset. The value is carried on the worker queue only; it is not stored on Task rows.
#### Response
@@ -1621,6 +1625,7 @@ Starts, cancels, or reruns ingestion for documents. Use this endpoint for docume
- `"doc_ids"`: `list[string]`
- `"run"`: `string`
- `"delete"`: `boolean`
+ - `"user_id"`: `string` (optional)
##### Request example
@@ -1633,7 +1638,8 @@ curl --request POST \
{
"doc_ids": ["97a5f1c2759811efaa500242ac120004"],
"run": "1",
- "delete": true
+ "delete": true,
+ "user_id": "end-user-123"
}'
```
@@ -1645,6 +1651,8 @@ curl --request POST \
The ingestion action. Use `"1"` to start ingestion and `"2"` to cancel ingestion.
- `"delete"`: (*Body parameter*), `boolean`
Whether to delete existing tasks and chunks before rerunning. Defaults to `false`.
+- `"user_id"`: (*Body parameter*), `string`, *Optional*
+ End-user identifier forwarded as the OpenAI `user` field on embedding requests when `run` starts ingestion. Omitted when unset.
#### Response
@@ -1748,6 +1756,7 @@ Adds a chunk to a specified document in a specified dataset.
- `"tag_kwd"`: `list[string]`
- `"questions"`: `list[string]`
- `"image_base64"`: `string`
+ - `"user_id"`: `string` (optional)
##### Request example
@@ -1779,6 +1788,8 @@ curl --request POST \
Optional questions to use when embedding the chunk.
- `"image_base64"`: (*Body parameter*), `string`
A base64-encoded image to associate with the chunk.
+- `"user_id"`: (*Body parameter*), `string`, *Optional*
+ End-user identifier forwarded as the OpenAI `user` field on the embedding request for this chunk. Omitted when unset.
#### Response
@@ -2079,6 +2090,7 @@ Updates content or configurations for a specified chunk.
- `"tag_kwd"`: `list[string]`
- `"available"`: `boolean`
- `"image_base64"`: `string`
+ - `"user_id"`: `string` (optional)
##### Request example
@@ -2118,6 +2130,8 @@ curl --request PATCH \
- `false`: Unavailable
- `"image_base64"`: (*Body parameter*), `string`
Base64-encoded image content to associate with the chunk.
+- `"user_id"`: (*Body parameter*), `string`, *Optional*
+ End-user identifier forwarded as the OpenAI `user` field on the embedding request for this update. Omitted when unset.
#### Response
diff --git a/rag/llm/embedding_model.py b/rag/llm/embedding_model.py
index 0012715e39..3ffc61513e 100644
--- a/rag/llm/embedding_model.py
+++ b/rag/llm/embedding_model.py
@@ -31,6 +31,7 @@ from zai import ZhipuAiClient
from common import settings
from common.aimlapi_utils import attribution_headers
from common.exceptions import ModelException
+from common.llm_request_context import openai_user_kwargs
from common.token_utils import num_tokens_from_string, truncate, total_token_count_from_response
from rag.llm.key_utils import _normalize_replicate_key
from rag.llm.mws_utils import mws_api_url, require_mws_token
@@ -267,12 +268,14 @@ class OpenAIEmbed(Base):
self.model_name = model_name
def _call(self, batch):
- # extra_body is forwarded verbatim to the provider. \`drop_params\` is
+ # extra_body is forwarded verbatim to the provider. `drop_params` is
# an OpenRouter-specific convention; Together AI (and any strict
# OpenAI-compatible provider) rejects it with HTTP 400
- # "Unrecognized request arguments supplied: drop_params". Send only
- # fields that every OpenAI-compatible provider accepts.
- res = self.client.embeddings.create(input=batch, model=self.model_name, encoding_format="float")
+ # "Unrecognized request arguments supplied: drop_params".
+ # `user` is OpenAI-standard and is only sent when LLM request context
+ # is active. Local servers that reject unknown fields (LocalAI,
+ # LM Studio, Xinference) use their own embed classes and omit it.
+ res = self.client.embeddings.create(input=batch, model=self.model_name, encoding_format="float", **openai_user_kwargs())
return [d.embedding for d in _sorted_by_index(res.data)], total_token_count_from_response(res)
def encode(self, texts: list):
@@ -295,6 +298,7 @@ class LocalAIEmbed(Base):
self.model_name = model_name.split("___")[0]
def _call(self, batch):
+ # Local servers often reject OpenAI's optional `user` field.
res = self.client.embeddings.create(input=batch, model=self.model_name)
# Local servers (LocalAI / LM Studio) usually omit usage data; fall back
# to a local tiktoken count rather than fabricating a fixed number.
@@ -539,6 +543,7 @@ class XinferenceEmbed(Base):
self.model_name = model_name
def _call(self, batch):
+ # Xinference's OpenAI-compatible server may reject unknown fields such as `user`.
res = self.client.embeddings.create(input=batch, model=self.model_name)
return [d.embedding for d in _sorted_by_index(res.data)], total_token_count_from_response(res)
@@ -1489,7 +1494,7 @@ class OpenRouterEmbed(Base):
if self.provider_order:
order = [s.strip() for s in self.provider_order.split(",") if s.strip()]
extra_body["provider"] = {"order": order, "allow_fallbacks": False}
- res = self.client.embeddings.create(input=batch, model=self.model_name, encoding_format="float", extra_body=extra_body)
+ res = self.client.embeddings.create(input=batch, model=self.model_name, encoding_format="float", extra_body=extra_body, **openai_user_kwargs())
return [d.embedding for d in _sorted_by_index(res.data)], total_token_count_from_response(res)
def encode(self, texts: list):
diff --git a/rag/svr/task_executor.py b/rag/svr/task_executor.py
index c16c44eef8..7b2009463b 100644
--- a/rag/svr/task_executor.py
+++ b/rag/svr/task_executor.py
@@ -98,6 +98,7 @@ from rag.svr.task_executor_limiter import (
)
from common import settings
from common.constants import PAGERANK_FLD, TAG_FLD, SVR_CONSUMER_GROUP_NAME
+from common.llm_request_context import normalize_llm_user_id, reset_llm_request_context, set_llm_request_context
from rag.utils.table_es_metadata import (
aggregate_table_doc_metadata,
merge_table_parser_config_from_kb,
@@ -172,6 +173,15 @@ FAILED_TASKS = 0
CURRENT_TASKS = {}
+
+def _redact_task_user(task: dict) -> dict:
+ """Copy a task dict for logs/heartbeat without the raw end-user identifier."""
+ payload = dict(task)
+ if "user_id" in payload:
+ payload["user_id"] = True
+ return payload
+
+
WORKER_HEARTBEAT_TIMEOUT = int(os.environ.get("WORKER_HEARTBEAT_TIMEOUT", "120"))
# Recycle the worker process after this many completed tasks to release memory
# that long-lived libraries cannot reclaim on their own -- notably ONNX
@@ -305,6 +315,10 @@ async def collect():
task["tenant_id"] = msg["tenant_id"]
task["source_id"] = msg["source_id"]
task["message_dict"] = msg["message_dict"]
+ # Redis-only: Task rows have no user_id column. Copy it onto the in-memory
+ # task so handle_task can install LLM request context for embedding calls.
+ if msg.get("user_id"):
+ task["user_id"] = msg["user_id"]
return redis_msg, task
@@ -1781,8 +1795,9 @@ async def handle_task() -> bool:
task_type = task["task_type"]
pipeline_task_type = TASK_TYPE_TO_PIPELINE_TASK_TYPE.get(task_type, PipelineTaskType.PARSE) or PipelineTaskType.PARSE
task_id = task["id"]
+ ctx_token = set_llm_request_context(user_id=normalize_llm_user_id(task.get("user_id")))
try:
- CURRENT_TASKS[task["id"]] = copy.deepcopy(task)
+ CURRENT_TASKS[task["id"]] = _redact_task_user(copy.deepcopy(task))
run_mode = os.environ.get("TE_RUN_MODE", "0")
logging.info(f"TE_RUN_MODE is {run_mode}")
@@ -1805,7 +1820,7 @@ async def handle_task() -> bool:
DONE_TASKS += 1
CURRENT_TASKS.pop(task_id, None)
- logging.info(f"handle_task done for task {json.dumps(task)}")
+ logging.info(f"handle_task done for task {json.dumps(_redact_task_user(task))}")
except TaskCanceledException as e:
DONE_TASKS += 1
CURRENT_TASKS.pop(task_id, None)
@@ -1822,8 +1837,9 @@ async def handle_task() -> bool:
except Exception as e:
logging.exception(f"[Exception]: {str(e)}")
pass
- logging.exception(f"handle_task got exception for task {json.dumps(task)}")
+ logging.exception(f"handle_task got exception for task {json.dumps(_redact_task_user(task))}")
finally:
+ reset_llm_request_context(ctx_token)
if not task.get("dataflow_id", ""):
referred_document_id = None
if task_type in _KB_FANOUT_TASK_TYPES:
diff --git a/test/unit_test/common/test_llm_request_context.py b/test/unit_test/common/test_llm_request_context.py
index b6748e806c..d81a6702f8 100644
--- a/test/unit_test/common/test_llm_request_context.py
+++ b/test/unit_test/common/test_llm_request_context.py
@@ -27,6 +27,8 @@ import pytest
from common.llm_request_context import (
current_llm_user,
llm_request_context,
+ normalize_llm_user_id,
+ openai_user_kwargs,
reset_llm_request_context,
set_llm_request_context,
)
@@ -69,6 +71,13 @@ class TestCurrentLlmUser:
finally:
reset_llm_request_context(token)
+ def test_whitespace_user_id_is_ignored(self):
+ token = set_llm_request_context(user_id=" ")
+ try:
+ assert current_llm_user() is None
+ finally:
+ reset_llm_request_context(token)
+
def test_identifier_is_truncated_to_128_chars(self):
token = set_llm_request_context(session_id="s" * 200)
try:
@@ -137,3 +146,39 @@ class TestCompletionArgsUserPrecedence:
def test_no_context_leaves_user_unset(self):
args = self._construct()
assert "user" not in args
+
+
+class TestNormalizeLlmUserId:
+ def test_rejects_non_strings(self):
+ assert normalize_llm_user_id(None) is None
+ assert normalize_llm_user_id(12) is None
+ assert normalize_llm_user_id(["u"]) is None
+
+ def test_rejects_blank_strings(self):
+ assert normalize_llm_user_id("") is None
+ assert normalize_llm_user_id(" ") is None
+
+ def test_strips_and_truncates(self):
+ assert normalize_llm_user_id(" end-user ") == "end-user"
+ assert normalize_llm_user_id("u" * 200) == "u" * 128
+
+
+class TestOpenaiUserKwargs:
+ def test_empty_without_context(self):
+ assert openai_user_kwargs() == {}
+
+ def test_forwards_user_id_when_session_absent(self):
+ token = set_llm_request_context(user_id="end-user-1")
+ try:
+ assert openai_user_kwargs() == {"user": "end-user-1"}
+ finally:
+ reset_llm_request_context(token)
+
+ def test_prefers_session_id(self):
+ token = set_llm_request_context(session_id="sess-1", user_id="end-user-1")
+ try:
+ assert openai_user_kwargs() == {"user": "sess-1"}
+ finally:
+ reset_llm_request_context(token)
diff --git a/test/unit_test/rag/llm/test_embedding_model.py b/test/unit_test/rag/llm/test_embedding_model.py
index 13059b7a8a..cb879684ca 100644
--- a/test/unit_test/rag/llm/test_embedding_model.py
+++ b/test/unit_test/rag/llm/test_embedding_model.py
@@ -427,3 +427,24 @@ class TestBaiduYiyanResponseParsing:
assert vector.shape == (3,)
np.testing.assert_array_equal(vector, np.array([1.0, 2.0, 3.0]))
assert tokens == 7
+
+
+class TestOpenAIUserForwarding:
+ """OpenAI-compatible embedding calls forward ``user`` when LLM context is set."""
+
+ def test_forwards_user_when_context_set(self):
+ from common.llm_request_context import reset_llm_request_context, set_llm_request_context
+
+ embed = _make_openai(total_tokens=3)
+ token = set_llm_request_context(user_id="end-user-1")
+ try:
+ embed.encode(["hello"])
+ finally:
+ reset_llm_request_context(token)
+ assert embed.client.embeddings.create.call_args.kwargs["user"] == "end-user-1"
+
+ def test_omits_user_when_no_context(self):
+ embed = _make_openai(total_tokens=3)
+ embed.encode(["hello"])
+ assert "user" not in embed.client.embeddings.create.call_args.kwargs