import json, requests import gradio as gr def request_to_v2(message, cookie, user_id, channel_id,context=[]): timeout = 5*60 #5分钟不回复,显示超时 context = [message] headers = { # ':Authority': 'claude.ai', # ':Method': 'POST', # ':Path': '/api/append_message', # ':Scheme': 'https', 'Content-Type': 'application/json', 'Origin': 'https://claude.ai', 'Referer': f'https://claude.ai/chat/{channel_id}', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', 'Cookie': cookie, 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', # 'Accept-Encoding': 'gzip, deflate, br', 'Accept': 'text/event-stream, text/event-stream', 'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"Windows"', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'Connection': 'keep-alive', } post_msg_url = 'https://claude.ai/api/append_message' post_msg_data = { "completion":{ "incremental":True, "prompt": message, "timezone":"Asia/Shanghai", "model":"claude-2" }, "organization_uuid": user_id, "conversation_uuid": channel_id, "text": message, "attachments": [] } post_msg_data = json.dumps(post_msg_data) print(post_msg_data) try: response = requests.post(post_msg_url, headers=headers, data=post_msg_data, verify=False, stream=False,timeout = timeout) bots = "" print("a"*100) for data in response.iter_lines(): if data: # 将字节字符串转换为字符串 data_str = data.decode('utf-8') # 删除开头的 "data: " 部分 data_str = data_str.replace("data: ", "") data_json = json.loads(data_str) # print(data_json) bots += data_json["completion"] print(bots) context += [bots] responses = [(u, b) for u, b in zip(context[::2], context[1::2])] return responses, context except Exception as e: print(">>>>>> 查询失败") print(e) response =">>>>>> 查询失败\n报错信息为:"+ str(e) context += [response] responses = [(u, b) for u, b in zip(context[::2], context[1::2])] return responses, context if __name__ == "__main__": with gr.Blocks() as dialog_app: gr.HTML("""