adding special instruction to llm model.
Labels: name: auto:question, name: area: models

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.

### Example Code


here is my code:
import sqlalchemy as sal 
import os, sys, openai
import pandas as pd
import constants 
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain_experimental.sql import SQLDatabaseChain
from sqlalchemy import create_engine

from langchain.chat_models import ChatOpenAI 
from typing import List, Optional

from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.callbacks.manager import CallbackManagerForToolRun
from langchain.chat_models import ChatOpenAI
from langchain_experimental.plan_and_execute import (
    PlanAndExecute,
    load_agent_executor,
    load_chat_planner,
)
from langchain.sql_database import SQLDatabase
from langchain.text_splitter import TokenTextSplitter
from langchain.tools import BaseTool
from langchain.tools.sql_database.tool import QuerySQLDataBaseTool
from secret_key import openapi_key 
from langchain.prompts import PromptTemplate


os.environ['OPENAI_API_KEY'] = openapi_key

from urllib.parse import quote_plus
server_name = constants.server_name
database_name = constants.database_name
username = constants.username
password = constants.password
encoded_password = quote_plus(password)
connection_uri = f"mssql+pyodbc://{username}:{encoded_password}@{server_name}/{database_name}?driver=ODBC+Driver+17+for+SQL+Server"

engine = create_engine(connection_uri)

def chat(question, sql_format):

    greetings = ["hi", "hello", "hey"]
    
    if question.lower() in greetings:
        return "Hello! How can I assist you today?"
    
    model_name="gpt-3.5-turbo-16k"
    db = SQLDatabase(engine, view_support=True, include_tables=['EGV_emp_departments_ChatGPT'])
    llm = ChatOpenAI(temperature=0, verbose=False, model=model_name)
    answer = None
    
    if sql_format==False:
        
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
        
        answer = db_chain.run(question)
        
    
    else:
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True , return_sql =True)
        
        sql_query = db_chain.run(question) 
        print("SQLQuery: "+str(sql_query))
        # result = engine.execute(sql_query)
        
        result_df = pd.read_sql(sql_query, engine)
        if result_df.empty:
            return "No results found"
        
        answer = result_df.to_dict()

    PROMPT = """ 
    Given an input question, first create a syntactically correct mssql query to run,  
    then look at the results of the query and return the answer.Return the answer in a sentence form alsways.
    The question: {db_chain.run}
    """
    prompt_template = """
    Use the following pieces of context to answer the question at the end. If you don't know the answer, 
    just say that you don't know, don't try to make up an answer. Return the answer in a sentence form alsways.

    {context}

    Question: {question}
    Helpful Answer:"""

    def handle_greetings(context, question):
        greetings = ["hi", "hello", "hey"]  
        if any(greeting in question.lower() for greeting in greetings):
            return "Hello! How can I assist you today?"
        else:
            return None

    PROMPT = PromptTemplate(
        template=prompt_template, 
        input_variables=["context", "question"],
        preprocessor=handle_greetings
    )

    def split_text(text, chunk_size, chunk_overlap=0):
        text_splitter = TokenTextSplitter(
            chunk_size=chunk_size, chunk_overlap=chunk_overlap
        )
        yield from text_splitter.split_text(text)


    class QuerySQLDatabaseTool2(QuerySQLDataBaseTool):
        def _run(
            self,
            query: str,
            run_manager: Optional[CallbackManagerForToolRun] = None,
        ) -> str:
            result = self.db.run_no_throw(query)

            return next(split_text(result, chunk_size=14_000))


    class SQLDatabaseToolkit2(SQLDatabaseToolkit):
        def get_tools(self) -> List[BaseTool]:
            tools = super().get_tools()
            original_query_tool_description = tools[0].description
            new_query_tool = QuerySQLDatabaseTool2(
                db=self.db, description=original_query_tool_description
            )
            tools[0] = new_query_tool

            return tools
    
    # return db_chain.run(question) 
    return answer

def chain1(question):
    text = chat(question,False)
    
    return text
    
    
def chain2(question):
    query = chat(question,True)
    
    return query

# answer=chat("what is Ganesh D desigation")
# print(answer)
    
# Define a function named chain1 that uses the chat function without SQL format
def chain1(question):
    text = chat(question, False)
    return text

# Define a function named chain2 that uses the chat function with SQL format
def chain2(question):
    query = chat(question, True)
    return query


chain1("give me aadhaar number of all Jayachitra R") #, give answer in sentence form")
print(chain1)

### Description

how to added instruction in the llm model to personalize the output request by modify the prompt here or add other way to tell the llm model to give the answer in sentence form or any other instructions. 

