Create prompt when using SQLDatabaseChain

Hi,
I use the SQLDatabaseChain to talk to my SQL Server. I just execute my db_chain.run and ask a question. Can I somehow make a prompt and tell the chatbot that for example “Answe as if you are a psychologist”. And Im having problems with tokenization aswell. Can I have OpenAiEmbeddings and chunksizing in this code too?

This is my code :slight_smile:

import os
import pypyodbc
from langchain.llms import OpenAI
from langchain.sql_database import SQLDatabase
from langchain_experimental.sql import SQLDatabaseChain
from sqlalchemy import create_engine

DRIVER_NAME = ‘SQL SERVER’
SERVER_NAME = ‘DESKTOP-VMQPT58’
DATABASE_NAME = ‘AvtryckMiljoProduction’

def open_connection():
try:
global conn
print(‘Trying to connect to SQL Server…’)
connection_string = f’DRIVER={{{DRIVER_NAME}}};SERVER={SERVER_NAME};DATABASE={DATABASE_NAME};Trusted_Connection=yes;’
conn = pypyodbc.connect(connection_string)
print(‘Connection made!’)
except Exception as e:
print(f’Could not connect: {str(e)}')

def close_connection():
try:
conn.close()
print(‘Connection closed’)
except:
print(‘Connection was never open or already closed’)

Example usage

open_connection()

db = SQLDatabase(create_engine(f"mssql+pyodbc://{SERVER_NAME}/{DATABASE_NAME}?driver={DRIVER_NAME}", use_setinputsizes=False))
llm = OpenAI(api_key=openai_api_key, temperature=0)
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)

db_chain.run(“Give me the names of all customers who got created in 2000”)
close_connection()

Have you tried it with sqldatabasetoolkit instead of sqldatabasechain ?

SQL database chain

SQL database toolkit