Commit 39221fa2 authored by Rajesham Suddala's avatar Rajesham Suddala

initial check-in.

parents
from marshal import loads
from fastapi import FastAPI
import google.generativeai as genai
import os
import mysql.connector
from dotenv import load_dotenv
from mysql.connector import Error
import json
app = FastAPI()
load_dotenv()
@app.get("/search/")
def get_student(question : str):
response = get_gemini_response(question, prompt)
response = json.loads(response)
print(response)
results = connect_to_mysql(response.get("Query"))
return results
genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))
def get_gemini_response(question, prompt):
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content([prompt, question])
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ,response)
return response.text
prompt ='''
Create an SQL query based on the below given text, you need to identify relevant attributes and then construct the query to match those attributes in Products table. Here’s how to break it down:
Use Schema: “ecom_db”
Products table exist in the "ecom_db" schema
Please find below instructions to build the sql query:
PRODUCTS table is consist of below columns:
["id", "category", "price", "age", "color", "size", "gender”, “brand”];
Use below possible filterOptions to understand and extract the data to construct the query:
{
"filterOptions": {
"category": ["Sarees", "Kurthas", "Footwear", "Electronics", "Shirts", "Frocks"],
"price": [100, 40, 400],
"age": ["Kids", "Adults"],
"color": ["Red", "Blue", "Green", "Black", "White"],
"size": ["S", "M", "L", "XL"],
"gender": ["Male", "Female", "Unisex”],
"brand": [“Levi, “Denim”, “Allen Solly”]
}
}
Note: If you don't understand how to map the given keywords to any filterOptions , please ignore it
Expected Response: Give the response as a json object
Example 1:
Text : “Men Blue Jacket”
Response:
{
"Input Text": "Men Blue Jacket",
"appliedFilters": {
"category": "Jacket",
"color": "Blue",
"gender": "Male"
},
"Query": "SELECT * FROM ecom_db.Products WHERE category = 'Jacket' AND color = 'Blue' AND gender = 'Male';"
}
Text query would be the question
'''
def connect_to_mysql(query):
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",query)
try:
# Establish the connection
connection = mysql.connector.connect(
host='localhost', # Replace with your DB host
database='ecom_db', # Replace with your DB name
user='root', # Replace with your MySQL username
password='root' # Replace with your MySQL password
)
if connection.is_connected():
print("Connected to MySQL database")
# Run a simple query
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall() # Fetch all rows from the query result
return result
except Error as e:
print(f"Error while connecting to MySQL: {e}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment