This example demonstrates how to use GenSX's useDatabase hook to create and query a SQL database. The workflow provides a basic text-to-SQL example where an LLM translates a user's questions into SQL queries to get the results. The data is stored in GenSX Cloud database storage.
The example consists of two main components:
InitializeDatabase: Creates and populates a database with baseball statistics and schemaTextToSqlWorkflow: Processes natural language questions about baseball statistics and translates them into SQL queries to get the results
Here's what happens when you run the workflow:
- The system checks if the baseball database exists and initializes it if needed
- Your question is processed by an agent that converts it to SQL
- The SQL query is executed against the database
- The results are formatted and displayed
-
Log in to GenSX (if you haven't already):
npx gensx login
-
Install dependencies:
pnpm install
-
Set up your environment variables:
export OPENAI_API_KEY=your_api_key_here
To run the workflow in GenSX Cloud:
-
Deploy your workflow:
pnpm run deploy
-
Initialize the database:
gensx run InitializeDatabase
-
Ask questions about baseball statistics:
gensx run TextToSqlWorkflow --input '{"question": "Who has the highest batting average?"}'
Once deployed, you can go to the GenSX console to see your workflows, test them, analyze traces, and get code snippets.
You can run the workflow directly:
pnpm dev "Who has the highest batting average?"This will automatically initialize the database and run the workflow.
You can also test the workflow through a local API server:
pnpm startThis will start a local API server and you can call the workflow APIs via curl or any HTTP client:
Initialize the database first:
curl -X POST http://localhost:1337/workflows/InitializeDatabase \
-H "Content-Type: application/json" \
-d '{}'Then run the workflow:
curl -X POST http://localhost:1337/workflows/TextToSqlWorkflow \
-H "Content-Type: application/json" \
-d '{
"question": "Who has the highest batting average?"
}'A swagger UI will also be available at http://localhost:1337/swagger-ui to view the API details and test the workflow.