from authentication import get_access_token import requests def upload_file(file_path, token, assistant_id): url = f"{base_url}/file_upload" # Check if the file exists try: file = open(file_path, 'rb') except FileNotFoundError: return {"error": "File not found", "file_path": file_path} try: # Dynamically get the file name from the file path file_name = file_path.split('/')[-1] files = {'file': (file_name, file)} headers = { "Authorization": f"Bearer {token}" } data = { "assistant_id": assistant_id } print("Request Headers:", headers) print("Request Data:", data) print("File Path:", file_path) response = requests.post(url, files=files, headers=headers) print("Response Status Code:", response.status_code) print("Response JSON:", response.json()) if response.status_code == 200: return response.json() else: return {"error": "File upload failed", "status_code": response.status_code, "response": response.json()} except requests.RequestException as e: return {"error": "Request failed", "details": str(e)} finally: file.close() api_key = '25bda2c39c0f8ca0' api_secret = 'e0008b9b9727cb8ceea5a132dbe62495' token = get_access_token(api_key, api_secret) print(token) assistant_id = "66b46e7d1c146ed6a5220d7a" base_url = "https://chatglm.cn/chatglm/assistant-api/v1" upload_file_path = "/Users/tigeren/Dev/digisky/market_assistant/pingcap.xlsx" upload_file_response = upload_file(upload_file_path, token, assistant_id) print(upload_file_response)