Skip to content

Commit

Permalink
Update chainlit sample app with roletype (#132)
Browse files Browse the repository at this point in the history
* chore: Fix sample app to conform with role_type requirement

* chore: Reformat

* chore: Send message role

* chore: Fix format

* chore: Edit logo + fix roles

* chore: Fix format

* chore: Add logo light
  • Loading branch information
paul-paliychuk authored Mar 12, 2024
1 parent b149e59 commit 4e26510
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
44 changes: 31 additions & 13 deletions examples/ecommerce-chainlit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

ASSISTANT_ROLE = "assistant"
USER_ROLE = "user"
BOT_NAME = "Amazing Shoe Salesbot"

zep = ZepClient(api_key=API_KEY, api_url=ZEP_API_URL)

Expand Down Expand Up @@ -68,7 +69,7 @@ async def load_previous_chat_history(session_id: str):
session_id,
Memory(
messages=[
Message(role=msg["role"], content=msg["content"])
Message(role_type=msg["role_type"], content=msg["content"])
for msg in previous_chat_history
]
),
Expand All @@ -84,17 +85,20 @@ async def get_history(session_id: str):

if facts:
message_history.append(
{"role": "system", "content": "Facts about this user:\n" + "\n".join(facts)}
{
"role_type": "system",
"content": "Facts about this user:\n" + "\n".join(facts),
}
)
if summary:
message_history.append(
{"role": "system", "content": "Chat History:\n" + summary.content}
{"role_type": "system", "content": "Chat History:\n" + summary.content}
)

for message in memory.messages:
message_history.append(
{
"role": message.role,
"role_type": message.role_type,
"content": message.content,
}
)
Expand Down Expand Up @@ -185,6 +189,7 @@ async def classify_ready_for_purchase(session_id: str):

@cl.step(name="OpenAI", type="llm")
async def call_openai(messages):

response = await openai_client.chat.completions.create(
model=OPENAI_MODEL,
temperature=0.1,
Expand All @@ -202,7 +207,11 @@ async def on_message(message: cl.Message):
session_id,
Memory(
messages=[
Message(role=USER_ROLE, content=message.content),
Message(
role_type=USER_ROLE,
content=message.content,
role=cl.user_session.get("user_name"),
),
]
),
)
Expand Down Expand Up @@ -271,18 +280,24 @@ async def on_message(message: cl.Message):
print("Unknown intent")
return

prompt = prompt + chat_history
prompt = prompt + [
{"role": message["role_type"], "content": message["content"]}
for message in chat_history
]

print(prompt)
response_message = await call_openai(prompt)
msg = cl.Message(content=(response_message.content))
msg = cl.Message(author=BOT_NAME, content=(response_message.content))
await msg.send()

await zep.memory.aadd_memory(
session_id,
Memory(
messages=[
Message(role=ASSISTANT_ROLE, content=response_message.content),
Message(
role_type=ASSISTANT_ROLE,
content=response_message.content,
role=BOT_NAME,
),
]
),
)
Expand All @@ -297,7 +312,7 @@ async def main():
cl.user_session.set("user_id", user_id)
cl.user_session.set("session_id", session_id)

msg = cl.Message(author=ASSISTANT_ROLE, content=welcome_message)
msg = cl.Message(author=BOT_NAME, content=welcome_message)
await msg.send()

name_prompt = "What is your name?"
Expand All @@ -306,6 +321,7 @@ async def main():
res = await cl.AskUserMessage(content=name_prompt).send()
if res:
user_name = res["output"]
cl.user_session.set("user_name", user_name)
name_response = f"Hi {user_name}! How can I assist you today?"
await cl.Message(
content=name_response,
Expand Down Expand Up @@ -337,10 +353,12 @@ async def main():
Memory(
messages=[
Message(
role=ASSISTANT_ROLE, content=welcome_message + " " + name_prompt
role_type=ASSISTANT_ROLE,
content=welcome_message + " " + name_prompt,
role=BOT_NAME,
),
Message(role=USER_ROLE, content=user_name),
Message(role=ASSISTANT_ROLE, content=name_response),
Message(role_type=USER_ROLE, content=user_name, role=user_name),
Message(role_type=ASSISTANT_ROLE, content=name_response, role=BOT_NAME),
]
),
)
40 changes: 20 additions & 20 deletions examples/ecommerce-chainlit/chat_history_shoe_purchase.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
history = [
{
"role": "user",
"role_type": "user",
"content": (
"Hi there, I'm looking for a new pair of shoes. I'm not sure where"
" to start."
),
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Hello! I'd be happy to help you find the perfect pair of shoes. To"
" get started, could you tell me what brand you're interested in,"
" or if you're open to any brand?"
),
},
{
"role": "user",
"role_type": "user",
"content": "I like Nike and Adidas, but I'm open to other brands too.",
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"That's great! It's important to find the right shoe for your"
" needs. Could you tell me what you'll be using these shoes for"
Expand All @@ -28,37 +28,37 @@
),
},
{
"role": "user",
"role_type": "user",
"content": "I'll be using them for running.",
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Fantastic! Now, could you please provide me with your shoe size"
" and foot width? This information will help me narrow down the"
" options for you."
),
},
{
"role": "user",
"role_type": "user",
"content": ("I wear a men's size 10, and I'm not sure about the foot width."),
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Thank you. Based on your size, I'll make sure to suggest shoes"
" with a wide range of sizes and widths available."
),
},
{
"role": "user",
"role_type": "user",
"content": (
"I have a bit of a pronation issue. Do I need to consider that"
" while choosing shoes?"
),
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Yes, pronation is essential when choosing running shoes. It refers"
" to the way your foot rolls inward or outward when you walk or"
Expand All @@ -68,19 +68,19 @@
),
},
{
"role": "user",
"role_type": "user",
"content": "Great, I have a budget of around $120.",
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Perfect! I'll make sure to find the best running shoes that fit"
" your preferences, taking into account your brand preferences,"
" running needs, size, and budget."
),
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"I've found a few great options for you. The first one is the"
" Brooks Ghost 14, which is known for its cushioning and support,"
Expand All @@ -90,14 +90,14 @@
"metadata": {"bar": "foo"},
},
{
"role": "user",
"role_type": "user",
"content": (
"I've heard good things about Brooks, but I was kind of hoping to"
" get a Nike or an Adidas shoe."
),
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"I understand. In that case, I'd recommend the Adidas Ultraboost"
" 21. It's a popular choice among runners for its comfort, energy"
Expand All @@ -106,11 +106,11 @@
),
},
{
"role": "user",
"role_type": "user",
"content": "They're nice looking. Yes, I think I'll go with those.",
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Excellent choice! To complete the purchase, I'll need you to"
" provide your credit card information in our secure payment form."
Expand All @@ -119,18 +119,18 @@
),
},
{
"role": "system",
"role_type": "system",
"content": "Credit card purchase approved. Order complete.",
},
{
"role": "assistant",
"role_type": "assistant",
"content": (
"Thank you for completing your order with us! Your purchase of a pair of Adidas Ultraboost 21 will"
" be processed and shipped right away!"
),
},
{
"role": "assistant",
"role_type": "assistant",
"content": "To complete the order, I'll need your full name, email address, and shipping address.",
},
]
Binary file modified examples/ecommerce-chainlit/public/logo_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4e26510

Please sign in to comment.