Passing custom object to SQLDatabaseChain from AgentExecutor, along with input #25753
Replies: 1 comment 2 replies
-
To pass a custom object called Here is an example of how you can do this: from langchain_openai import ChatOpenAI
from langchain_community.agent_toolkits import create_sql_agent
from langchain_community.utilities import SQLDatabase
# Create your custom object
cv = CustomObject()
# Initialize the database and language model
db = SQLDatabase.from_uri("sqlite:///Chinook.db")
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# Create the SQL agent and pass the custom object via agent_executor_kwargs
agent_executor = create_sql_agent(
llm,
db=db,
agent_type="tool-calling",
verbose=True,
agent_executor_kwargs={"cv": cv}
) In this example, Additionally, ensure that your # Define the prompt template
prompt_template = """
You have access to the following sql tables:
{table_info}
- 'gene_expression_1' for gene expression when species is homo sapiens.
- 'gene_expression_2' for gene expression when species is mus musculus.
- 'gene_expression_3' for gene expression only when species is homo sapiens and there is a disease involved.
Given an input question:
{input}
Create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Use values in {cv} to generate SQL queries
Use the following format:
Question: original user question
SQLQuery: SQL Query to run
SQLResult: sql result
Answer: final answer
Question: {input}
"""
# Create the prompt template
sql_prompt = PromptTemplate(
input_variables=["input", "cv", "table_info", "dialect"],
template=prompt_template
)
# Create the SQLDatabaseChain
sql_db_chain = SQLDatabaseChain.from_llm(
llm=get_llm(),
db=db,
verbose=True,
top_k=100,
prompt=sql_prompt,
return_intermediate_steps=True
) This setup ensures that the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
I have created a langchain agent with mulitple tools.
These tools include a SQL tool which uses SQLDatabaseChain
Along with the input i want to pass an object called 'cv' to the agent executor, which should then pass it to the Sql tool to use in its prompt. Cv is a dict object converted to string.
My agent executor is called 'agent_executor'
The prompt template of my SQL tool should look like this:
I have tried the following, but it does not recognize the cv object:
Is there any way to achieve this without changing langchain source code?
System Info
Langchain 0.2.3
Beta Was this translation helpful? Give feedback.
All reactions