Search code examples
langchainlarge-language-model

How to ensure that the langchain generates the correct output and is not random?


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:

  • Response1: This table does not exist in the provided database schema.
  • Response2: SELECT * FROM information_schema.tables WHERE table_name = 'student' LIMIT 1;
  • Response3: This question cannot be answered directly using the existing tables provided in the database schema. To create a new table named "student", you can use the following SQL query:
CREATE TABLE student (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT NOT NULL,
    age INTEGER,
    major TEXT
);

Solution

  • 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:

    https://api.python.langchain.com/en/latest/_modules/langchain/chains/sql_database/query.html#create_sql_query_chain

    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.