camparchimedes's picture
Update app.py
6038ecf verified
raw
history blame
8.4 kB
# ===========================================
# ver01.01-5.workload-----app.py
# ===========================================
#You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
#By default, you respond in Norwegian language, using a warm, direct, and professional tone.
#Your expertise is exclusively in retrieving booking information for a given booking ID assistance related to
#to this.
#You do not provide information outside of this scope. If a question is not about this topic, respond with
#"Ooops da, jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon. Gjelder det andre henvendelser,
#må du nok kontakte kundeservice på [email protected]😊"
import asyncio
import os
import re
import time
import json
import chainlit as cl
#from tiktoken import encoding_for_model
from pydantic import BaseModel, ConfigDict
from langchain import hub
from langchain_openai import OpenAI
from langchain.chains import LLMChain, APIChain
from langchain_core.prompts import PromptTemplate
from langchain.memory.buffer import ConversationBufferMemory
from langchain.memory import ConversationTokenBufferMemory
from langchain.memory import ConversationSummaryMemory
from api_docs_mck import api_docs_str
from personvernspolicy import instruction_text_priv, personvernspolicy_data
from frequently_asked_questions import instruction_text_faq, faq
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
# If you don't know the answer, just say that you don't know, don't try to make up an answer.
daysoff_assistant_template = """
You are a customer support assistant (’kundeservice AI assistent’) for Daysoff.
By default, you respond in Norwegian language, using a warm, direct, and professional tone.
Your expertise is exclusively in retrieving booking information for a given booking id and answering
questions about firmahytteorning and personvernspolicy.
If a question does not involve booking information for a given booking id, ask: "Gjelder spørsmålet firmahytteordning?",
upon user confirmation, do your best to try to answer accordingly by referring to {instruction_text_faq} and {faq}.
If a query does not involve booking information for a given booking id or firmahytteordning, ask: "Gjelder spørsmålet personvernspolicy?"
upon user confirmation, do your best to provide a precise privacy-related response by referring to: {instruction_text_priv} and {personvernspolicy_data}.
If the query does not involve booking information for a given booking id, firmahytteordning or personvernspolicy,
respond with: "Jeg driver faktisk kun med henvendelser omkring bestillingsinformasjon og ofte-stilte-spørsmål i forbindelse
med DaysOff firmahytteordning (inkludert personvernspolicyn). Gjelder det andre henvendelser, må du nok kontakte kundeservice på [email protected]😊"
Chat History: {chat_history}
Question: {question}
Answer:
"""
daysoff_assistant_prompt = PromptTemplate(
input_variables=['chat_history', 'question', "instruction_text_faq", "faq", "instruction_text_priv", "personvernspolicy_data"],
template=daysoff_assistant_template
)
api_url_template = """
Given the following API Documentation for Daysoff's official
booking information API: {api_docs}
Your task is to construct the most efficient API URL to answer
the user's question, ensuring the
call is optimized to include only the necessary information.
Question: {question}
API URL:
"""
api_url_prompt = PromptTemplate(input_variables=['api_docs', 'question'],
template=api_url_template)
# If the response includes booking information, provide the information verbatim (do not summarize it.)
api_response_template = """
With the API Documentation for Daysoff's official API: {api_docs} in mind,
and the specific user question: {question},
and given this API URL: {api_url} for querying,
and response from Daysoff's API: {api_response},
never refer the user to the API URL as your answer!
You should always provide a clear and concise summary (in Norwegian) of the booking information retrieved.
This way you directly address the user's question in a manner that reflects the professionalism and warmth
of a human customer service agent.
Summary:
"""
api_response_prompt = PromptTemplate(
input_variables=['api_docs', 'question', 'api_url', 'api_response'],
template=api_response_template
)
# ---------------------------------------------------------------------------------------------------------
# 100 tokens ≃ 75 words
# system prompt(s), total = 330 tokens
# average api response = 250-300 tokens (current)
# user input "reserved" = 400 tokens (300 words max. /English; Polish, Norwegian {..}?@tiktokenizer), could be reduc3d to 140 tokens ≃ 105 words
# model output (max_tokens) = 2048
# ConversationBufferMemory = maintains raw chat history; crucial for "nuanced" follow-ups (e.g. "nuanced" ~ for non-English inputs)
# ConversationTokenBufferMemory (max_token_limit) = 1318 (gives space in chat_history for approximately 10-15 exchanges, assuming ~100 tokens/exchange)
# ConversationSummaryMemory = scalable approach, especially useful for extended or complex interactions, caveat: loss of granular context
# ---------------------------------------------------------------------------------------------------------
@cl.on_chat_start
def setup_multiple_chains():
llm = OpenAI(
model='gpt-3.5-turbo-instruct',
temperature=0.7,
openai_api_key=OPENAI_API_KEY,
max_tokens=2048,
top_p=0.9,
frequency_penalty=0.1,
presence_penalty=0.1
)
# --ConversationBufferMemory
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
max_len=30, # --retains only the last 30 exchanges
return_messages=True,
)
# --ConversationTokenBufferMemory
#conversation_memory = ConversationTokenBufferMemory(memory_key="chat_history",
#max_token_limit=1318,
#return_messages=True,
#)
# --ConversationSummaryMemory
#conversation_memory = ConversationSummaryMemory(memory_key="chat_history",
#return_messages=True,
#)
class LLMChainConfig(BaseModel):
model_config = ConfigDict(extra='allow')
instruction_text_faq: str
faq: dict
instruction_text_priv: str
personvernspolicy_data: dict
llm_chain = LLMChain(
llm=llm,
prompt=daysoff_assistant_prompt,
memory=conversation_memory,
**LLMChainConfig(
instruction_text_faq=instruction_text_faq,
faq=faq,
instruction_text_priv=instruction_text_priv,
personvernspolicy_data=personvernspolicy_data
).model_dump()
)
cl.user_session.set("llm_chain", llm_chain)
api_chain = APIChain.from_llm_and_api_docs(
llm=llm,
api_docs=api_docs_str,
api_url_prompt=api_url_prompt,
api_response_prompt=api_response_prompt,
verbose=True,
limit_to_domains=None
)
cl.user_session.set("api_chain", api_chain)
@cl.on_message
async def handle_message(message: cl.Message):
user_message = message.content #.lower()
llm_chain = cl.user_session.get("llm_chain")
api_chain = cl.user_session.get("api_chain")
booking_pattern = r'\b[A-Z]{6}\d{6}\b'
endpoint_url = "https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1/booking"
if re.search(booking_pattern, user_message):
bestillingskode = re.search(booking_pattern, user_message).group(0)
question = f"Retrieve information for booking ID {endpoint_url}?search={bestillingskode}"
response = await api_chain.acall(
{
"bestillingskode": bestillingskode,
"question": question
},
callbacks=[cl.AsyncLangchainCallbackHandler()])
else:
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
response_key = "output" if "output" in response else "text"
await cl.Message(response.get(response_key, "")).send()
return message.content