Jiayou-Chao commited on
Commit
d3ea5e9
·
1 Parent(s): caa8fa4

Pull Request: Integrate .env File Support and Add Backend Usage Example (#187)

Browse files

* feat: Integrate .env file support for environment variables
feat: Add backend_example.py file support for using without a frontend

- Added .env.example with necessary environment variable placeholders: OPENAI_API_KEY, OPENAI_API_BASE, OPENAI_MODEL, SILICON_API_KEY, SILICON_MODEL.
- Updated models.py to load environment variables using python-dotenv.
- Modified gpt4 amd silicon model configurations to use values from .env file or defaults.
- Updated .gitignore to exclude .env file.
- Updated requirements.txt to include python-dotenv.
- Updated README.md to document environment variable setup and backend usage example.
- Added backend_example.py for direct backend interaction.

* (README.md): remove new features section from forked version

Files changed (6) hide show
  1. .env.example +5 -0
  2. .gitignore +2 -0
  3. README.md +40 -25
  4. backend_example.py +34 -0
  5. mindsearch/agent/models.py +5 -2
  6. requirements.txt +1 -0
.env.example ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ OPENAI_API_KEY=
2
+ OPENAI_API_BASE=
3
+ OPENAI_MODEL=
4
+ SILICON_API_KEY=
5
+ SILICON_MODEL=
.gitignore CHANGED
@@ -160,4 +160,6 @@ cython_debug/
160
  # and can be added to the global gitignore or merged into this file. For a more nuclear
161
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
  #.idea/
 
 
163
  temp
 
160
  # and can be added to the global gitignore or merged into this file. For a more nuclear
161
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
  #.idea/
163
+
164
+ .env
165
  temp
README.md CHANGED
@@ -15,29 +15,6 @@ English | [简体中文](README_zh-CN.md)
15
 
16
  ## ✨ MindSearch: Mimicking Human Minds Elicits Deep AI Searcher
17
 
18
- MindSearch is an open-source AI Search Engine Framework with Perplexity.ai Pro performance. You can simply deploy it with your own perplexity.ai style search engine with either close-source LLMs (GPT, Claude) or open-source LLMs ([InternLM2.5 series](https://huggingface.co/internlm/internlm2_5-7b-chat) are specifically optimized to provide superior performance within the MindSearch framework; other open-source models have not been specifically tested). It owns following features:
19
-
20
- - 🤔 **Ask everything you want to know**: MindSearch is designed to solve any question in your life and use web knowledge.
21
- - 📚 **In-depth Knowledge Discovery**: MindSearch browses hundreds of web pages to answer your question, providing deeper and wider knowledge base answer.
22
- - 🔍 **Detailed Solution Path**: MindSearch exposes all details, allowing users to check everything they want. This greatly improves the credibility of its final response as well as usability.
23
- - 💻 **Optimized UI Experience**: Providing all kinds of interfaces for users, including React, Gradio, Streamlit and Terminal. Choose any type based on your need.
24
- - 🧠 **Dynamic Graph Construction Process**: MindSearch decomposes the user query into atomic sub-questions as nodes in the graph and progressively extends the graph based on the search result from WebSearcher.
25
-
26
- <div align="center">
27
-
28
- <img src="assets/teaser.gif">
29
-
30
- </div>
31
-
32
- ## ⚡️ MindSearch vs other AI Search Engines
33
-
34
- Comparison on human preference based on depth, breadth, factuality of the response generated by ChatGPT-Web, Perplexity.ai (Pro), and MindSearch. Results are obtained on 100 human-crafted real-world questions and evaluated by 5 human experts\*.
35
-
36
- <div align="center">
37
- <img src="assets/mindsearch_openset.png" width="90%">
38
- </div>
39
- * All experiments are done before July.7 2024.
40
-
41
  ## ⚽️ Build Your Own MindSearch
42
 
43
  ### Step1: Dependencies Installation
@@ -48,7 +25,16 @@ cd MindSearch
48
  pip install -r requirements.txt
49
  ```
50
 
51
- ### Step2: Setup MindSearch API
 
 
 
 
 
 
 
 
 
52
 
53
  Setup FastAPI Server.
54
 
@@ -69,7 +55,7 @@ python -m mindsearch.app --lang en --model_format internlm_server --search_engin
69
 
70
  Please set your Web Search engine API key as the `WEB_SEARCH_API_KEY` environment variable unless you are using `DuckDuckGo`.
71
 
72
- ### Step3: Setup MindSearch Frontend
73
 
74
  Providing following frontend interfaces,
75
 
@@ -104,6 +90,35 @@ python frontend/mindsearch_gradio.py
104
  streamlit run frontend/mindsearch_streamlit.py
105
  ```
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  ## 🐞 Debug Locally
108
 
109
  ```bash
 
15
 
16
  ## ✨ MindSearch: Mimicking Human Minds Elicits Deep AI Searcher
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  ## ⚽️ Build Your Own MindSearch
19
 
20
  ### Step1: Dependencies Installation
 
25
  pip install -r requirements.txt
26
  ```
27
 
28
+ ### Step2: Setup Environment Variables
29
+
30
+ Before setting up the API, you need to configure environment variables. Rename the `.env.example` file to `.env` and fill in the required values.
31
+
32
+ ```bash
33
+ mv .env.example .env
34
+ # Open .env and add your keys and model configurations
35
+ ```
36
+
37
+ ### Step3: Setup MindSearch API
38
 
39
  Setup FastAPI Server.
40
 
 
55
 
56
  Please set your Web Search engine API key as the `WEB_SEARCH_API_KEY` environment variable unless you are using `DuckDuckGo`.
57
 
58
+ ### Step4: Setup MindSearch Frontend
59
 
60
  Providing following frontend interfaces,
61
 
 
90
  streamlit run frontend/mindsearch_streamlit.py
91
  ```
92
 
93
+ ## 🌐 Change Web Search API
94
+
95
+ To use a different type of web search API, modify the `searcher_type` attribute in the `searcher_cfg` located in `mindsearch/agent/__init__.py`. Currently supported web search APIs include:
96
+
97
+ - `GoogleSearch`
98
+ - `DuckDuckGoSearch`
99
+ - `BraveSearch`
100
+ - `BingSearch`
101
+
102
+ For example, to change to the Brave Search API, you would configure it as follows:
103
+
104
+ ```python
105
+ BingBrowser(
106
+ searcher_type='BraveSearch',
107
+ topk=2,
108
+ api_key=os.environ.get('BRAVE_API_KEY', 'YOUR BRAVE API')
109
+ )
110
+ ```
111
+
112
+ ## 🐞 Using the Backend Without Frontend
113
+
114
+ For users who prefer to interact with the backend directly, use the `backend_example.py` script. This script demonstrates how to send a query to the backend and process the response.
115
+
116
+ ```bash
117
+ python backend_example.py
118
+ ```
119
+
120
+ Make sure you have set up the environment variables and the backend is running before executing the script.
121
+
122
  ## 🐞 Debug Locally
123
 
124
  ```bash
backend_example.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+
4
+ # Define the backend URL
5
+ url = 'http://localhost:8002/solve'
6
+ headers = {'Content-Type': 'application/json'}
7
+
8
+ # Function to send a query to the backend and get the response
9
+ def get_response(query):
10
+ # Prepare the input data
11
+ data = {'inputs': [{'role': 'user', 'content': query}]}
12
+
13
+ # Send the request to the backend
14
+ response = requests.post(url, headers=headers, data=json.dumps(data), timeout=20, stream=True)
15
+
16
+ # Process the streaming response
17
+ for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b'\n'):
18
+ if chunk:
19
+ decoded = chunk.decode('utf-8')
20
+ if decoded == '\r':
21
+ continue
22
+ if decoded[:6] == 'data: ':
23
+ decoded = decoded[6:]
24
+ elif decoded.startswith(': ping - '):
25
+ continue
26
+ response_data = json.loads(decoded)
27
+ agent_return = response_data['response']
28
+ node_name = response_data['current_node']
29
+ print(f"Node: {node_name}, Response: {agent_return['response']}")
30
+
31
+ # Example usage
32
+ if __name__ == '__main__':
33
+ query = "What is the weather like today in New York?"
34
+ get_response(query)
mindsearch/agent/models.py CHANGED
@@ -1,8 +1,11 @@
1
  import os
 
2
 
3
  from lagent.llms import (GPTAPI, INTERNLM2_META, HFTransformerCasualLM,
4
  LMDeployClient, LMDeployServer)
5
 
 
 
6
  internlm_server = dict(type=LMDeployServer,
7
  path='internlm/internlm2_5-7b-chat',
8
  model_name='internlm2',
@@ -36,7 +39,7 @@ internlm_hf = dict(type=HFTransformerCasualLM,
36
  stop_words=['<|im_end|>'])
37
  # openai_api_base needs to fill in the complete chat api address, such as: https://api.openai.com/v1/chat/completions
38
  gpt4 = dict(type=GPTAPI,
39
- model_type='gpt-4-turbo',
40
  key=os.environ.get('OPENAI_API_KEY', 'YOUR OPENAI API KEY'),
41
  openai_api_base=os.environ.get('OPENAI_API_BASE', 'https://api.openai.com/v1/chat/completions'),
42
  )
@@ -60,7 +63,7 @@ qwen = dict(type=GPTAPI,
60
  stop_words=['<|im_end|>'])
61
 
62
  internlm_silicon = dict(type=GPTAPI,
63
- model_type='internlm/internlm2_5-7b-chat',
64
  key=os.environ.get('SILICON_API_KEY', 'YOUR SILICON API KEY'),
65
  openai_api_base='https://api.siliconflow.cn/v1/chat/completions',
66
  meta_template=[
 
1
  import os
2
+ from dotenv import load_dotenv
3
 
4
  from lagent.llms import (GPTAPI, INTERNLM2_META, HFTransformerCasualLM,
5
  LMDeployClient, LMDeployServer)
6
 
7
+ load_dotenv()
8
+
9
  internlm_server = dict(type=LMDeployServer,
10
  path='internlm/internlm2_5-7b-chat',
11
  model_name='internlm2',
 
39
  stop_words=['<|im_end|>'])
40
  # openai_api_base needs to fill in the complete chat api address, such as: https://api.openai.com/v1/chat/completions
41
  gpt4 = dict(type=GPTAPI,
42
+ model_type=os.environ.get('OPENAI_MODEL', 'gpt-4o'),
43
  key=os.environ.get('OPENAI_API_KEY', 'YOUR OPENAI API KEY'),
44
  openai_api_base=os.environ.get('OPENAI_API_BASE', 'https://api.openai.com/v1/chat/completions'),
45
  )
 
63
  stop_words=['<|im_end|>'])
64
 
65
  internlm_silicon = dict(type=GPTAPI,
66
+ model_type=os.environ.get('SILICON_MODEL', 'internlm/internlm2_5-7b-chat'),
67
  key=os.environ.get('SILICON_API_KEY', 'YOUR SILICON API KEY'),
68
  openai_api_base='https://api.siliconflow.cn/v1/chat/completions',
69
  meta_template=[
requirements.txt CHANGED
@@ -10,3 +10,4 @@ sse-starlette
10
  termcolor
11
  transformers==4.41.0
12
  uvicorn
 
 
10
  termcolor
11
  transformers==4.41.0
12
  uvicorn
13
+ python-dotenv