Jupyter Notebook - Building an Orderbot using Gemini API

Building a Chatbot using Gemini API

Task 0: Configure API key

The Python SDK for the Gemini API is contained in the google-generativeai package.

Install dependency using: pip install -q -U google-generativeai

Do not check an API key into your version control system.

Task 1: Initialize the model

import google.generativeai as genai

with open('gemini_api_key.txt','r') as file: # rename file to 'llm_api_key.txt'
    API_KEY = file.read()
genai.configure(api_key=API_KEY)

Use system instructions to steer the behavior of the model

# Using a context manager to open the file
with open('pizzabot_system_instruction.txt', 'r') as file:
    sys_instr = file.read()
model=genai.GenerativeModel(
  model_name="gemini-1.5-flash",
  system_instruction=sys_instr)

##### 2.1 Initialize the chat
chat = model.start_chat(history=[])

Task 2: Receive Prompts and Save Context and Generate chat responses

ChatSession class of gemini enables us to have freeform conversation over multiple turns. We dont have to store conversation history as a list.

Example: Build an interactive chat

The ChatSession.send_message method returns the same GenerateContentResponse type as GenerativeModel.generate_content. It also appends your message and the response to the chat history

import panel as pn
def run_chat(value,user,instance):
    response = chat.send_message(value)
    # return f"{response.text}"
    return pn.chat.ChatMessage(response.text, user="Pizza Bot", avatar="🐼")

Task 3: Build GUI

Help: doc

pn.extension("perspective")#initialization

chat_bot = pn.chat.ChatInterface(help_text="Welcome to Pizza Paradise! What would like to order?",callback=run_chat,
                                 max_height=500,max_width=500,show_rerun=False,show_clear=False,
                                 )

Challenges with Gemini model

  1. Making mistakes in mathematical calculations. When we ask it to re-check the calculation, it may give you a different total than the one it said earlier. –> What helped: While calculating the total bill of the customer, calculate the sum of price of all items ordered by the customer.
  2. Compared to openai api, I had to add more instructions for the chatbot to make it perform satisfactorily. Eg:
    1. While calculating the total bill of the customer, calculate the sum of price of all items ordered by the customer.
    2. Please do not suggest dishes not in the menu mentioned below.
  3. The instructions specify that the chatbot should always ask for toppings after a pizza order is placed. However, chatbot has missed it in some instances.

Task 4: Serving the Notebook

We’ll organize our components in a nicely styled template (MaterialTemplate) and mark it .servable() to add it to our served app

pn.template.MaterialTemplate(
    site="Panel",
    title="Order bot of Pizza Paradise!",
    main=[chat_bot],
).servable(); # The ; is needed in the notebook to not display the template. Its not needed in a script

Deploy the app locally:

Run in terminal:

panel serve v2_gemini-pizza-chatbot_chatInterface.ipynb --autoreload

Deploy in Cloud:

  1. How can I deploy my app and embed it in my website? : DONE
    1. Use: https://docs.cloud.ploomber.io/en/latest/apps/panel.html

Appendix: System instructions to the model

You are OrderBot, an automated service to collect orders for a pizza restaurant. You first greet the customer, then collects the order, and then asks if it’s a pickup or delivery. You wait to collect the entire order, then summarize it and check for a final time if the customer wants to add anything else. If it’s a delivery, you ask for an address and contact number. When you give the final order summary, calculate the total price incurred by the customer based on the price tag in the menu. While calculating the total bill of the customer, calculate the sum of price of all items ordered by the customer. If the customer asks for any clarifications on the bill total, re-do the calculation and say the result. Finally you collect the payment. Make sure to clarify all options, extras and sizes to uniquely identify the item from the menu. If they choose a pizza, make sure to ask about Toppings. Please do not suggest dishes not in the menu mentioned below. You respond in a short, very conversational friendly style. The menu includes pepperoni pizza 12.95, 10.00, 7.00 cheese pizza 10.95, 9.25, 6.50 eggplant pizza 11.95, 9.75, 6.75 fries 4.50, 3.50 greek salad 7.25 Toppings: extra cheese 2.00, mushrooms 1.50 sausage 3.00 canadian bacon 3.50 AI sauce 1.50 peppers 1.00 Drinks: coke 3.00, 2.00, 1.00 sprite 3.00, 2.00, 1.00 bottled water 5.00