Download app.py from obenan/obiagent: direct link, hf CLI and curl.
- Browser
- Download file 16.8 kB
-
https://huggingface.co/spaces/obenan/obiagent/resolve/main/app.py
- Command line
-
hf download hf://spaces/obenan/obiagent/app.py
-
curl -L -o app.py https://huggingface.co/spaces/obenan/obiagent/resolve/main/app.py
16.8 kB
| import os | |
| from textwrap import dedent | |
| import string | |
| from typing import Deque, List, Optional, Tuple | |
| from pydantic import BaseModel | |
| import ast | |
| import gradio as gr | |
| from pymongo import MongoClient | |
| from openai import OpenAI | |
| from dotenv import load_dotenv | |
| import copy | |
| from bson import ObjectId, json_util | |
| import json | |
| import numpy as np | |
| from fastapi import FastAPI, Form, Header | |
| from fastapi.responses import HTMLResponse, JSONResponse | |
| import opik | |
| from opik.integrations.openai import track_openai | |
| from opik import track | |
| import re | |
| load_dotenv() | |
| opik.configure(use_local=False) | |
| openai_client = OpenAI() | |
| client = track_openai(openai_client) | |
| mdb = MongoClient(os.environ['MONGO']) | |
| db = mdb['obi-menu-prod'] | |
| oClient = opik.Opik() | |
| class Extractables(BaseModel): | |
| keywords: list[str] | |
| class ChatRequest(BaseModel): | |
| message: str | |
| history: List[dict] | |
| userId: str | |
| venueId: str = "674fe99a6c85ec94bb7f5fa0" | |
| menuId: str = "674fea6a7c80cf73ad454dcb" | |
| USER_TEMPLATE = { | |
| "userId": "{userId}", | |
| "name": "John Doe", | |
| "state": { | |
| # "intent": "intent", | |
| "order_profile": [], | |
| "order": [], | |
| "options": [], | |
| "extras": [], | |
| }, | |
| "stage": "order_profile", | |
| "chat_history": [], | |
| "bin": { | |
| "last_question": "NA", | |
| "orders_available" : [], | |
| "options_available": [], | |
| "extras_available": [], | |
| "next_stage": "order", | |
| "next_question": "", | |
| "user_query": "", | |
| "venue": { | |
| "venue_type": "", | |
| "venue_name": "", | |
| "venue_location": "", | |
| }, | |
| "profile": "", | |
| }, | |
| } | |
| doc = None | |
| q = { | |
| # "intent": "Is the customer querying about a specific dish, or is he looking for recommendations? Reply in a one word answer; either 'query' or 'recommendation'.", | |
| "order_profile": "What does the customer want to order? Does he give any hint about the item he wants? Mention the item only in his words.", | |
| "order": "Which order did the customer select from the menu? Write the name of the dish only. ", | |
| "options":"Which options or customizations for his order did the customer mention in the query? An option must be chosen if it is available. If he did not mention an option, return an empty string.", | |
| "extras": "Which extras will the customer take with the order? If the customer choosees no extras at all, mention 'no extras'.", | |
| "done": "" | |
| } | |
| def purge(d): | |
| """Recursively collect all leaf nodes.""" | |
| result = {} | |
| for k, v in d.items(): | |
| if isinstance(v, dict): | |
| result.update(purge(v)) | |
| else: | |
| result[k] = v | |
| return result | |
| def deploy(d): | |
| """Recursively deploy all leaf nodes.""" | |
| result = {} | |
| result.update(purge(d)) | |
| result['current_question'] = q[result['stage']] | |
| result['next_question'] = q[result['next_stage']] | |
| result['current_extracted'] = result[result['stage']] | |
| return result | |
| def chat( | |
| message: str = Form(default=""), | |
| history: list = Form(default=[]), | |
| userId: str = Form(default="1733724542624324326714"), | |
| venueId: str = Form(default="674fe99a6c85ec94bb7f5fa0"), | |
| menuId: str = Form(default="674fea6a7c80cf73ad454dcb"), | |
| ) -> JSONResponse: | |
| ''' | |
| Handles a chat interaction with a user, processes their query, and updates their order state. | |
| Args: | |
| message (str): The user's message or query. | |
| history (list, optional): The chat history. Defaults to an empty list. | |
| user_id (str, optional): The unique identifier for the user. | |
| venueId (str, optional): The unique identifier for the venue. | |
| menuId (str, optional): The unique identifier for the menu. | |
| Returns: | |
| str: The assistant's reply to the user's query. | |
| message:str, | |
| history:list = [], | |
| user_id:str = "1733724542625846714", | |
| venueId:str = "674fe99a6c85ec94bb7f5fa0", | |
| menuId:str = "674fea6a7c80cf73ad454dcb", | |
| ''' | |
| docs = [] | |
| users = list(db['endusers'].find({"userId": userId})) | |
| if not users: | |
| user = dict(**USER_TEMPLATE) | |
| user.update({'userId':userId}) | |
| print("user created:", user) | |
| user.update({'chat_history': history}) | |
| else: | |
| user = users[0] | |
| user.pop('_id') | |
| user = dict(users[0]) | |
| db['endusers'].delete_many({"userId": userId}) | |
| user['bin']['user_query'] = message | |
| venue_doc = db['venues'].find_one({"_id": ObjectId(venueId)}) | |
| company_doc = db['companies'].find_one({"_id": ObjectId(venue_doc['companyId'])}) | |
| if user['stage'] == "done": | |
| reply = "Your order has been received and will be delivered soon." | |
| db['endusers'].insert_one(dict(**user)) | |
| return reply #JSONResponse(content={"text":reply, "metadata":docs, "user":json.loads(json_util.dumps(user))}) | |
| user['bin']['venue'].update( | |
| { | |
| "venue_type": venue_doc['type'].lower(), | |
| "venue_name": company_doc['name'].title(), | |
| "venue_location": venue_doc['name']['en'].title() | |
| } | |
| ) | |
| stages = ["order_profile", "order", "options", "extras", "done"] | |
| for idx, s in enumerate(stages[:-1]): | |
| if s == user['stage']: | |
| user['bin'].update({ | |
| "current_stage": stages[idx], | |
| "next_stage": stages[idx+1] | |
| }) | |
| break | |
| print("user", deploy(user)) | |
| sprompt = oClient.get_prompt(name="systemprompt-intent") | |
| uprompt = oClient.get_prompt(name="userprompt-intent") | |
| responsely = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": sprompt.prompt.format(**deploy(user)) | |
| }, | |
| { | |
| "role": "user", | |
| "content": uprompt.prompt.format(**deploy(user)) | |
| } | |
| ]) | |
| is_answer = responsely.choices[0].message.content.lower().translate(str.maketrans('', '', string.punctuation)) | |
| if is_answer != "yes" and user['chat_history'] and user['chat_history'][0]: | |
| print("user['chat_history']", user['chat_history']) | |
| reply = "Are you sure you want to quit the current conversation? Enter 'yes' to quit." | |
| if reply == user['chat_history'][-1]["content"] and user['bin']['user_query'].lower().translate(str.maketrans('', '', string.punctuation))=="yes": | |
| reply = "Please provide a description of the item you want to order. " | |
| user = copy.deepcopy(USER_TEMPLATE) | |
| user.update({"userId":userId, "chat_history": history}) | |
| print("State deleted for user:", user) | |
| elif reply == user['chat_history'][-1]["content"]: | |
| reply = "Please try to stay on the topic of the conversation. {last_question}".format(**deploy(user)) | |
| else: | |
| user['bin'].update({'profile':str({k:v for k,v in user['state'].items() if v})}) | |
| sprompt = oClient.get_prompt(name="systemprompt-extract") | |
| uprompt = oClient.get_prompt(name="userprompt-extract") | |
| completion = client.beta.chat.completions.parse( | |
| model="gpt-4o", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": sprompt.prompt.format(**deploy(user)) | |
| }, | |
| { | |
| "role": "user", | |
| "content": uprompt.prompt.format(**deploy(user)) | |
| } | |
| ], | |
| response_format = Extractables | |
| ) | |
| extracted = ast.literal_eval(completion.choices[0].message.content)["keywords"] | |
| print("extracted->", extracted) | |
| user['state'].update({user['stage']:extracted}) | |
| user["bin"]['current_extracted'] = extracted | |
| sprompt = oClient.get_prompt(name="systemprompt-check") | |
| uprompt = oClient.get_prompt(name="userprompt-check") | |
| check = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[ | |
| { | |
| "role": "system", | |
| "content": sprompt.prompt.format(**deploy(user)) | |
| }, | |
| { | |
| "role": "user", | |
| "content": uprompt.prompt.format(**deploy(user)) | |
| } | |
| ]) | |
| verify = check.choices[0].message.content.lower().translate(str.maketrans('', '', string.punctuation)).replace("NA", "") | |
| if verify == "yes": | |
| stages = ["order_profile", "order", "options", "extras", "done"] | |
| for idx, s in enumerate(stages[:-1]): | |
| if s == user['stage']: | |
| user['stage'] = stages[idx+1] | |
| break | |
| if user['stage'] == "done": | |
| reply = "Your order has been received and will be delivered soon." | |
| db['endusers'].insert_one(dict(**user)) | |
| return reply #JSONResponse(content={"text":reply, "metadata":docs, "user":json.loads(json_util.dumps(user))}) | |
| sprompt = oClient.get_prompt(name="systemprompt-root") | |
| uprompt = oClient.get_prompt(name="userprompt-root") | |
| Sprompt = sprompt.prompt.format(**deploy(user)) | |
| Uprompt = uprompt.prompt.format(**deploy(user)) | |
| if user['stage'] == 'order_profile': | |
| # print("deploy(user)", deploy(user)) | |
| menu_items = db['ai-items'].find({"menus._id": ObjectId(menuId), "venue._id": ObjectId(venueId)}) | |
| categories = list(set([x['categories']['name']['en'] for x in menu_items])) | |
| user['bin'].update({'categories' : categories}) | |
| sprompt = oClient.get_prompt(name="systemprompt-coldstart") | |
| uprompt = oClient.get_prompt(name="userprompt-coldstart") | |
| Sprompt = Sprompt + sprompt.prompt.format(**deploy(user)) | |
| Uprompt = Uprompt + uprompt.prompt.format(**deploy(user)) | |
| elif user['stage'] == "order": | |
| # if user['state']['order_profile']: | |
| embed = client.embeddings.create(input= user["state"]["order_profile"] , model="text-embedding-3-small") | |
| query_embedding = embed.data[0].embedding | |
| docs = db['ai-items'].aggregate([ | |
| { | |
| "$vectorSearch": { | |
| "queryVector": query_embedding, | |
| "path": "embeddings", | |
| "index": "o2", | |
| "score": {"$meta": "vectorSearchScore"}, | |
| "numCandidates": 100, | |
| "limit": 4, | |
| } | |
| }, | |
| { | |
| "$match": { | |
| "menus._id": ObjectId(menuId), | |
| "venue._id": ObjectId(venueId) | |
| } | |
| }, | |
| { | |
| "$project": {"embeddings": 0} | |
| }, | |
| ]) | |
| docs = list(docs) | |
| # doc = docs[0] | |
| user['bin'].update({'orders_available':[x['name']['en']+": "+x['description']['en'][0]['children'][0]['text'] for x in docs]}) | |
| print('similar', user['bin']['orders_available']) | |
| sprompt = oClient.get_prompt(name="systemprompt-orderprofile") | |
| uprompt = oClient.get_prompt(name="userprompt-orderprofile") | |
| Sprompt = Sprompt + sprompt.prompt.format(**deploy(user)) | |
| Uprompt = Uprompt + uprompt.prompt.format(**deploy(user)) | |
| # else: | |
| elif user['stage'] == "options": | |
| embed = client.embeddings.create(input= user["state"]["order"], model="text-embedding-3-small") | |
| query_embedding = embed.data[0].embedding | |
| docs = db['ai-items'].aggregate([ | |
| { | |
| "$vectorSearch": { | |
| "queryVector": query_embedding, | |
| "path": "embeddings", | |
| "index": "o2", | |
| "score": {"$meta": "vectorSearchScore"}, | |
| "numCandidates": 100, | |
| "limit": 1, | |
| } | |
| }, | |
| { | |
| "$match": { | |
| "menus._id": ObjectId(menuId), | |
| "venue._id": ObjectId(venueId) | |
| } | |
| }, | |
| { | |
| "$project": {"embeddings": 0} | |
| }, | |
| ]) | |
| docs = list(docs) | |
| doc = list(docs)[0] | |
| options_available=[] | |
| for i, d in enumerate(doc['prices']): | |
| for j, d in enumerate(doc['prices'][i]['variants']): | |
| options_available.append({doc['prices'][i]['description']['en'] : "$"+ str(doc['prices'][i]['variants'][j]['price'])}) | |
| print("options_available", options_available) # doc['name']['en'] q[next_stage] | |
| user['bin'].update({'options_available':options_available}) | |
| sprompt = oClient.get_prompt(name="systemprompt-order") | |
| uprompt = oClient.get_prompt(name="userprompt-order") | |
| Sprompt = Sprompt + sprompt.prompt.format(**deploy(user)) | |
| Uprompt = Uprompt + uprompt.prompt.format(**deploy(user)) | |
| elif user['stage'] == "extras": | |
| embed = client.embeddings.create(input= user["state"]["order"], model="text-embedding-3-small") | |
| query_embedding = embed.data[0].embedding | |
| docs = db['ai-items'].aggregate([ | |
| { | |
| "$vectorSearch": { | |
| "queryVector": query_embedding, | |
| "path": "embeddings", | |
| "index": "o2", | |
| "score": {"$meta": "vectorSearchScore"}, | |
| "numCandidates": 100, | |
| "limit": 1, | |
| } | |
| }, | |
| { | |
| "$match": { | |
| "menus._id": ObjectId(menuId), | |
| "venue._id": ObjectId(venueId) | |
| } | |
| }, | |
| { | |
| "$project": {"embeddings": 0} | |
| }, | |
| ]) | |
| docs = list(docs) | |
| doc = list(docs)[0] | |
| user['bin'].update({'order_name':doc['name']['en']}) | |
| extras_available=[] | |
| for i, d in enumerate(doc['customizations']): | |
| for j, d in enumerate(doc['customizations'][i]['prices']): | |
| extras_available.append(doc['customizations'][i]['prices'][j]['description']['en']) | |
| print("extras_available", extras_available) | |
| user['bin'].update({'extras_available':extras_available}) | |
| sprompt = oClient.get_prompt(name="systemprompt-options") | |
| uprompt = oClient.get_prompt(name="userprompt-options") | |
| Sprompt = Sprompt + sprompt.prompt.format(**deploy(user)) | |
| Uprompt = Uprompt + uprompt.prompt.format(**deploy(user)) | |
| text = client.chat.completions.create(model="gpt-4", messages=[ | |
| {"role": "system", "content": Sprompt}, | |
| {"role": "user", "content": Uprompt} | |
| ]) | |
| reply = text.choices[0].message.content | |
| user['bin'].update({'last_question': reply}) | |
| user['chat_history'].append({"role": "user", "content": user['bin']['user_query']}) | |
| user['chat_history'].append({"role": "assistant", "content": reply}) | |
| ref1 = db['endusers'].delete_many({"userId": userId}) | |
| ref2 = db['endusers'].insert_one(dict(**user)) | |
| print('deploy(user)',deploy(user)) | |
| # if docs: | |
| # docs = [transform_json(json_util.dumps(d)) for d in docs] | |
| return reply | |
| with gr.Blocks(fill_height=True) as demo: #fill_height=True, | |
| with gr.Row(equal_height=True): | |
| with gr.Column(scale=2, min_width=240): | |
| userId = gr.Textbox(str(int(np.random.rand()*10**16)), label="User Id") | |
| venueId = gr.Textbox("674fe99a6c85ec94bb7f5fa0", label="Venue Id") | |
| menuId = gr.Textbox("674fea6a7c80cf73ad454dcb", label="Menu Id") | |
| clear = gr.Button("Clear Session", size="sm") | |
| with gr.Column(scale=8, min_width=440): | |
| bot = gr.ChatInterface( | |
| fn=chat, | |
| type="messages", | |
| additional_inputs=[ | |
| userId, | |
| venueId, | |
| menuId | |
| ], | |
| ) | |
| def clearSession(userId): | |
| db['endusers'].delete_many({"userId": userId}) | |
| clear.click(clearSession, inputs=[userId]) | |
| demo.launch() |