RedmindGPT / app.py
lakshmivairamani's picture
Update app.py
0549e31 verified
raw
history blame
7.18 kB
import os
import requests
import gradio as gr
from langchain.memory import ConversationBufferMemory # Updated import
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.utilities import SQLDatabase
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor, Tool
from langchain.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from PyPDF2 import PdfReader
# Initialize the memory
memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")
open_api_key_token = os.environ['OPEN_AI_API']
open_weather_api_key = os.environ['OPEN_WEATHER_API']
os.environ['OPENAI_API_KEY'] = open_api_key_token
db_uri = 'postgresql+psycopg2://postgres:postpass@193.203.162.39:5432/warehouse'
# Database setup
db = SQLDatabase.from_uri(db_uri)
# LLM setup
llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
# Define the SQL query generation tool
template_query_generation = """Based on the table schema below, write a SQL query that would answer the user's question:
{schema}
Question: {question}
SQL Query:"""
prompt_query_generation = ChatPromptTemplate.from_template(template_query_generation)
def get_schema(_):
return db.get_table_info()
def generate_sql_query(question):
schema = get_schema(None)
input_data = {"question": question}
sql_chain = (RunnablePassthrough.assign(schema=get_schema)
| prompt_query_generation
| llm.bind(stop="\n SQL Result:")
| StrOutputParser()
)
return sql_chain.invoke(input_data)
def run_query(query):
return db.run(query)
# Define the database query tool
def database_tool(question):
sql_query = generate_sql_query(question)
return run_query(sql_query)
# Define the weather data retrieval tool
def get_weather_data(location="United Arab Emirates"):
api_key = open_weather_api_key
base_url = "http://api.openweathermap.org/data/2.5/weather?"
if location is None or location.strip() == "":
location = "United Arab Emirates"
complete_url = f"{base_url}q={location}&appid={api_key}&units=metric"
response = requests.get(complete_url)
data = response.json()
if data["cod"] != "404":
main = data["main"]
weather_description = data["weather"][0]["description"]
temperature = main["temp"]
return f"The current temperature in {location} is {temperature}°C with {weather_description}."
else:
return "Weather data is not found."
#get_weather_data("United Arab Emirates")
# Define the document data tool
def load_and_split_pdf(pdf_path):
reader = PdfReader(pdf_path)
text = ''
for page in reader.pages:
text += page.extract_text()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
texts = text_splitter.split_text(text)
return texts
def create_vector_store(texts):
embeddings = OpenAIEmbeddings()
vector_store = FAISS.from_texts(texts, embeddings)
return vector_store
def query_vector_store(vector_store, query):
docs = vector_store.similarity_search(query)
return '\n\n'.join([doc.page_content for doc in docs])
# Load and process the PDF (ensure the PDF is accessible from your Colab environment)
pdf_path = "/content/drive/My Drive/Colab Notebooks/LLM/data/PDF/medical_information.pdf"
# Check if the user has the necessary permissions to access the directory
# if not os.path.isdir(pdf_path):
# raise ValueError(f"Directory {pdf_path} does not exist or you do not have permission to access it.")
texts = load_and_split_pdf(pdf_path)
vector_store = create_vector_store(texts)
def document_data_tool(query):
return query_vector_store(vector_store, query)
# Initialize the agent with the tools
tools = [
Tool(name="DatabaseQuery", func=database_tool, description="Tool to query the database based on the user's question. Only handles questions related to the collegedb schema, including tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook. Ensure to use only the available fields in these tables.", tool_choice="required"),
Tool(name="WeatherData", func=get_weather_data, description="Tool to get current weather data for a city or country. Handles questions related to current weather conditions in specific cities or countries.", tool_choice="required"),
Tool(name="DocumentData", func=document_data_tool, description="Tool to search and retrieve information from the uploaded document.", tool_choice="required"),
]
prompt_template = f"""You are an assistant that helps with database queries, weather information, and document retrieval.
For SQL database-related questions, only use the fields available in the collegedb schema, which includes tables such as buildings, classrooms, college, course, faculty, interns, person, section, student, and textbook.
For weather-related questions, if the user specifies a city, provide the weather information for that city. If the user specifies a country or does not specify a city, provide the weather information for the specified country or the default location of 'United Arab Emirates'.
For document-related questions, search and retrieve information from the uploaded document.
{{agent_scratchpad}}
Question: {{input}}
{memory.buffer}
"""
prompt = ChatPromptTemplate.from_template(prompt_template)
# Initialize the agent with memory
llm_with_memory = llm.bind(memory=memory)
agent = create_tool_calling_agent(llm_with_memory, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, memory= memory, verbose=True)
# Define the interface function
max_iterations = 5
iterations = 0
def answer_question(user_question, city="United Arab Emirates"):
global iterations
iterations = 0
while iterations < max_iterations:
response = agent_executor.invoke({"input": user_question})
if isinstance(response, dict):
response_text = response.get("output", "")
else:
response_text = response
if "invalid" not in response_text.lower():
break
iterations += 1
if iterations == max_iterations:
return "The agent could not generate a valid response within the iteration limit."
# Print memory buffer for debugging
print("Memory Buffer:", memory.buffer)
# Print memory buffer for debugging
print("Memory Buffer11:", memory.load_memory_variables({}))
# Format the response text
response_text = response_text.replace('\n', ' ').replace(' ', ' ').strip()
return response_text
# Create the Gradio interface
iface = gr.Interface(
fn=answer_question,
inputs="text",
outputs="text",
title="Chat with Database and Weather",
description="Ask a question about the database and get a response in natural language, including current weather information."
)
# Launch the Gradio interface
iface.launch(share=True, debug=True)