| import asyncio |
| from app.models import ChatCompletionRequest |
| from app.services import GeminiClient |
| from app.utils.logging import log |
|
|
| |
| async def run_gemini_completion( |
| gemini_client, |
| chat_request: ChatCompletionRequest, |
| contents, |
| system_instruction, |
| request_type: str, |
| current_api_key: str, |
| safety_settings, |
| safety_settings_g2 |
| ): |
| """运行Gemini非流式请求""" |
| |
| run_fn = run_gemini_completion |
| |
| try: |
| |
| response_future = asyncio.create_task( |
| asyncio.to_thread( |
| gemini_client.complete_chat, |
| chat_request, |
| contents, |
| safety_settings_g2 if 'gemini-2.0-flash-exp' in chat_request.model else safety_settings, |
| system_instruction |
| ) |
| ) |
| |
| |
| response_content = await asyncio.shield(response_future) |
| |
| |
| if not hasattr(run_fn, 'logged_complete'): |
| log('info', "非流式请求成功完成", |
| extra={'key': current_api_key[:8], 'request_type': request_type, 'model': chat_request.model}) |
| run_fn.logged_complete = True |
| return response_content |
| except asyncio.CancelledError: |
| |
| if 'response_future' in locals() and not response_future.done(): |
| try: |
| |
| response_content = await asyncio.shield(response_future) |
| log('info', "API请求在客户端断开后完成", |
| extra={'key': current_api_key[:8], 'request_type': request_type, 'model': chat_request.model}) |
| return response_content |
| except Exception as e: |
| log('info', "API调用因客户端断开而失败", |
| extra={'key': current_api_key[:8], 'request_type': request_type, 'model': chat_request.model, 'error_message': f'API请求在客户端断开后失败: {str(e)}'}) |
| raise |
| |
| |
| log('info', "API调用因客户端断开而取消", |
| extra={'key': current_api_key[:8], 'request_type': request_type, 'model': chat_request.model, 'error_message': '客户端断开导致API调用取消'}) |
| raise |