langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.12k stars 14.97k forks source link

not able to add few shots example to create_sql_agent #16731

Closed akashkumar398 closed 2 months ago

akashkumar398 commented 8 months ago

Checked other resources

Example Code

i want to add few shots example to make my prompts better to understand the complex questions to create_sql_agent

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent( prefix=MSSQL_AGENT_PREFIX, format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS, llm=llm, toolkit=toolkit, top_k=30, verbose=True )

Error Message and Stack Trace (if applicable)

i want to add few shots example to make my prompts better to understand the complex questions to create_sql_agent

toolkit = SQLDatabaseToolkit(db=db, llm=llm)

agent_executor = create_sql_agent( prefix=MSSQL_AGENT_PREFIX, format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS, llm=llm, toolkit=toolkit, top_k=30, verbose=True )

Description

i want to add FewShotPromptTemplate to my agent_executor

examples = [ {"input": "List all artists.", "query": "SELECT FROM Artist;"}, { "input": "Find all albums for the artist 'AC/DC'.", "query": "SELECT FROM Album WHERE ArtistId = (SELECT ArtistId FROM Artist WHERE Name = 'AC/DC');", }, { "input": "List all tracks in the 'Rock' genre.", "query": "SELECT * FROM Track WHERE GenreId = (SELECT GenreId FROM Genre WHERE Name = 'Rock');", }]

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

agent_executor = create_sql_agent( prefix=MSSQL_AGENT_PREFIX, format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS, llm=llm, toolkit=toolkit, top_k=30, verbose=True )

System Info

langchain version 0.0.352

dosubot[bot] commented 8 months ago

🤖

Hello again, @akashkumar398! Nice to see you keep exploring the LangChain framework. Let's dive into your issue.

Based on the information provided, it seems you want to add a FewShotPromptTemplate to your agent_executor in LangChain. Here's how you can do it:

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

# Define your examples
examples = [
    {"input": "List all artists.", "query": "SELECT * FROM Artist;"},
    {
        "input": "Find all albums for the artist 'AC/DC'.",
        "query": "SELECT * FROM Album WHERE ArtistId = (SELECT ArtistId FROM Artist WHERE Name = 'AC/DC');",
    },
    {
        "input": "List all tracks in the 'Rock' genre.",
        "query": "SELECT * FROM Track WHERE GenreId = (SELECT GenreId FROM Genre WHERE Name = 'Rock');",
    }
]

# Define the prompt template for each example
example_prompt = PromptTemplate.from_messages(
    [('human', '{input}'), ('ai', '{query}')]
)

# Create the FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="You are a helpful AI Assistant",
    suffix="{input}",
    example_separator="\n\n",
    template_format="f-string",
    validate_template=True,
    input_variables=["input"]
)

# Add the FewShotPromptTemplate to your agent_executor
agent_executor.add_prompt(few_shot_prompt)

In this example, we first define a list of examples. Each example is a dictionary with an "input" and "query". Then, we define a PromptTemplate for each example. This template specifies how each example should be formatted.

Next, we create the FewShotPromptTemplate. We pass the examples and the example_prompt to the constructor. We also specify a prefix and a suffix for the prompt, as well as a separator to use between the examples. We choose to use the "f-string" format for the template and enable template validation. Finally, we specify the input variables that the prompt template expects.

Lastly, we add the FewShotPromptTemplate to the agent_executor. This will allow the agent_executor to use the FewShotPromptTemplate when generating responses.

However, I wasn't able to find any information on whether the create_sql_agent function in LangChain supports the addition of a FewShotPromptTemplate directly. It's possible that this feature is not yet implemented or that I may have missed it. If you have more information or if there's something I misunderstood, please let me know.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

akashkumar398 commented 8 months ago

I am getting this error now: type object ‘PromptTemplate’ has no attribute ‘from_messages’

jatinchawda1503 commented 8 months ago

@akashkumar398, You can try like this,

example_prompt = PromptTemplate(
    input_variables=["question", "answer"], template="Question: {question}\n{answer}"
)

prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    suffix="Question: {input}",
    input_variables=["input"],
)

create_sql_agent(prompt=prompt)

Please refer : https://api.python.langchain.com/en/stable/agent_toolkits/langchain_community.agent_toolkits.sql.base.create_sql_agent.html#langchain_community.agent_toolkits.sql.base.create_sql_agent

tombeteende commented 6 months ago

@jatinchawda1503 unfortunately, this does not work as it expects to obtain agent_scratchpad variable

jatinchawda1503 commented 6 months ago

@tombeteende, you can refer this Use case for sql agent and Use case for sql prompting for better understanding