Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatGrid Backend Setup Procedure Update. #113

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ config.py
spack_*
hiop/
solution
myenv/
22 changes: 10 additions & 12 deletions viz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ npm start
```
This will open a webpage with the visualization of the given network.


The figures show the visualization of the synthetic electric grid. The data for developing this visualization was created by merging the synthetic dataset for the [Eastern](https://electricgrids.engr.tamu.edu/electric-grid-test-cases/activsg70k/), [Western](https://electricgrids.engr.tamu.edu/electric-grid-test-cases/activsg10k/), and [Texas](https://electricgrids.engr.tamu.edu/electric-grid-test-cases/activsg2000/) interconnects from the [Electric Grid Test Case Repository](https://electricgrids.engr.tamu.edu/)
### 2D synthetic US western grid network display
![](images/network_viz.PNG)
Expand All @@ -63,6 +64,9 @@ The figures show the visualization of the synthetic electric grid. The data for
### 2.5D synthetic US western grid displaying network, flow, generation, and load
![](images/all_viz.PNG)

### Demo
See [Here](../tutorials/demo1.ipynb)

## ChatGrid
ChatGrid is a natural language query tool for ExaGO visualizations. It is powered by OpenAI GPT-3.5-Turbo and Langchain. ChatGrid allows users to query on ExaGO visualizations through natural language and returns text summaries and visual outputs as answers. The following flow chart shows the architecture design of ChatGrid.
![](images/chatgrid_arch.png)
Expand All @@ -83,7 +87,7 @@ Behind the scenes, LLM translates natural language queries into SQL queries to r

To use the provided script, first copy the ExaGO output `.json` file to the `viz/data` subdirectory and simply run the following script in the `viz/data` subdirectory (replace the example filename with your json filename). This will output three CSV files: `generation.csv`, `bus.csv`, and `tranmission_line.csv`.
```
python dataprocess.py case_ACTIVSg10k.json
python jsontocsv.py case_ACTIVSg10k.json
```

2. Download PostgreSQL database from this [link](https://www.postgresql.org/download/) and install it.
Expand All @@ -103,28 +107,22 @@ Behind the scenes, LLM translates natural language queries into SQL queries to r

4. Connect to your database.

Create the `config.py` file in the `viz/backend` subdirectory and paste the following script into it (replace `YOUR_DATABASE_PASSWORD` with your own database password )

```
sql_key = "YOUR_DATABASE_PASSWORD"
```
Open the `config.py` file in the `viz/backend` subdirectory and replace `YOUR_DATABASE_PASSWORD` and `YOUR_DATABASE_NAME` with your own database password and database name.

Note: Please make sure you use exactly the same file name (i.e., `config.py`) and key name (i.e., 'sql_key') as indicated.

### Getting your OpenAI API key
ChatGrid uses GPT models from OpenAI to process natural language queries. To use LLMs from OpenAI, you first need to go to [OpenAI's Platform website](https://platform.openai.com) and sign in with an OpenAI account. Click your profile icon at the top-right corner of the page and select "View API Keys." Click "Create New Secret Key" to generate a new API key. Open the `config.py` file you just created in the `viz/backend` subdirectory and add the following script into it (replace `YOUR_OPENAI_KEY` with your own OpenAI API key)
ChatGrid uses GPT models from OpenAI to process natural language queries. To use LLMs from OpenAI, you first need to go to [OpenAI's Platform website](https://platform.openai.com) and sign in with an OpenAI account. Click your profile icon at the top-right corner of the page and select "View API Keys." Click "Create New Secret Key" to generate a new API key.

Open the `config.py` in the `viz/backend` subdirectory replace `YOUR_OPENAI_KEY` with your own OpenAI API key.

```
openai_key = "YOUR_OPENAI_KEY"
```

<!-- data script -->
<!-- installation: pip install -r requirements.txt in the backend directory-->
### Launch backend
ChatGrid uses Flask to host the service of receiving user queries and returning the data output and text summaries to update the visualizations on the frontend. Please follow the steps below to run the backend server.

1. Go to the `viz/backend` subdirectory and use the `pip install -r requirements.txt` command to install all the Python dependencies.
2. run the following command in the `viz/backend` subdirectory
2. Run the following command in the `viz/backend` subdirectory
```
python server.py
```
Expand Down
3 changes: 3 additions & 0 deletions viz/backend/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sql_key = "YOUR_DATABASE_PASSWORD"
database_name = "YOUR_DATABASE_NAME"
openai_key = "YOUR_OPENAI_KEY"
4 changes: 2 additions & 2 deletions viz/backend/sqlchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def sqlchain(input_text):
llm = OpenAI(openai_api_key=config.openai_key,
model_name="gpt-3.5-turbo", temperature=0, verbose=True)
db = SQLDatabase.from_uri(
f"postgresql+psycopg2://postgres:{config.sql_key}@localhost:5432/US west power grid")
f"postgresql+psycopg2://postgres:{config.sql_key}@localhost:5432/{config.database_name}")
mydb = sqldb.create_engine(
f"postgresql+psycopg2://postgres:{config.sql_key}@localhost:5432/US west power grid")
f"postgresql+psycopg2://postgres:{config.sql_key}@localhost:5432/{config.database_name}")
myconnection = mydb.connect()

_CUSTOMIZE__TEMPLATE = """You are a PostgreSQL expert. Given an input question, first create a syntactically correct PostgreSQL query to run then look at the results of the query and return the answer to the input question.
Expand Down
Loading