I am using the below code and for the same question, it return different results, is there any way to fix that?
from langchain.chains import create_sql_query_chain
from langchain_openai import ChatOpenAI
from langchain_community.utilities import SQLDatabase
import os
def return_query(question)
db = SQLDatabase.from_uri(os.getenv("POSTGRES_URL"))
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
chain = create_sql_query_chain(llm, db)
response = chain.invoke({"question": question})
return response
Example my question is "create table student" and i get the below responses on re-trying the same code:
CREATE TABLE student (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
age INTEGER,
major TEXT
);
Firstly, I'd like to point out that Large Language Models (LLMs) produce non-deterministic outputs, meaning that for every query, you may get different results. This is not a bug, it is feature.
The two most crucial terms for achieving deterministic outcomes with LLMs are the "prompt" and the "function calling".
Regarding your question, check the source code:
The SQLDatabase class provides a get_table_info method that can be used to get column information as well as sample data from the table.
This functionality does not extend to "write" operations. If you want your LLM to produce the same result when you query "create table", you can use the create_sql_agent method found at https://python.langchain.com/docs/use_cases/sql/agents. This method enables the creation of a tool (function calling in Langchain) with a function that can generate a table in the database with the necessary parameters provided.