Noah Nsimbe commited on
Commit
87900c5
1 Parent(s): dc17691

Add application file

Browse files
Files changed (2) hide show
  1. app.py +41 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain import SQLDatabase
3
+ from langchain_openai import ChatOpenAI
4
+ from langchain_experimental.sql import SQLDatabaseChain
5
+ import os
6
+
7
+ DB_USERNAME = os.environ.get("DB_USERNAME")
8
+ DB_PASSWORD = os.environ.get("DB_PASSWORD")
9
+ DB_HOST = os.environ.get("DB_HOST")
10
+ DB_PORT = os.environ.get("DB_PORT")
11
+ DB_NAME = os.environ.get("DB_NAME")
12
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
13
+ GPT_MODEL = "gpt-3.5-turbo-0125"
14
+
15
+
16
+ pg_uri = (
17
+ f"postgresql+psycopg2://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
18
+ )
19
+ db = SQLDatabase.from_uri(pg_uri)
20
+ llm = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name=GPT_MODEL)
21
+ db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True, top_k=3)
22
+
23
+
24
+ PROMPT = """
25
+ Given an input question, first create a syntactically correct Postgresql query without ```sql formatting in the query.
26
+ Run the query then look at the results of the query.
27
+ Interpret th results and return an answer.
28
+ The question: {question}
29
+ """
30
+
31
+
32
+ def query(question):
33
+ try:
34
+ response = db_chain.run(PROMPT.format(question=question))
35
+ return response
36
+ except:
37
+ return "Could not perform request. Try another one"
38
+
39
+
40
+ iface = gr.Interface(fn=query, inputs="text", outputs="text")
41
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ psycopg2-binary
4
+ langchain_experimental
5
+ langchain-openai