camparchimedes commited on
Commit
747720a
ยท
verified ยท
1 Parent(s): 8f0245e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -35
app.py CHANGED
@@ -1,5 +1,6 @@
1
  # ===========================================
2
- # !-----app.py
 
3
  # ===========================================
4
 
5
  import json
@@ -15,18 +16,25 @@ from langchain.chains import LLMChain
15
  from langchain_core.prompts import PromptTemplate
16
  from langchain.memory.buffer import ConversationBufferMemory
17
 
 
 
 
 
 
 
 
18
  load_dotenv()
19
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
20
  auth_token = os.environ.get("DAYSOFF_API_TOKEN")
21
 
22
  API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
23
 
24
- # You help users retrieve booking information associated with their booking IDs.Provide a conversational answer.
25
  daysoff_assistant_template = """
26
  You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
27
- By default, you respond in Norwegian language.
28
- You should always keep in mind the professionalism and warmth of a real human female customer support representative when you provide users with their
29
- the reqyested booking information in a conversational answer.
 
30
  Chat History: {chat_history}
31
  Question: {question}
32
  Answer:
@@ -40,10 +48,35 @@ daysoff_assistant_prompt = PromptTemplate(
40
  async def async_post_request(url, headers, data):
41
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  @cl.on_chat_start
44
  def setup_multiple_chains():
45
  llm = OpenAI(
46
- model="gpt-3.5-turbo-instruct", # gpt-3.5-turbo-instruct
47
  temperature=0.7,
48
  openai_api_key=OPENAI_API_KEY,
49
  max_tokens=2048,
@@ -54,8 +87,6 @@ def setup_multiple_chains():
54
 
55
  conversation_memory = ConversationBufferMemory(
56
  memory_key="chat_history",
57
- #input_key="question", # ?
58
- #output_key="text", # ?
59
  max_len=30,
60
  return_messages=True
61
  )
@@ -72,12 +103,12 @@ def setup_multiple_chains():
72
  async def handle_message(message: cl.Message):
73
  user_message = message.content
74
  llm_chain = cl.user_session.get("llm_chain")
75
-
76
- booking_pattern = r'\b[A-Z]{6}\d{6}\b'
77
  match = re.search(booking_pattern, user_message)
78
 
79
  if match:
80
-
81
  bestillingskode = match.group()
82
  headers = {
83
  "Authorization": auth_token,
@@ -86,43 +117,52 @@ async def handle_message(message: cl.Message):
86
  payload = {"booking_id": bestillingskode}
87
 
88
  try:
89
-
90
  response = await async_post_request(API_URL, headers, payload)
91
- response.raise_for_status()
92
  booking_data = response.json()
93
 
94
  if "booking_id" in booking_data:
95
-
96
- table = (
97
- "| Field | Info |\n"
98
- "|:-----------|:---------------------|\n"
99
- f"| Booking ID | {booking_data.get('booking_id', 'N/A')} |\n"
100
- f"| Full Name | {booking_data.get('full_name', 'N/A')} |\n"
101
- f"| Amount | {booking_data.get('amount', 0)} kr |\n"
102
- f"| Check-in | {booking_data.get('checkin', 'N/A')} |\n"
103
- f"| Check-out | {booking_data.get('checkout', 'N/A')} |\n"
104
- f"| Address | {booking_data.get('address', 'N/A')} |\n"
105
- f"| User ID | {booking_data.get('user_id', 0)} |\n"
106
- f"| Info Text | {booking_data.get('infotext', 'N/A')} |\n"
107
- f"| Included | {booking_data.get('included', 'N/A')} |"
108
- )
109
-
110
- await cl.Message(content=table).send()
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  else:
113
- await cl.Message(content="Booking not found or invalid response.").send()
114
-
115
- except requests.exceptions.RequestException as e:
116
- await cl.Message(content=f"Request failed: {str(e)}").send()
117
  else:
118
  try:
119
  response = await llm_chain.ainvoke({
120
  "question": user_message,
121
  "chat_history": ""
122
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
123
-
124
  await cl.Message(content=response["text"]).send()
125
-
126
  except Exception as e:
127
  await cl.Message(content=f"Error: {str(e)}").send()
128
 
 
1
  # ===========================================
2
+ # title: daysoff-assistant-API-v2
3
+ # file: app.py
4
  # ===========================================
5
 
6
  import json
 
16
  from langchain_core.prompts import PromptTemplate
17
  from langchain.memory.buffer import ConversationBufferMemory
18
 
19
+ # ---------------------------------------------------for backend looks, example file:----------------------------------
20
+
21
+ #with open('/usr/local/lib/python3.10/site-packages/transformers/utils/chat_template_utils.py', 'r') as file:
22
+ #content = file.read()
23
+ #print("base.py:", content)
24
+ # ------------------------------------------------------the end--------------------------------------------------------
25
+
26
  load_dotenv()
27
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
28
  auth_token = os.environ.get("DAYSOFF_API_TOKEN")
29
 
30
  API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
31
 
 
32
  daysoff_assistant_template = """
33
  You are a customer support assistant for Daysoff kundeservice and help users retrieve booking information associated with their booking IDs.