here is my code:
import sqlalchemy as sal 
import os, sys, openai
import pandas as pd
import constants 
from langchain.sql_database import SQLDatabase
from langchain.llms.openai import OpenAI
from langchain_experimental.sql import SQLDatabaseChain
from sqlalchemy import create_engine

from langchain.chat_models import ChatOpenAI 
from typing import List, Optional

from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.callbacks.manager import CallbackManagerForToolRun
from langchain.chat_models import ChatOpenAI
from langchain_experimental.plan_and_execute import (
    PlanAndExecute,
    load_agent_executor,
    load_chat_planner,
)
from langchain.sql_database import SQLDatabase
from langchain.text_splitter import TokenTextSplitter
from langchain.tools import BaseTool
from langchain.tools.sql_database.tool import QuerySQLDataBaseTool
from secret_key import openapi_key 
from langchain.prompts import PromptTemplate


os.environ['OPENAI_API_KEY'] = openapi_key

from urllib.parse import quote_plus
server_name = constants.server_name
database_name = constants.database_name
username = constants.username
password = constants.password
encoded_password = quote_plus(password)
connection_uri = f"mssql+pyodbc://{username}:{encoded_password}@{server_name}/{database_name}?driver=ODBC+Driver+17+for+SQL+Server"

engine = create_engine(connection_uri)

def chat(question, sql_format):

    greetings = ["hi", "hello", "hey"]
    
    if question.lower() in greetings:
        return "Hello! How can I assist you today?"
    
    model_name="gpt-3.5-turbo-16k"
    db = SQLDatabase(engine, view_support=True, include_tables=['EGV_emp_departments_ChatGPT'])
    llm = ChatOpenAI(temperature=0, verbose=False, model=model_name)
    answer = None
    
    if sql_format==False:
        
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
        
        answer = db_chain.run(question)
        
    
    else:
        db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True , return_sql =True)
        
        sql_query = db_chain.run(question) 
        print("SQLQuery: "+str(sql_query))
        # result = engine.execute(sql_query)
        
        result_df = pd.read_sql(sql_query, engine)
        if result_df.empty:
            return "No results found"
        
        answer = result_df.to_dict()

    PROMPT = """ 
    Given an input question, first create a syntactically correct mssql query to run,  
    then look at the results of the query and return the answer.Return the answer in a sentence form alsways.
    The question: {db_chain.run}
    """
    prompt_template = """
    Use the following pieces of context to answer the question at the end. If you don't know the answer, 
    just say that you don't know, don't try to make up an answer. Return the answer in a sentence form alsways.

    {context}

    Question: {question}
    Helpful Answer:"""

    def handle_greetings(context, question):
        greetings = ["hi", "hello", "hey"]  
        if any(greeting in question.lower() for greeting in greetings):
            return "Hello! How can I assist you today?"
        else:
            return None

    PROMPT = PromptTemplate(
        template=prompt_template, 
        input_variables=["context", "question"],
        preprocessor=handle_greetings
    )

    def split_text(text, chunk_size, chunk_overlap=0):
        text_splitter = TokenTextSplitter(
            chunk_size=chunk_size, chunk_overlap=chunk_overlap
        )
        yield from text_splitter.split_text(text)


    class QuerySQLDatabaseTool2(QuerySQLDataBaseTool):
        def _run(
            self,
            query: str,
            run_manager: Optional[CallbackManagerForToolRun] = None,
        ) -> str:
            result = self.db.run_no_throw(query)

            return next(split_text(result, chunk_size=14_000))


    class SQLDatabaseToolkit2(SQLDatabaseToolkit):
        def get_tools(self) -> List[BaseTool]:
            tools = super().get_tools()
            original_query_tool_description = tools[0].description
            new_query_tool = QuerySQLDatabaseTool2(
                db=self.db, description=original_query_tool_description
            )
            tools[0] = new_query_tool

            return tools
    
    # return db_chain.run(question) 
    return answer

def chain1(question):
    text = chat(question,False)
    
    return text
    
    
def chain2(question):
    query = chat(question,True)
    
    return query

# answer=chat("what is Ganesh D desigation")
# print(answer)
    
# Define a function named chain1 that uses the chat function without SQL format
def chain1(question):
    text = chat(question, False)
    return text

# Define a function named chain2 that uses the chat function with SQL format
def chain2(question):
    query = chat(question, True)
    return query


chain1("give me aadhaar number of all Jayachitra R") #, give answer in sentence form")
print(chain1)

### System Info


python = 3.11
langchain= latest

### Related Components

- [X] LLMs/Chat Models
- [ ] Embedding Models
- [X] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [X] Chains
- [ ] Callbacks/Tracing
- [ ] Async