Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 010125-daysoff-assistant-api
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
import json
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
from api_docs_mck import daysoff_api_docs
|
| 9 |
+
|
| 10 |
+
import chainlit as cl
|
| 11 |
+
|
| 12 |
+
from langchain import hub
|
| 13 |
+
from langchain.chains import LLMChain, APIChain
|
| 14 |
+
from langchain_core.prompts import PromptTemplate
|
| 15 |
+
from langchain_community.llms import HuggingFaceHub
|
| 16 |
+
from langchain.memory.buffer import ConversationBufferMemory
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 20 |
+
LANGCHAIN_API_KEY = os.environ.get("LANGCHAIN_API_KEY")
|
| 21 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 22 |
+
|
| 23 |
+
#os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:true"
|
| 24 |
+
|
| 25 |
+
dtype = torch.float16
|
| 26 |
+
device = torch.device("cuda")
|
| 27 |
+
|
| 28 |
+
daysoff_assistant_booking_template = """
|
| 29 |
+
You are a customer support assistant for Daysoff.no. Your expertise is
|
| 30 |
+
retrieving booking information for a given booking ID (โbestillingsnummerโ)"
|
| 31 |
+
Chat History: {chat_history}
|
| 32 |
+
Question: {question}
|
| 33 |
+
Answer:
|
| 34 |
+
"""
|
| 35 |
+
daysoff_assistant_booking_prompt= PromptTemplate(
|
| 36 |
+
input_variables=["chat_history", "question"],
|
| 37 |
+
template=daysoff_assistant_booking_template
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
api_url_template = """
|
| 41 |
+
Given the following API Documentation for Daysoff's official
|
| 42 |
+
booking information API: {api_docs_mck}
|
| 43 |
+
Your task is to construct the most efficient API URL to answer
|
| 44 |
+
the user's question, ensuring the
|
| 45 |
+
call is optimized to include only the necessary information.
|
| 46 |
+
Question: {question}
|
| 47 |
+
API URL:
|
| 48 |
+
"""
|
| 49 |
+
api_url_prompt = PromptTemplate(input_variables=['api_docs_mck', 'question'],
|
| 50 |
+
template=api_url_template)
|
| 51 |
+
|
| 52 |
+
api_response_template = """"
|
| 53 |
+
With the API Documentation for Daysoff's official API: {api_docs_mck}
|
| 54 |
+
and the specific user question: {question} in mind,
|
| 55 |
+
and given this API URL: {api_url} for querying, here is the
|
| 56 |
+
response from Daysoff's API: {api_response}.
|
| 57 |
+
Please provide a summary that directly addresses the user's question,
|
| 58 |
+
omitting technical details like response format, and
|
| 59 |
+
focusing on delivering the answer with clarity and conciseness,
|
| 60 |
+
as if a human customer service agent is providing this information.
|
| 61 |
+
Adapt to user's language. By default, you speak Norwegian.
|
| 62 |
+
Summary:
|
| 63 |
+
"""
|
| 64 |
+
api_response_prompt = PromptTemplate(input_variables=['api_docs_mck',
|
| 65 |
+
'question',
|
| 66 |
+
'api_url',
|
| 67 |
+
'api_response'],
|
| 68 |
+
template=api_response_template)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# --model, memory object, and llm_chain
|
| 72 |
+
@cl.on_chat_start
|
| 73 |
+
def setup_multiple_chains():
|
| 74 |
+
llm = HuggingFaceHub(repo_id="google/gemma-2-2b-it",
|
| 75 |
+
temperature=0.7,
|
| 76 |
+
huggingface_api_token=HUGGINGFACEHUB_API_TOKEN,
|
| 77 |
+
device=device)
|
| 78 |
+
|
| 79 |
+
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
|
| 80 |
+
max_len=200,
|
| 81 |
+
return_messages=True,
|
| 82 |
+
)
|
| 83 |
+
llm_chain = LLMChain(llm=llm,
|
| 84 |
+
prompt=daysoff_assistant_booking_prompt,
|
| 85 |
+
memory=conversation_memory
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
cl.user_session.set("llm_chain", llm_chain)
|
| 89 |
+
|
| 90 |
+
api_chain = APIChain.from_llm_and_api_docs_mck(
|
| 91 |
+
llm=llm,
|
| 92 |
+
api_docs_mck=daysoff_api_docs,
|
| 93 |
+
api_url_prompt=api_url_prompt,
|
| 94 |
+
api_response_prompt=api_response_prompt,
|
| 95 |
+
verbose=True,
|
| 96 |
+
limit_to_domains=None)
|
| 97 |
+
|
| 98 |
+
cl.user_session.set("api_chain", api_chain)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# --wrapper function around the @cl.on_message decorator; chain trigger(s)
|
| 102 |
+
@cl.on_message
|
| 103 |
+
async def handle_message(message: cl.Message):
|
| 104 |
+
user_message = message.content.lower()
|
| 105 |
+
llm_chain = cl.user_session.get("llm_chain")
|
| 106 |
+
api_chain = cl.user_session.get("api_chain")
|
| 107 |
+
|
| 108 |
+
if any(keyword in user_message for keyword in ["booking_id", "full_name", "amount", # + "bestillingsnummer", "checkin", "checkout" for api_docs
|
| 109 |
+
"date", "address", "amount", "user_id"]):
|
| 110 |
+
# --if any keywords in user_message, use api_chain
|
| 111 |
+
response = await api_chain.acall(user_message,
|
| 112 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
| 113 |
+
else:
|
| 114 |
+
# --defaults to llm_chain4general queries
|
| 115 |
+
response = await llm_chain.acall(user_message,
|
| 116 |
+
callbacks=[cl.AsyncLangchainCallbackHandler()])
|
| 117 |
+
response_key = "output" if "output" in response else "text"
|
| 118 |
+
await cl.Message(response.get(response_key, "")).send()
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
if __name__ == '__main__':
|
| 123 |
+
cl.launch()
|