Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -150,28 +150,41 @@ response = requests.get(json_url)
|
|
150 |
if response.status_code != 200:
|
151 |
raise Exception(f"Failed to load JSON from {json_url}. Status code: {response.status_code}")
|
152 |
|
153 |
-
#
|
154 |
-
|
|
|
155 |
|
156 |
-
#
|
157 |
-
|
158 |
-
raise ValueError("The JSON object must contain a 'question' field.")
|
159 |
|
|
|
|
|
|
|
|
|
|
|
160 |
# -------------------------------
|
161 |
-
# Step 2: Create
|
162 |
# -------------------------------
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
|
173 |
-
#
|
174 |
-
docs =
|
175 |
|
176 |
|
177 |
# -------------------------------
|
|
|
150 |
if response.status_code != 200:
|
151 |
raise Exception(f"Failed to load JSON from {json_url}. Status code: {response.status_code}")
|
152 |
|
153 |
+
# Ensure the request was successful
|
154 |
+
if response.status_code != 200:
|
155 |
+
raise Exception(f"Failed to load JSON from {json_url}. Status code: {response.status_code}")
|
156 |
|
157 |
+
# Parse the JSON array
|
158 |
+
tasks = response.json()
|
|
|
159 |
|
160 |
+
# Ensure that the loaded data is a list (JSON array)
|
161 |
+
if not isinstance(tasks, list):
|
162 |
+
raise ValueError("The JSON file must be a list of objects.")
|
163 |
+
|
164 |
+
|
165 |
# -------------------------------
|
166 |
+
# Step 2: Create Documents from Each JSON Object
|
167 |
# -------------------------------
|
168 |
+
docs = []
|
169 |
+
for task in tasks:
|
170 |
+
# Debugging: Print the keys of each task to ensure 'question' exists
|
171 |
+
print(f"Keys in task: {task.keys()}")
|
172 |
+
|
173 |
+
# Ensure the required field 'question' exists
|
174 |
+
if 'question' not in task:
|
175 |
+
print(f"Skipping task with missing 'question' field: {task}")
|
176 |
+
continue
|
177 |
+
|
178 |
+
content = task.get('question', "").strip()
|
179 |
+
if not content:
|
180 |
+
print(f"Skipping task with empty 'question': {task}")
|
181 |
+
continue
|
182 |
+
|
183 |
+
# Add unique ID to each document
|
184 |
+
task['id'] = str(uuid.uuid4())
|
185 |
|
186 |
+
# Create a document from the task data
|
187 |
+
docs.append(Document(page_content=content, metadata=task))
|
188 |
|
189 |
|
190 |
# -------------------------------
|