DEV Community

Aymen Hmani
Aymen Hmani

Posted on

My "solution" to integrate ChatGPT into any database

So here, I worked on an SQL database implementing the following steps:
1- Export SQL to CSV
2- Clean CSV to TXT
3- Convert to 1-line Database TXT
4- Merge it with the Base Prompt
5- Send the Merge to ChatGPT API

diagram

steps

here my base prompt as an example :

Act as a chatbot that assists the agents of EcoDeliver. Your role is to help EcoDeliver agents effectively utilize the application, provide guidance, answer project-related questions, and offer solutions to challenges specific to EcoDeliver. EcoDeliver is an innovative IoT-powered app for efficient commands delivery management, integrating real-time tracking, user-friendly interfaces, and environmentally friendly practices. You have access to the following database:
{}
Remember that every user who interacts with you is an agent seeking assistance with EcoDeliver-related tasks
here is the message:
Enter fullscreen mode Exit fullscreen mode

then add the 1line_db (NĀ°4 in the pic) to the base prompt between {} and the user message after here is the message:

then send all that to OpenAi api

import openai

openai.api_base = "https://openrouter.ai/api/v1"
openai.api_key = 'get_ur_own_api_key'

user_msg_file_path = r'C:\path\to\the\file\merged.txt'
with open(user_msg_file_path, 'r') as user_msg_file:
    user_msg = user_msg_file.read()

response = openai.ChatCompletion.create(
    model="openai/gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": ""},
        {"role": "user", "content": user_msg},
    ],
    headers={
        "HTTP-Referer": 'http://localhost:3000',
        "X-Title": 'Your App Name',
    },
)

reply_content = response.choices[0].message['content']

response_file_path = r'C:\path\to\the\file\response.txt'
with open(response_file_path, 'w') as response_file:
    response_file.write(reply_content)

print(reply_content)
Enter fullscreen mode Exit fullscreen mode

ET VOILA

prototype

Pros and Cons:

Pros:
Diverse Responses: ChatGPT is a general AI, so you get responses that can't be queried using SQL. For example, you can ask about the distance traveled using just addresses.

Cons:
Costly: Exporting your entire database to ChatGPT for each message can get expensive.
Data Risk: There might be a risk when exporting data, so be cautious about the information you share.

Top comments (0)