Skip to main content

Type of prompt

The type of the prompt is detected next. This is an individual prompt in the sequence of instructions returned by the cleaning tool.

The type can be detected in two ways - manually or through a ReAct (Reason-Act) agent.

Manually

prompt_template = PromptTemplate(
template="""
You will be asked three types of queries - to answer something about Sales, to answer something about forecast, or to solve some mathematical problem.
You must identify the type of query and respond only 'Sales', 'Forecast' or 'Maths' and nothing else.

---
'Sales': if the query is about the delivery/demand/sales of various brands/SKUs/products. Look out for keywords 'delivery', 'demand', 'sales'
'Forecast': if the query is about the prediction/forecast of delivery/demand/sales of various brands/SKUs/products. Look out for keywords 'forecast', 'error', 'IBP', 'improvement', 'score'
'Maths': if the query is about performing some mathematical computation. Loot out for keywords 'add', 'subtract', 'multiply', 'divide'.
---

Your response will be directly fed into a function as an argument so don't respond with anything other
than 'Sales', 'Forecast' or 'Maths'.

---
Query:
{query}
---
""", input_variables=["query"]
)

type = model.invoke(prompt_template.format(query=query)).content
if type == "Sales":
return sales(llm, query, response, country)
elif type == "Forecast":
return forecast(llm, query, response, country)
elif type == "Maths":
return maths(llm, query, response)

ReAct agent


def create_db_tool(table):
all = {"Sales": ["sales", "demand", "quantity_delivered"],
"Forecast": ["forecast", "prediction", "IBP", "error", "improvement"]}
quantities = " ".join(all[table])

class Input(BaseModel):
query: str = Field(description=f"instruction to be converted to SQL query on {table} database for computation and response")

tool = StructuredTool.from_function(
func=chain.run,
name=f"{table}_database_tool",
description=f"Useful for querying the {table} database to get some information about {quantities} of brands and SKUs",
args_schema=Input
)
return tool

def create_maths_tool():
class MathInput(BaseModel):
query: str = Field(description="prompt to be analyzed mathematically with programs")

maths_chain = PALChain.from_math_prompt(llm, allow_dangerous_code=True)
maths_tool = StructuredTool.from_function(
func=maths_chain.run,
name="maths_tool",
description="Useful for analyzing a problem mathematically",
args_schema=MathInput
)

return maths_tool

sales, forecast, maths = create_db_tool("Sales"), create_db_tool("Forecast"), create_maths_tool()

agent = create_react_agent(llm, [sales, forecast, maths], prompt)
agent_executor = AgentExecutor(agent=agent, tools=[sales, forecast, maths], verbose=True)

agent_executer.invoke({"input": query})

The following pages document the manual process.