ploomber / doc

Documentation for Ploomber Cloud
https://docs.cloud.ploomber.io
Apache License 2.0
20 stars 12 forks source link

customer chatbot v2 #139

Closed edublancas closed 3 months ago

edublancas commented 4 months ago

we need to improve the existing demo to be more capable

The existing demo was built with haystack, I'll leave it up to you @neelasha23 to determine if it's worth keeping haystack or go straight to vanilla OpenAI. The problem of using haystack is that it creates another layer of abstraction, and introduces a lot of dependencies like torch, which slow down deployment significantly.

Feel free to change the code as much as you want to simplify and speed things up.

we want this new version to allow canceling orders.

User: hi, I'd like to cancel an order
*Assistant (detects that the user wants to perform the CANCEL action on an order, but it's missing the ORDER ID*
Assistant: Sounds like you want to cancel an order, please provide the order ID
User: XYZ
*Assistant validates that XYZ is a valid order ID*
*Assistant calls a function that determines if order XYZ can still be canceled according to the company's policy*
Assistant: Your order can still be canceled, please confirm that you want to cancel order XYZ
User: Yes
Assistant: your order has been canceled

There might be other scenarios to consider, so as a first step, let's agree on what the process is, you can write a process diagram with mermaid

neelasha23 commented 4 months ago

Tried a few functions using OpenAI only and not Haystack. This is the approach that I have come up with:

  1. Using OpenAI detect invoice number and intent from user query. We can shortlist some intents like: ["TOTAL_ORDER_COST", "CANCEL", "NUMBER_OF_ITEMS", "ORDER_ITEM_DETAILS", "CUSTOMER_ORDERS"] etc
  2. Individual functions for handling each of these intents. Function will filter the Pandas dataframe (dataset) for InvoiceNo == invoice number in input and perform necessary action. e.g. If intent == "CANCEL": call function to determine if order can be cancelled. This function filters the dataset by InvoiceNo and checks whether InvoiceDate is after November 1 2010 and before December 2 2010.
  3. If intent is not detected, then just filter the dataset by InvoiceNo and pass the filtered data to OpenAI as context.

Here's the notebook: customer_chatbot.ipynb.zip

i have shortlisted some query types:

What were the items in order [order id] ?
How many items were in order [order id] ?
Can I still cancel order with invoice number [invoice number] ?
What is the total cost for order with invoice number [invoice] ?
All orders made by customer Id [customer id] ?
What is the price of item [item description] ?
How many orders mader on [date] ?
Orders made by [customer ID] on [date] ?
Item price of [product description] ?
What is the highest selling product in the month of [month] ?
Invoice numbers/ Order IDs between [start_date] and [end_date] ?

Link for mermaid diagram: https://www.mermaidchart.com/app/projects/be96d41b-f9d3-46ac-9eab-c774547abff8/diagrams/4dd3c612-8baa-4684-a03f-daa274723db8/version/v0.1/edit

(This doesn't have all the intent types)

@edublancas

edublancas commented 4 months ago

yeah, sounds good overall. I think you'll need to experiment a bit when detecting intent. Ideally, we'd detect both intent and data in the same openAI API call by forcing it to return a JSON e.g. {"intent": "CANCEL", "order_id": "XYZ"} (if user passes order ID) or {"intent": "CANCEL", "order_id": None} (if user wants to cancel but doesn't pass order ID) - but if a single API call doesn't work that well, then you can have separate API calls for intent, order ID, etc.

you can read some docs on how to force OpenAI to return JSON, there are also some libraries: https://github.com/jxnl/instructor up to you to decide which approach is best

can you post the mermaid diagram inline? like this:

graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;

the link you shared is asking me to create an account

neelasha23 commented 4 months ago

I cant see a Download option:

Screenshot 2024-03-08 at 2 35 40 AM

I'm only seeing Share

@edublancas

neelasha23 commented 4 months ago

Is this clear?

Screenshot 2024-03-08 at 2 37 09 AM
edublancas commented 4 months ago

yeah, I can read it. but you can embed mermaid diagrams directly here, no need to use any other tool, see: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-diagrams

neelasha23 commented 4 months ago
flowchart TD
A[User query] --> R[Detect invoice number]
R --> B{Invoice number present?}
B -->|No| C[Ask user for invoice number]
B -->|Yes| D[Detect intent, e.g., TOTAL_ORDER_COST, CANCEL, etc]
D --> E{intent detected?}
E --> |No| F[Pass the invoice details as context to OpenAI for answering the question]
E --> |TOTAL_ORDER_COST| G[Call function for calculating order total for the invoice number detected]
E --> |CANCEL| H[Call function to check if invoice date is after November 1 2010 and before December 2 2010. Provide feedback to user]
neelasha23 commented 4 months ago

Does it need to be a FastAPI application or can be Panel? @edublancas

edublancas commented 4 months ago

yep. it can be panel

edublancas commented 4 months ago

overall, it looks good. I simplified it a bit but it remains pretty much the same

flowchart TD
first([User asks first query]) --> check_order_id{Order ID and intent present?}
check_order_id -- No --> missing[Ask for missing info] --> check_order_id
check_order_id -- Yes --> intent_cancel{Intent==CANCEL?}
intent_cancel -- No --> invalid_intent[Tell user the intent is unsupported]
intent_cancel -- Yes --> valid_req{Valid request?}
valid_req -- Yes --> ask_confirm{Ask user to confirm}
ask_confirm -- Yes --> success[Confirm success]
ask_confirm -- No --> retry[Ask if we can help with anything else] -->check_order_id
valid_req -- No --> fail[Explain why it couldn't proceed]