Created
September 19, 2023 14:26
-
-
Save fsndzomga/cbe8927dd55c2a4f0f3319cee101d86f to your computer and use it in GitHub Desktop.
text to sql prompt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain.utilities import SQLDatabase | |
from langchain.chat_models import ChatOpenAI | |
from langchain.schema.output_parser import StrOutputParser | |
from langchain import hub | |
from keys import OPENAI_API_KEY | |
import os | |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY | |
# Initialize database | |
db = SQLDatabase.from_uri("sqlite:///Chinook.db") | |
# Pull down prompt | |
prompt = hub.pull("rlm/text-to-sql") | |
# Initialize model | |
model = ChatOpenAI() | |
# Create chain with LangChain Expression Language | |
inputs = { | |
"table_info": lambda x: db.get_table_info(), | |
"input": lambda x: x["question"], | |
"few_shot_examples": lambda x: "", | |
"dialect": lambda x: db.dialect, | |
} | |
sql_response = ( | |
inputs | |
| prompt | |
| model.bind(stop=["\nSQLResult:"]) | |
| StrOutputParser() | |
) | |
# Call with a given question | |
response = sql_response.invoke({"question": "How many customers are there?"}) | |
print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment