Skip to content

Commit

Permalink
Pull Request: Integrate .env File Support and Add Backend Usage Examp…
Browse files Browse the repository at this point in the history
…le (InternLM#187)

* 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
  • Loading branch information
Jiayou-Chao authored Sep 9, 2024
1 parent 4bd0e24 commit 98fd84d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OPENAI_API_KEY=
OPENAI_API_BASE=
OPENAI_MODEL=
SILICON_API_KEY=
SILICON_MODEL=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.env
temp
65 changes: 40 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,6 @@ English | [简体中文](README_zh-CN.md)

## ✨ MindSearch: Mimicking Human Minds Elicits Deep AI Searcher

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:

- 🤔 **Ask everything you want to know**: MindSearch is designed to solve any question in your life and use web knowledge.
- 📚 **In-depth Knowledge Discovery**: MindSearch browses hundreds of web pages to answer your question, providing deeper and wider knowledge base answer.
- 🔍 **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.
- 💻 **Optimized UI Experience**: Providing all kinds of interfaces for users, including React, Gradio, Streamlit and Terminal. Choose any type based on your need.
- 🧠 **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.

<div align="center">

<img src="assets/teaser.gif">

</div>

## ⚡️ MindSearch vs other AI Search Engines

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\*.

<div align="center">
<img src="assets/mindsearch_openset.png" width="90%">
</div>
* All experiments are done before July.7 2024.

## ⚽️ Build Your Own MindSearch

### Step1: Dependencies Installation
Expand All @@ -48,7 +25,16 @@ cd MindSearch
pip install -r requirements.txt
```

### Step2: Setup MindSearch API
### Step2: Setup Environment Variables

Before setting up the API, you need to configure environment variables. Rename the `.env.example` file to `.env` and fill in the required values.

```bash
mv .env.example .env
# Open .env and add your keys and model configurations
```

### Step3: Setup MindSearch API

Setup FastAPI Server.

Expand All @@ -69,7 +55,7 @@ python -m mindsearch.app --lang en --model_format internlm_server --search_engin

Please set your Web Search engine API key as the `WEB_SEARCH_API_KEY` environment variable unless you are using `DuckDuckGo`.

### Step3: Setup MindSearch Frontend
### Step4: Setup MindSearch Frontend

Providing following frontend interfaces,

Expand Down Expand Up @@ -104,6 +90,35 @@ python frontend/mindsearch_gradio.py
streamlit run frontend/mindsearch_streamlit.py
```

## 🌐 Change Web Search API

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:

- `GoogleSearch`
- `DuckDuckGoSearch`
- `BraveSearch`
- `BingSearch`

For example, to change to the Brave Search API, you would configure it as follows:

```python
BingBrowser(
searcher_type='BraveSearch',
topk=2,
api_key=os.environ.get('BRAVE_API_KEY', 'YOUR BRAVE API')
)
```

## 🐞 Using the Backend Without Frontend

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.

```bash
python backend_example.py
```

Make sure you have set up the environment variables and the backend is running before executing the script.

## 🐞 Debug Locally

```bash
Expand Down
34 changes: 34 additions & 0 deletions backend_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json
import requests

# Define the backend URL
url = 'http://localhost:8002/solve'
headers = {'Content-Type': 'application/json'}

# Function to send a query to the backend and get the response
def get_response(query):
# Prepare the input data
data = {'inputs': [{'role': 'user', 'content': query}]}

# Send the request to the backend
response = requests.post(url, headers=headers, data=json.dumps(data), timeout=20, stream=True)

# Process the streaming response
for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b'\n'):
if chunk:
decoded = chunk.decode('utf-8')
if decoded == '\r':
continue
if decoded[:6] == 'data: ':
decoded = decoded[6:]
elif decoded.startswith(': ping - '):
continue
response_data = json.loads(decoded)
agent_return = response_data['response']
node_name = response_data['current_node']
print(f"Node: {node_name}, Response: {agent_return['response']}")

# Example usage
if __name__ == '__main__':
query = "What is the weather like today in New York?"
get_response(query)
7 changes: 5 additions & 2 deletions mindsearch/agent/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
from dotenv import load_dotenv

from lagent.llms import (GPTAPI, INTERNLM2_META, HFTransformerCasualLM,
LMDeployClient, LMDeployServer)

load_dotenv()

internlm_server = dict(type=LMDeployServer,
path='internlm/internlm2_5-7b-chat',
model_name='internlm2',
Expand Down Expand Up @@ -36,7 +39,7 @@
stop_words=['<|im_end|>'])
# openai_api_base needs to fill in the complete chat api address, such as: https://api.openai.com/v1/chat/completions
gpt4 = dict(type=GPTAPI,
model_type='gpt-4-turbo',
model_type=os.environ.get('OPENAI_MODEL', 'gpt-4o'),
key=os.environ.get('OPENAI_API_KEY', 'YOUR OPENAI API KEY'),
openai_api_base=os.environ.get('OPENAI_API_BASE', 'https://api.openai.com/v1/chat/completions'),
)
Expand All @@ -60,7 +63,7 @@
stop_words=['<|im_end|>'])

internlm_silicon = dict(type=GPTAPI,
model_type='internlm/internlm2_5-7b-chat',
model_type=os.environ.get('SILICON_MODEL', 'internlm/internlm2_5-7b-chat'),
key=os.environ.get('SILICON_API_KEY', 'YOUR SILICON API KEY'),
openai_api_base='https://api.siliconflow.cn/v1/chat/completions',
meta_template=[
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ sse-starlette
termcolor
transformers==4.41.0
uvicorn
python-dotenv

0 comments on commit 98fd84d

Please sign in to comment.