wt002 commited on
Commit
46c3b43
·
verified ·
1 Parent(s): b280aa5

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +30 -17
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
- # Parse the JSON object
154
- data = response.json()
 
155
 
156
- # Ensure the required field 'question' exists
157
- if 'question' not in data:
158
- raise ValueError("The JSON object must contain a 'question' field.")
159
 
 
 
 
 
 
160
  # -------------------------------
161
- # Step 2: Create a Document from the JSON Object
162
  # -------------------------------
163
- content = data.get('question', "").strip()
164
- if not content:
165
- raise ValueError("The 'question' field in the JSON object cannot be empty.")
166
-
167
- # Create a document and add metadata from the JSON object
168
- document = Document(
169
- page_content=content,
170
- metadata=data
171
- )
 
 
 
 
 
 
 
 
172
 
173
- # Wrap the document in a list to work with LangChain (as it expects a list of documents)
174
- docs = [document]
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
  # -------------------------------