向指定URL发送POST请求,并返回响应结果。 Args: url (str): 请求的目标URL。 payload (dict): 请求的负载数据,应该是一个字典类型。 timeout (int, optional): 请求的超时时间,默认为600秒。 stream (bool, optional): 是否以流的方式下载响应内容,默认为False。 Returns: response: 请求的响应结果,如果请求失败则返回None。
(url, payload, timeout=60, stream=False)
| 27 | |
| 28 | |
| 29 | def send_request(url, payload, timeout=60, stream=False): |
| 30 | """ |
| 31 | 向指定URL发送POST请求,并返回响应结果。 |
| 32 | |
| 33 | Args: |
| 34 | url (str): 请求的目标URL。 |
| 35 | payload (dict): 请求的负载数据,应该是一个字典类型。 |
| 36 | timeout (int, optional): 请求的超时时间,默认为600秒。 |
| 37 | stream (bool, optional): 是否以流的方式下载响应内容,默认为False。 |
| 38 | |
| 39 | Returns: |
| 40 | response: 请求的响应结果,如果请求失败则返回None。 |
| 41 | """ |
| 42 | headers = { |
| 43 | "Content-Type": "application/json", |
| 44 | } |
| 45 | base_logger.info("🔄 正在请求模型接口...") |
| 46 | |
| 47 | try: |
| 48 | res = requests.post(url, headers=headers, json=payload, stream=stream, timeout=timeout) |
| 49 | base_logger.info("🟢 接收响应中...\n") |
| 50 | return res |
| 51 | except requests.exceptions.Timeout: |
| 52 | base_logger.error(f"❌ 请求超时(超过 {timeout} 秒)") |
| 53 | return None |
| 54 | except requests.exceptions.RequestException as e: |
| 55 | base_logger.error(f"❌ 请求失败:{e}") |
| 56 | return None |
| 57 | |
| 58 | |
| 59 | def get_stream_chunks(response): |