camparchimedes commited on
Commit
a0d14b1
·
verified ·
1 Parent(s): f1d1fa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -40
app.py CHANGED
@@ -28,26 +28,27 @@ import chainlit as cl
28
 
29
  system_template = """
30
  Use the following pieces of context to answer the user's question.
31
- Please respond as if you are a human female customer service representative for Daysoff,
32
- a Norwegian company that provides welfare services by offering access to cottages and
33
- apartments for employees of member companies.
34
- By default, you respond (in Norwegian language) using a warm, direct, and professional tone.
35
- Your expertise covers FAQs, and privacy policies.
36
- If you don't know the answer, just say that you don't know, don't try to make up an answer:
37
- politely redirect the user to customer service at [email protected] and remind them to always
38
- include their booking id (bestillingskode).
39
  You can make inferences based on the context as long as it still faithfully represents the feedback.
40
 
41
- Example of how your response should be direct:
42
 
43
  ```
44
- foo
45
  ```
46
 
47
  Begin!
48
  ----------------
49
  {context}"""
50
 
 
51
  messages = [
52
  SystemMessagePromptTemplate.from_template(system_template),
53
  HumanMessagePromptTemplate.from_template("{question}"),
@@ -55,28 +56,31 @@ messages = [
55
  prompt = ChatPromptTemplate(messages=messages)
56
  chain_type_kwargs = {"prompt": prompt}
57
 
 
 
58
  @cl.author_rename
59
  def rename(orig_author: str):
60
  rename_dict = {"Just a moment": "Thinking.."}
61
  return rename_dict.get(orig_author, orig_author)
62
 
63
  @cl.on_chat_start
64
- async def init():
65
 
66
- msg = cl.Message(content=f"Building vector store...")
67
  await msg.send()
68
 
69
- loader = CSVLoader(file_path="./data/total_faq.csv", source_column="Answer")
70
  data = loader.load()
71
 
72
  for i, doc in enumerate(data):
73
- doc.metadata["row_index"] = i + 1
74
- doc.metadata["source"] = doc.metadata.get("Info_Url", "")
75
 
76
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
77
-
78
  documents = text_splitter.transform_documents(data)
 
79
  store = LocalFileStore("./cache/")
 
80
  core_embeddings_model = OpenAIEmbeddings()
81
  embedder = CacheBackedEmbeddings.from_bytes_store(
82
  core_embeddings_model, store, namespace=core_embeddings_model.model
@@ -84,34 +88,36 @@ async def init():
84
  docsearch = await cl.make_async(FAISS.from_documents)(documents, embedder)
85
 
86
  chain = RetrievalQA.from_chain_type(
87
- ChatOpenAI(model="gpt-4", temperature=0.0, streaming=True), # streaming=False
88
  chain_type="stuff",
89
  return_source_documents=True,
90
  retriever=docsearch.as_retriever(),
91
- chain_type_kwargs = {"prompt": prompt}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  )
93
 
94
- html_content = """
95
- <table style="width: 80%; background-color: #1e1e1e; color: white; border-radius: 8px;">
96
- <tr>
97
- <td style="padding: 10px;">
98
- <strong>Eksempler på spørsmål:</strong>
99
- <ul>
100
- <li>Hvordan registrerer jeg meg som bruker?</li>
101
- <li>Kan jeg ha med kjæledyr på hytta?</li>
102
- <li>Adferdsmessig annonsering?</li>
103
- <li>Hvordan blir dataene mine beskyttet?</li>
104
- </ul>
105
- </td>
106
- </tr>
107
- </table>
108
- """
109
-
110
- html_element = cl.Text(content=html_content, name="HTML Table", display="inline")
111
- await cl.Message(content=f"FAISS ready. Her er noen eksempler på spørsmål:☕️", elements=[html_element]).send()
112
- #msg.content = f"FAISS ready. Her er noen eksempler på spørsmål:\n\n{markdown}"
113
- #await msg.send()
114
-
115
  cl.user_session.set("chain", chain)
116
 
117
  @cl.on_message
@@ -156,4 +162,4 @@ async def main(message):
156
  return
157
 
158
  else:
159
- await cl.Message(content=f"No sources found").send()
 
28
 
29
  system_template = """
30
  Use the following pieces of context to answer the user's question.
31
+ You are an AI customer service assistant for Daysoff and respond to queries in Norwegian language (by default).
32
+ Your expertise is in providing fast, accurate and on-brand answers that covers frequently asked questions about
33
+ Daysoff firmahytteordning and personvernspolicy.
34
+
35
+ If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
36
+ If the question is not about either of these two topics, politely inform them that you are tuned to only answer
37
+ questions about this and that all other queries are best directed to [email protected].
38
+
39
  You can make inferences based on the context as long as it still faithfully represents the feedback.
40
 
41
+ Example of how your response should be, using a warm, direct, and professional tone:
42
 
43
  ```
44
+ foo
45
  ```
46
 
47
  Begin!
48
  ----------------
49
  {context}"""
50
 
51
+
52
  messages = [
53
  SystemMessagePromptTemplate.from_template(system_template),
54
  HumanMessagePromptTemplate.from_template("{question}"),
 
56
  prompt = ChatPromptTemplate(messages=messages)
57
  chain_type_kwargs = {"prompt": prompt}
58
 
59
+
60
+
61
  @cl.author_rename
62
  def rename(orig_author: str):
63
  rename_dict = {"Just a moment": "Thinking.."}
64
  return rename_dict.get(orig_author, orig_author)
65
 
66
  @cl.on_chat_start
67
+ async def start():
68
 
69
+ msg = cl.Message(content="Building vector store...")
70
  await msg.send()
71
 
72
+ loader = CSVLoader(file_path="./data/total_faq.csv", source_column="Answer")
73
  data = loader.load()
74
 
75
  for i, doc in enumerate(data):
76
+ doc.metadata["row_index"] = i + 1
77
+ doc.metadata["source"] = doc.metadata.get("Info_Url", "")
78
 
79
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
 
80
  documents = text_splitter.transform_documents(data)
81
+
82
  store = LocalFileStore("./cache/")
83
+
84
  core_embeddings_model = OpenAIEmbeddings()
85
  embedder = CacheBackedEmbeddings.from_bytes_store(
86
  core_embeddings_model, store, namespace=core_embeddings_model.model
 
88
  docsearch = await cl.make_async(FAISS.from_documents)(documents, embedder)
89
 
90
  chain = RetrievalQA.from_chain_type(
91
+ ChatOpenAI(model="gpt-4", temperature=0.0, streaming=True),
92
  chain_type="stuff",
93
  return_source_documents=True,
94
  retriever=docsearch.as_retriever(),
95
+ chain_type_kwargs={"prompt": prompt}
96
+ )
97
+
98
+ props = {
99
+ "questions": [
100
+ "Jeg har lagt inn en bestilling hva skjer videre?",
101
+ "Hva er betingelser for utleie?",
102
+ "Jeg fikk en e-post om ny bestilling, men jeg finner den ikke i systemet?",
103
+ "Med hvem deler dere mine personlige opplysninger?",
104
+ "Hvordan beskytter dere dataene mine?",
105
+ "Kan jeg ta med hund eller katt?"
106
+ ]
107
+ }
108
+
109
+ custom_element = cl.CustomElement(
110
+ name="ExampleQuestions",
111
+ props=props,
112
+ display="inline"
113
  )
114
 
115
+ await cl.Message(
116
+ content="FAISS ready. Her er noen eksempler på populære spørsmål omkring firmahytteordning og personvern: 🤔",
117
+ elements=[custom_element]
118
+ ).send()
119
+
120
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  cl.user_session.set("chain", chain)
122
 
123
  @cl.on_message
 
162
  return
163
 
164
  else:
165
+ await cl.Message(content=f"No sources found").send()