Skip to content

Commit 7e9d632

Browse files
Fix/research module (#694)
1 parent 6c863ce commit 7e9d632

7 files changed

Lines changed: 34 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ celerybeat-schedule
9494
*.sage.py
9595

9696
# Environments
97-
.env
97+
projects/deep_research/.env
9898
.venv
99+
.env
99100
env/
100101
venv/
101102
ENV/

ms_agent/tools/search_engine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from typing import Any, Dict
33

4+
from ms_agent.config.env import Env
45
from ms_agent.tools.exa import ExaSearch
56
from ms_agent.tools.search.arxiv import ArxivSearch
67
from ms_agent.tools.search.search_base import SearchEngineType
@@ -68,6 +69,8 @@ def replace_env_vars(value: str) -> str:
6869
if not isinstance(value, str):
6970
return value
7071

72+
Env.load_env()
73+
7174
if value.startswith('$'):
7275
env_var = value[1:]
7376
return os.getenv(env_var, None)

ms_agent/workflow/research_workflow.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import copy
44
import os
55
import re
6-
from datetime import datetime
76
from typing import Any, Dict, List, Optional, Union
87

98
import json
@@ -236,6 +235,8 @@ def filter_search_res(single_res: SearchResult):
236235
search_results = [
237236
filter_search_res(single_res) for single_res in search_results
238237
]
238+
239+
# TODO: Implement a more robust way to handle multiple search results
239240
dump_batch_search_results(results=search_results, file_path=self.workdir_structure['search'])
240241

241242
return self.workdir_structure['search']
@@ -244,25 +245,35 @@ def filter_search_res(single_res: SearchResult):
244245
def parse_json_from_content(text_content: str) -> Union[List[Dict[str, Any]], Dict[str, Any]]:
245246
"""
246247
Parses the given text content to extract JSON format data.
248+
It can parse JSON embedded within triple backticks (```json...```)
249+
or stand-alone JSON text.
247250
248251
Args:
249252
text_content (str): The text content containing JSON format data.
250253
251254
Returns:
252-
list: A dict or list of dictionaries representing the target data.
255+
list: A dict or list of dictionaries representing the parsed JSON data.
253256
"""
257+
# Try to find JSON embedded in ```json...```
254258
pattern = r'```json(.*?)```'
255-
256259
matches = re.findall(pattern, text_content, re.DOTALL)
257260

258-
if len(matches) == 0:
261+
json_string = ''
262+
if matches:
263+
# If matches are found, use the first one
264+
json_string = matches[0].strip()
265+
else:
266+
# If no ```json...``` block is found, assume the entire content is JSON
267+
json_string = text_content.strip()
268+
269+
if not json_string:
259270
return []
260271

261272
try:
262-
items = json.loads(matches[0].strip())
273+
items = json.loads(json_string)
263274
return items
264275
except json.JSONDecodeError as e:
265-
raise ValueError(f'parse_py_todo: JSON decoding error: {e}') from e
276+
raise ValueError(f'Failed to parse JSON content. Error: {e}')
266277

267278
def _dump_todo_file(self):
268279

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
EXA_API_KEY=xxx
2+
SERPAPI_API_KEY=xxx

projects/deep_research/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ pip install -r requirements/research.txt
5858

5959
#### Environment Setting
6060

61-
1. If youre using Exa or SerpApi, make sure your .env file includes the following configuration settings:
61+
1. If you're using Exa or SerpApi, make sure your .env file includes the following configuration settings:
6262
```bash
63-
EXA_API_KEY="xxx"
64-
SERPAPI_API_KEY="xxx"
63+
cp .env.example ../../.env
64+
65+
# Then, edit the `.env` file to include your API keys:
66+
EXA_API_KEY=xxx
67+
SERPAPI_API_KEY=xxx
6568
```
69+
6670
2. Configure the search engine in conf.yaml, using free arxiv search by default:
6771
```yaml
6872
SEARCH_ENGINE:

projects/deep_research/conf.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#Search Engine, Supported values: EXA, SERPAPI, ARXIV
1+
## Search Engine, Supported values: EXA, SERPAPI, ARXIV ##
2+
23
#SEARCH_ENGINE:
34
# engine: exa
45
# exa_api_key: $EXA_API_KEY

projects/deep_research/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def run_workflow(user_prompt: str, task_dir: str, reuse: bool,
2424
if __name__ == '__main__':
2525

2626
query: str = 'Survey of the AI Agent within the recent 3 month, including the latest research papers, open-source projects, and industry applications.' # noqa
27-
task_workdir: str = '/path/to/your_task_dir'
27+
task_workdir: str = '/path/to/your_workdir' # Specify your task work directory here
2828
reuse: bool = False
2929

3030
# Get chat client OpenAI compatible api

0 commit comments

Comments
 (0)