Spaces:
Sleeping
Sleeping
File size: 8,579 Bytes
f79e226 f709b40 70ee030 f709b40 d7debf4 f709b40 8318d4c 77d14f7 f709b40 2248513 4322daa 20b2953 2248513 c80f584 2248513 f709b40 2248513 30480ad 2248513 20b2953 19da83b a699d4a f74d64c 7839a66 861654a f709b40 7839a66 f74d64c 861654a f74d64c f709b40 c80f584 f709b40 a699d4a f709b40 c80f584 f709b40 3dffd82 0dc2f2a 47d7912 3dffd82 335fa81 a699d4a 3dffd82 335fa81 f709b40 0dc2f2a f709b40 4322daa 30480ad 7f4c041 4322daa b9270a4 2248513 4322daa 30480ad 4322daa 8beb913 4322daa 2248513 f709b40 f74d64c f709b40 48c5ac5 f709b40 c80f584 f709b40 71663a5 0dc2f2a f709b40 a699d4a f709b40 7839a66 f709b40 8318d4c f709b40 7839a66 c5d3d49 0975922 20b2953 22e697c f709b40 a699d4a e06eacc 22e697c f709b40 20b2953 8318d4c f709b40 a699d4a 7839a66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
### title: 010125-daysoff-assistant-api
### file: app.py
import asyncio
import os
import re
import time
import json
import torch
import logging
from api_docs_mck import api_docs_str
#from daysoff import daysoff_str ## make daysoff.py, put json info in dict.
#from personvernpolicy import personvernpolicy_str ## make personvernpolicy.py, put json info in dict.
import chainlit as cl
from langchain import hub
from langchain.chains import LLMChain, APIChain
from langchain_core.prompts import PromptTemplate
from langchain.memory.buffer import ConversationBufferMemory
from langchain_openai import OpenAI
from langchain_community.llms import HuggingFaceHub
from langchain_huggingface import HuggingFacePipeline
from langchain_huggingface import HuggingFaceEndpoint
from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
#logging.basicConfig(level=logging.DEBUG)
HUGGINGFACEHUB_API_TOKEN = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
#HF_INFERENCE_ENDPOINT =
#BOOKING_ID = re.compile(r'\b[A-Z]{6}\d{6}\b')
BOOKING_KEYWORDS = [
"booking",
"bestillingsnummer",
"bookingen",
"ordrenummer",
"reservation",
"rezerwacji",
"bookingreferanse",
"rezerwacja",
"booket",
"reservation number",
"bestilling",
"order number",
"booking ID",
"identyfikacyjny pลatnoลci"
]
daysoff_assistant_template = """
You are a customer support assistant (โkundeservice AI assistentโ) for Daysoff named "Agrippa".
Your primary objective is to provide exceptional, empathetic, and efficient customer service.
This includes retrieving booking details for a given booking ID and answering questions about
Daysoff's personvernspolicy and firmahytteordning.
Core Operational Guidelines:
- By default, you respond in Norwegian language
- Prioritize customer satisfaction and question-answering
- Provide clear, concise, and helpful responses
- Use a friendly, professional, and approachable tone
Interaction Protocol:
4. Never fabricate information
5. Ask clarifying questions if the query is ambiguous
6. Always transparent about information limitations
7. Prioritize precision over verbosity
Persona Characteristics:
- Gender: female
- Role: customer support assistant for Dayoff.no
- Communication Style: Warm, direct, solution-oriented
- Brand Personality: Helpful, efficient, trustworthy
Response Structure:
- Directly address the user's question
- Provide step-by-step guidance if applicable
Ethical Constraints:
- Stay within the scope of Dayoff.no's services
- Maintain customer privacy
- Avoid sharing sensitive personal information
- Refer complex issues to human support at [email protected] when necessary
Special Instructions:
- Adapt communication complexity to the customer's apparent technical understanding
- Proactively suggest solutions based on contextual information
Chat History: {chat_history}
Question: {question}
Answer:
"""
daysoff_assistant_prompt= PromptTemplate(
input_variables=["chat_history", "question"],
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 {question} contains an alphanumeric identifier consisting of 6 letters followed by 6 digits (e.g., DAGHNS116478)
api_response_template = """
With the API Documentation for Daysoff's official API: {api_docs} in mind,
and user question: {question}, and given this API URL: {api_url} for querying,
here is the response from Daysoff's API: {api_response}.
Please provide an summary that directly addresses the user's question,
omitting technical details like response format, and
focusing on delivering the answer with clarity and conciseness,
as if a human customer service agent is providing this information.
Summary:
"""
api_response_prompt = PromptTemplate(
input_variables=['api_docs', 'question', 'api_url', 'api_response'],
template=api_response_template
)
@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)
#llm = HuggingFaceEndpoint(
#repo_id="google/gemma-2-2b", #"norallm/normistral-7b-warm-instruct",
#endpoint_url="http://localhost:8010/",
#model="google/gemma-2-2b",
#max_new_tokens=512,
#top_k=10,
#top_p=0.95,
#typical_p=0.95,
#temperature=0.7,
#repetition_penalty=1.03,
#huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
#task="text-generation"
#)
#llm = HuggingFacePipeline.from_model_id(
#model_id="normistral-7b-warm-instruct",
#task="text-generation",
#pipeline_kwargs={"max_new_tokens": 10},
#)
conversation_memory = ConversationBufferMemory(memory_key="chat_history",
max_len=200,
return_messages=True,
)
llm_chain = LLMChain(llm=llm,
prompt=daysoff_assistant_prompt,
memory=conversation_memory
)
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 #["https://670dccd0073307b4ee447f2f.mockapi.io/daysoff/api/V1"]
)
cl.user_session.set("api_chain", api_chain)
import logging
@cl.on_message
async def handle_message(message: cl.Message):
user_message = message.content
llm_chain = cl.user_session.get("llm_chain")
api_chain = cl.user_session.get("api_chain")
api_keywords = ["firmahytteordning", "personvernspolicy"]
# --check message for a booking ID
try:
# --check message for booking ID
if re.search(r'\b[A-Z]{6}\d{6}\b', user_message):
logging.debug(f"Booking ID detected in message: {user_message}")
response = await api_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
# --check message for API keywords
elif any(keyword in user_message.lower() for keyword in api_keywords):
logging.debug(f"API keyword detected in message: {user_message}")
response = await api_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
else:
logging.debug("Triggering LLMChain for everything else.")
response = await llm_chain.acall(user_message, callbacks=[cl.AsyncLangchainCallbackHandler()])
except Exception as e:
logging.error(f"Error in processing message: {str(e)}")
response = {"output": "Beklager, jeg kunne ikke behandle forespรธrselen din akkurat nรฅ."}
response_key = "output" if "output" in response else "text"
await cl.Message(response.get(response_key, "")).send()
return message.content
"""
@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")
#if any(keyword in user_message for keyword in ["firmahytteordning","personvernspolicy"]):
#def is_booking_query(user_message):
#match = re.search(r'\b[A-Z]{6}\d{6}\b', user_message)
#return match is not None # --works boolean
#booked = is_booking_query(user_message)
#if booked:
if re.search(r'\b[A-Z]{6}\d{6}\b', user_message): # ex. "EQJLCQ362149"
response = await api_chain.ainvoke(user_message,
callbacks=[cl.AsyncLangchainCallbackHandler()])
else:
response = await llm_chain.ainvoke(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
"""
|