34
+ By default, you respond using Norwegian bokmรฅl.
35
+ Provide a conversational answer.
36
+ This way you directly address the user's question in a manner that reflects the professionalism and warmth
37
+ of a customer support representative (female).
38
  Chat History: {chat_history}
39
  Question: {question}
40
  Answer:
 
48
  async def async_post_request(url, headers, data):
49
  return await asyncio.to_thread(requests.post, url, headers=headers, json=data)
50
 
51
+ @cl.set_starters
52
+ async def set_starters():
53
+ return [
54
+ cl.Starter(
55
+ label="Booking ID request",
56
+ message="Kan du gi meg info om en reservasjon?",
57
+ icon="/public/booking_id.svg",
58
+ ),
59
+ cl.Starter(
60
+ label="Metric Space Self-Identity Framework",
61
+ message="Explain the Metric Space Self-Identity Framework like I'm five years old.",
62
+ icon="/public/learn.svg",
63
+ ),
64
+ cl.Starter(
65
+ label="Python script for daily email reports",
66
+ message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.",
67
+ icon="/public/terminal.svg",
68
+ ),
69
+ cl.Starter(
70
+ label="Morning routine ideation",
71
+ message="Can you help me create a personalized Yoga/pranayama/meditation morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.",
72
+ icon="/public/idea.svg",
73
+ )
74
+ ]
75
+
76
  @cl.on_chat_start
77
  def setup_multiple_chains():
78
  llm = OpenAI(
79
+ model="gpt-3.5-turbo-instruct",
80
  temperature=0.7,
81
  openai_api_key=OPENAI_API_KEY,
82
  max_tokens=2048,
 
87
 
88
  conversation_memory = ConversationBufferMemory(
89
  memory_key="chat_history",
 
 
90
  max_len=30,
91
  return_messages=True
92
  )
 
103
  async def handle_message(message: cl.Message):
104
  user_message = message.content
105
  llm_chain = cl.user_session.get("llm_chain")
106
+
107
+ booking_pattern = r'\b[A-Z]{6}\d{6}\b'
108
  match = re.search(booking_pattern, user_message)
109
 
110
  if match:
111
+
112
  bestillingskode = match.group()
113
  headers = {
114
  "Authorization": auth_token,
 
117
  payload = {"booking_id": bestillingskode}
118
 
119
  try:
120
+
121
  response = await async_post_request(API_URL, headers, payload)
122
+ response.raise_for_status()
123
  booking_data = response.json()
124
 
125
  if "booking_id" in booking_data:
126
+
127
+ try:
128
+ table = (
129
+ "| ๐‘ญ๐’Š๐’†๐’๐’… | ๐—œ๐—ป๐—ณ๐—ผ |\n"
130
+ "|:-----------|:---------------------|\n"
131
+ f"| ๐™ฑ๐šŽ๐šœ๐š๐š’๐š•๐š•๐š’๐š—๐š๐šœ๐š”๐š˜๐š๐šŽ | {booking_data.get('booking_id', 'N/A')} |\n"
132
+ f"| ๐™๐™ช๐™ก๐™ก ๐™‰๐™–๐™ข๐™š | {booking_data.get('full_name', 'N/A')} |\n"
133
+ f"| ๐˜ผ๐™ข๐™ค๐™ช๐™ฃ๐™ฉ | {booking_data.get('amount', 0)} kr |\n"
134
+ f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ž๐™ฃ | {booking_data.get('checkin', 'N/A')} |\n"
135
+ f"| ๐˜พ๐™๐™š๐™˜๐™ -๐™ค๐™ช๐™ฉ | {booking_data.get('checkout', 'N/A')} |\n"
136
+ f"| ๐˜ผ๐™™๐™™๐™ง๐™š๐™จ๐™จ | {booking_data.get('address', 'N/A')} |\n"
137
+ f"| ๐™๐™จ๐™š๐™ง ๐™„๐˜ฟ | {booking_data.get('user_id', 0)} |\n"
138
+ f"| ๐™„๐™ฃ๐™›๐™ค ๐™๐™š๐™ญ๐™ฉ | {booking_data.get('infotext', 'N/A')} |\n"
139
+ f"| ๐™„๐™ฃ๐™˜๐™ก๐™ช๐™™๐™š๐™™ | {booking_data.get('included', 'N/A')} |"
140
+ )
141
+
142
+ response = await llm_chain.ainvoke({
143
+ "table": table,
144
+ "question": user_message,
145
+ "chat_history": ""
146
+ }, callbacks=[cl.AsyncLangchainCallbackHandler()])
147
+
148
+ await cl.Message(content=table,["text"]).send()
149
+
150
+ except Exception as e:
151
+ except requests.exceptions.RequestException as e:
152
+ await cl.Message(content=f"Request failed: {str(e)}").send()
153
+
154
 
155
  else:
156
+ await cl.Message(content="Booking information not found.").send()
 
 
 
157
  else:
158
  try:
159
  response = await llm_chain.ainvoke({
160
  "question": user_message,
161
  "chat_history": ""
162
  }, callbacks=[cl.AsyncLangchainCallbackHandler()])
163
+
164
  await cl.Message(content=response["text"]).send()
165
+
166
  except Exception as e:
167
  await cl.Message(content=f"Error: {str(e)}").send()
168