(An Interface to OpenAI GPT-3.5-turbo & SQL Databases)
This project provides a way to use OpenAI's GPT-3.5-turbo language model in combination with SQL databases, enabling the powerful and intuitive language processing of OpenAI to interact with databases directly. This is achieved using the langchain
module which includes classes like OpenAI
, SQLDatabase
, and SQLDatabaseChain
.
Before starting with this project, ensure you have the following:
- Python 3.6 or newer
- OpenAI's Python package
- MySQL Connector Python (Optional if using a MySQL database)
- SQLite database or MySQL database
Install necessary packages using pip:
pip install openai
pip install langchain
pip install mysql-connector-python
- Initialize OpenAI model: Initialize the OpenAI model using the
OpenAI
class fromlangchain
module. It requires an API key that can be retrieved from your OpenAI account.initialize your openai-api-key in terminal.
export OPENAI_API_KEY='your own api key'
-
Connect to Database: Create an instance of the
SQLDatabase
object that connects to the SQLite database or MySQL database using thefrom_uri
method. -
Create SQLDatabaseChain object: Create an instance of the
SQLDatabaseChain
object that connects the OpenAI model to theSQLDatabase
.
After setup, run a while loop to take in user input. If the user types "quit", the loop will break. Otherwise, it will run the input question through the SQLDatabaseChain
object which will interact with the database using the OpenAI model. Here's the code:
while True:
question=input("Ask a question: ")
if question=="quit":
break
db_chain.run(question)
The OpenAI model will interpret the question and return a relevant SQL query, which will then be executed on the connected SQL database. This functionality is encapsulated in the SQLDatabaseChain.run()
method.
This project is a powerful tool that leverages the abilities of OpenAI's language models to interact with databases in an intuitive, conversational manner. It can be very useful for performing complex database operations without needing to write complicated SQL queries.
This project is in a development phase. We encourage users to report any issues and contribute to the development by submitting pull requests.
For more information, please visit the project homepage.
This project is licensed under the MIT License - see the LICENSE.md file for details.
- Thanks to OpenAI for providing the GPT-3.5-turbo model.
- Thanks to users who contribute to the project's improvement.