langchain-ai / langchain

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

not able to add FewShotPromptTemplate to create_sql_agent #16837

Closed akashkumar398 closed 3 months ago

akashkumar398 commented 9 months ago

Checked other resources

Example Code

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');", }, { "input": "Find the total duration of all tracks.", "query": "SELECT SUM(Milliseconds) FROM Track;", }, { "input": "List all customers from Canada.", "query": "SELECT FROM Customer WHERE Country = 'Canada';", }, { "input": "How many tracks are there in the album with ID 5?", "query": "SELECT COUNT() FROM Track WHERE AlbumId = 5;", }, { "input": "Find the total number of invoices.", "query": "SELECT COUNT() FROM Invoice;", }, { "input": "List all tracks that are longer than 5 minutes.", "query": "SELECT FROM Track WHERE Milliseconds > 300000;", }, { "input": "Who are the top 5 customers by total purchase?", "query": "SELECT CustomerId, SUM(Total) AS TotalPurchase FROM Invoice GROUP BY CustomerId ORDER BY TotalPurchase DESC LIMIT 5;", }, { "input": "Which albums are from the year 2000?", "query": "SELECT FROM Album WHERE strftime('%Y', ReleaseDate) = '2000';", }, { "input": "How many employees are there", "query": 'SELECT COUNT(*) FROM "Employee"', }, ]

We can create a few-shot prompt with them like so:

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate.from_template("User input: {input}\nSQL query: {query}") prompt = FewShotPromptTemplate( examples=examples[:5], example_prompt=example_prompt, prefix="You are a SQLite expert. Given an input question, create a syntactically correct SQLite query to run. Unless otherwise specificed, do not return more than {top_k} rows.\n\nHere is the relevant table info: {table_info}\n\nBelow are a number of examples of questions and their corresponding SQL queries.", suffix="User input: {input}\nSQL query: ", input_variables=["input", "top_k", "table_info"], )

print(prompt.format(input="How many artists are there?", top_k=3, table_info="foo"))

        db_url = URL.create(**db_config)
        db = SQLDatabase.from_uri(db_url)
        toolkit = SQLDatabaseToolkit(db=db, llm=self.llm)
        agent_executor = create_sql_agent(
        prefix=MSSQL_AGENT_PREFIX,
        format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS,
        llm=self.llm,
        toolkit=toolkit,
        callback_manager=self.callbacks,
        verbose=True
    )

        logging.info(f"Login successful: {db_config['username']}")
        response = agent_executor.run(query) 

Error Message and Stack Trace (if applicable)

not able to add FewShotPromptTemplate to create_sql_agent

Description

not able to add FewShotPromptTemplate to create_sql_agent on z=azure openai bot

System Info

langchain==0.0.352 langchain-core==0.1.11 langchain-experimental==0.0.47 langchain-community==0.0.13

dosubot[bot] commented 9 months ago

🤖

Hey @akashkumar398, great to see you diving into some interesting challenges with LangChain again! How's everything going on your end?

Based on the information you've provided, it seems like you're trying to integrate FewShotPromptTemplate into the create_sql_agent function in the LangChain framework. However, the create_sql_agent function doesn't directly support the integration of FewShotPromptTemplate as it's not designed to work with prompts in this way.

The create_sql_agent function is used to construct an SQL agent from a language model and tools. It doesn't directly use prompts, but rather uses a language model, a toolkit, and other parameters to create an agent that can execute SQL queries.

If you want to use FewShotPromptTemplate in your application, you might need to create a separate function or modify the create_sql_agent function to support this. Here's a simplified example of how you might use FewShotPromptTemplate:

# Define your examples
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"},
]

# Define your example prompt
example_prompt = StringPromptTemplate.from_template("{input} = {output}")

# Create your FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="You are a helpful AI Assistant. Here are some examples:",
    suffix="Now, it's your turn:",
    input_variables=["input"]
)

# Use the few_shot_prompt within your create_sql_agent function
def create_sql_agent():
    # ... your existing code ...

    # Use the few_shot_prompt
    prompt = few_shot_prompt.format(input="What is 4+4?")

    # ... your existing code ...

This is a simplified example and may need to be adjusted based on the specifics of your create_sql_agent function and how you want to use the FewShotPromptTemplate.

Please note that this is a general guidance and might not work out of the box as it depends on the specifics of your create_sql_agent function and how you want to use the FewShotPromptTemplate. If you could provide more details about your create_sql_agent function and how you want to integrate FewShotPromptTemplate, I might be able to provide a more specific solution.

For more information about FewShotPromptTemplate, you can refer to the LangChain repository. For more information about the create_sql_agent function, you can refer to the LangChain repository.

I hope this helps! If you have any other questions, feel free to ask.

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.

aradhanachaturvedi commented 7 months ago

@akashkumar398 hi Akash,were you able to provide few shot prompts to an agent? even I have the same requirements