fix: Enhance tool binding in LangChainXAIService to support web search and update API handler for new parameters

This commit is contained in:
bsiggel
2026-03-15 16:37:57 +00:00
parent 59fdd7d9ec
commit 8e53fd6345
2 changed files with 97 additions and 26 deletions

View File

@@ -34,7 +34,13 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
"max_tokens": 2000,
"stream": false,
"extra_body": {
"collection_id": "col_abc123" // Optional: override auto-detection
"collection_id": "col_abc123", // Optional: override auto-detection
"enable_web_search": true, // Optional: enable web search (default: false)
"web_search_config": { // Optional: web search configuration
"allowed_domains": ["example.com"],
"excluded_domains": ["spam.com"],
"enable_image_understanding": true
}
}
}
@@ -91,9 +97,16 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
stream = body.get('stream', False)
extra_body = body.get('extra_body', {})
# Web Search parameters (default: disabled)
enable_web_search = extra_body.get('enable_web_search', False)
web_search_config = extra_body.get('web_search_config', {})
ctx.logger.info(f"📋 Model: {model_name}")
ctx.logger.info(f"📋 Messages: {len(messages)}")
ctx.logger.info(f"📋 Stream: {stream}")
ctx.logger.info(f"📋 Web Search: {'enabled' if enable_web_search else 'disabled'}")
if enable_web_search and web_search_config:
ctx.logger.debug(f"Web Search Config: {json.dumps(web_search_config, indent=2)}")
ctx.logger.debug(f"Messages: {json.dumps(messages, indent=2, ensure_ascii=False)}")
# Validate messages
@@ -141,15 +154,15 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
break # Only check first user message
# Priority 3: Error if no collection_id (strict mode)
if not collection_id:
ctx.logger.error("❌ No collection_id found (neither extra_body nor Aktenzeichen)")
ctx.logger.error(" Provide collection_id in extra_body or start message with Aktenzeichen")
# Priority 3: Error if no collection_id AND web_search disabled
if not collection_id and not enable_web_search:
ctx.logger.error("❌ No collection_id found and web_search disabled")
ctx.logger.error(" Provide collection_id, enable web_search, or both")
return ApiResponse(
status=400,
body={
'error': 'collection_id required',
'message': 'Provide collection_id in extra_body or start message with Aktenzeichen (e.g., "1234/56 question")'
'error': 'collection_id or web_search required',
'message': 'Provide collection_id in extra_body, enable web_search, or start message with Aktenzeichen (e.g., "1234/56 question")'
}
)
@@ -170,10 +183,12 @@ async def handler(request: ApiRequest, ctx: FlowContext[Any]) -> ApiResponse:
max_tokens=max_tokens
)
# Bind file_search tool
model_with_tools = langchain_service.bind_file_search(
# Bind tools (file_search and/or web_search)
model_with_tools = langchain_service.bind_tools(
model=model,
collection_id=collection_id,
enable_web_search=enable_web_search,
web_search_config=web_search_config,
max_num_results=10
)