-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
108 lines (87 loc) · 3.61 KB
/
api_client.py
File metadata and controls
108 lines (87 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# api_client.py (v2 - Updated for POST /queries endpoint )
import os
import requests
import time
from dotenv import load_dotenv
# --- Configuration ---
load_dotenv()
LIMITLESS_API_KEY = os.getenv("LIMITLESS_API_KEY")
BASE_API_URL = "https://api.limitless.ai/v1/"
# --- Core Functions ---
def check_api_key( ):
"""Checks if the API key was successfully loaded."""
if not LIMITLESS_API_KEY:
print("❌ ERROR: LIMITLESS_API_KEY not found in .env file.")
return False
print(f"✅ API Key loaded successfully (starts with: {LIMITLESS_API_KEY[:6]}...).")
return True
def get_latest_meeting_transcript():
"""
Connects to the Limitless API by creating a query to fetch the latest meeting.
This matches the official API documentation.
"""
if not check_api_key():
return None
# The correct endpoint according to the documentation
endpoint = "queries"
url = BASE_API_URL + endpoint
headers = {
"Authorization": f"Bearer {LIMITLESS_API_KEY}",
"Content-Type": "application/json"
}
# The "payload" or "body" of our request.
# We are asking the AI a question in plain English.
payload = {
"prompt": "What was my last meeting and what is the full transcript?"
}
print("\n🔄 Sending query to Limitless API: 'What was my last meeting...?'")
try:
# Step 1: Create the query
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
query_creation_data = response.json()
query_id = query_creation_data.get("id")
if not query_id:
print("❌ Failed to create a query. Response did not include an ID.")
print(f" Response: {query_creation_data}")
return None
print(f"✅ Query created successfully with ID: {query_id}")
# Step 2: Poll for the result
result_url = f"{url}/{query_id}"
print(f"🔄 Polling for result at: {result_url}")
while True:
result_response = requests.get(result_url, headers=headers)
result_response.raise_for_status()
result_data = result_response.json()
status = result_data.get("status")
print(f" Query status: {status}")
if status == "completed":
print("✅ Query completed!")
# Assuming the answer is in a field called 'result' or 'answer'
answer = result_data.get("result", {}).get("answer", "No answer found.")
return answer
if status in ["failed", "error"]:
print(f"❌ Query failed with status: {status}")
print(f" Details: {result_data}")
return None
# Wait for 2 seconds before checking again
time.sleep(2)
except requests.exceptions.HTTPError as http_err:
print(f"❌ HTTP Error occurred: {http_err}" )
print(f" Status Code: {http_err.response.status_code}" )
print(f" Response Text: {http_err.response.text}" )
return None
except Exception as err:
print(f"❌ An unexpected error occurred: {err}")
return None
# --- Self-Testing Block ---
if __name__ == "__main__":
print("--- Running API Client Self-Test (v2) ---")
transcript = get_latest_meeting_transcript()
if transcript:
print("\n--- Test Successful! ---")
print("Retrieved Answer/Transcript:")
print(transcript)
else:
print("\n--- Test Failed ---")
print("Could not retrieve the answer. Please check the error messages above.")