camparchimedes commited on
Commit
46de342
ยท
verified ยท
1 Parent(s): a06635d

Update api_data.py

Browse files
Files changed (1) hide show
  1. api_data.py +10 -34
api_data.py CHANGED
@@ -1,10 +1,6 @@
1
  # ================================
2
- # {๐——๐—ฎ๐˜†๐˜€๐—ผ๐—ณ๐—ณ แต‰หฃแต–แตƒโฟหขแต‰ หขสธหขแต—แต‰แต} | ๐˜ด๐˜ถ๐˜ฑ๐˜ฑ๐˜ฐ๐˜ณ๐˜ต
3
  # ================================
4
- # V2 | try: chainlit==1.3.0 for better inteerface
5
- # โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”>
6
-
7
- # โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”>
8
 
9
  import json
10
  import asyncio
@@ -14,19 +10,9 @@ import requests
14
  import chainlit as cl
15
  from dotenv import load_dotenv
16
 
17
-
18
- from chromadb.config import Settings
19
- from chromadb import Client
20
-
21
- # Initialize ChromaDB client
22
- chroma_client = Client(Settings(chroma_db_impl="duckdb+parquet", persist_directory="./session_data"))
23
-
24
-
25
-
26
  # --------------------------------=== API URL ===-----------------------------------
27
  API_URL = "https://firmahytte.daysoff.no/api/bookings"
28
 
29
-
30
  # --------------------------------=== environment ===-------------------------------
31
  load_dotenv()
32
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
@@ -64,12 +50,12 @@ async def async_post_request(url, headers, data):
64
  async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
65
 
66
  user_message = message.content # -- doc: extract message text
67
- booking_pattern = r'\b[A-Z]{6}\d{6}\b' # -- raw string (r'')
68
  match = re.search(booking_pattern, user_message)
69
 
70
  if match:
71
  preliminary = (
72
- f"Takk for bookingnummer ๐Ÿ‘๐Ÿป " # โ€”โ€”> f"Takk for bookingnummer \\b[A-Z]{{6}}\\d{{6}} ", OBS! โ†’ \b, {}, and \d must be double-escaped inside an f-string.
73
  "Jeg skal sjekke dette for deg nรฅ. Vennligst et รธyeblikk bare..\n"
74
  )
75
  for token in preliminary.split():
@@ -88,10 +74,11 @@ async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
88
  }
89
 
90
  try:
 
91
  response = await async_post_request(API_URL, headers, payload)
92
  response.raise_for_status()
93
  response_data = response.json()
94
- booking_data = response_data.get('data', {})
95
 
96
  print("API Response Data:", json.dumps(booking_data, indent=2))
97
 
@@ -100,18 +87,7 @@ async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
100
  if "error" in booking_data:
101
  raise APIResponseError(booking_data["error"])
102
 
103
-
104
- # Store booking data in ChromaDB
105
- collection = chroma_client.create_collection(name=f"session_{message.user_id}")
106
- collection.add(
107
- documents=[json.dumps(booking_data)],
108
- metadatas=[{"booking_number": booking_number}],
109
- ids=[booking_number]
110
- )
111
-
112
-
113
-
114
- table = (
115
  "\n\n## ๐˜ฝ๐™ค๐™ค๐™ ๐™ž๐™ฃ๐™œ ๐™ž๐™ฃ๐™›๐™ค๐™ง๐™ข๐™–๐™จ๐™Ÿ๐™ค๐™ฃ\n\n"
116
  f"| ๐ต๐‘œ๐‘œ๐‘˜๐‘–๐‘›๐‘”๐‘›๐‘ข๐‘š๐‘š๐‘’๐‘Ÿ | {booking_data.get('order_number', 'N/A')} |\n"
117
  f"|:-----|:------------------|\n"
@@ -141,14 +117,14 @@ async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
141
  await msg.stream_token(table)
142
  await asyncio.sleep(0.07)
143
  await msg.send()
144
-
145
  header = (
146
  "\n\n# ๐—ฉ๐—ถ๐—ธ๐˜๐—ถ๐—ด ๐—œ๐—ป๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐˜€๐—ท๐—ผ๐—ป ๐—ผ๐—บ ๐—ข๐—ฝ๐—ฝ๐—ต๐—ผ๐—น๐—ฑ๐—ฒ๐˜\n\n"
147
  )
148
  await msg.stream_token(header)
149
  await asyncio.sleep(0.07)
150
  await msg.send()
151
-
152
  # ๐˜ฟ๐™š๐™จ๐™˜๐™ง๐™ž๐™ฅ๐™ฉ๐™ž๐™ค๐™ฃ
153
  markdown = (
154
  "\n\n# ๐Ÿ‡ฉ ๐Ÿ‡ช ๐Ÿ‡ธ ๐Ÿ‡จ ๐Ÿ‡ท ๐Ÿ‡ฎ ๐Ÿ‡ต ๐Ÿ‡น ๐Ÿ‡ฎ ๐Ÿ‡ด ๐Ÿ‡ณ\n\n"
@@ -168,7 +144,7 @@ async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
168
  await asyncio.sleep(0.07)
169
  await msg.send()
170
 
171
-
172
  except (APIConnectionError, APIResponseError, BookingNotFoundError) as e:
173
  error_msg = (
174
  f"๐Ÿ™„ {str(e)} "
@@ -177,7 +153,7 @@ async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
177
  await msg.stream_token(token + " ")
178
  #await asyncio.sleep(0.01)
179
  await msg.send()
180
- return True # --โ€”โ€”> returns True, bcoz handled booking query even if with error
181
 
182
  except requests.exceptions.RequestException as e:
183
  unexpected = (
 
1
  # ================================
2
+ # api-data.py
3
  # ================================
 
 
 
 
4
 
5
  import json
6
  import asyncio
 
10
  import chainlit as cl
11
  from dotenv import load_dotenv
12
 
 
 
 
 
 
 
 
 
 
13
  # --------------------------------=== API URL ===-----------------------------------
14
  API_URL = "https://firmahytte.daysoff.no/api/bookings"
15
 
 
16
  # --------------------------------=== environment ===-------------------------------
17
  load_dotenv()
18
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
50
  async def booking_agent_system(message: cl.Message, msg: cl.Message) -> bool:
51
 
52
  user_message = message.content # -- doc: extract message text
53
+ booking_pattern = r'\b[A-Z]{6}\d{6}\b' # -- raw string (r'')
54
  match = re.search(booking_pattern, user_message)
55
 
56
  if match:
57
  preliminary = (
58
+ f"Takk for bookingnummer ๐Ÿ‘๐Ÿป "
59
  "Jeg skal sjekke dette for deg nรฅ. Vennligst et รธyeblikk bare..\n"
60
  )
61
  for token in preliminary.split():
 
74
  }
75
 
76
  try:
77
+
78
  response = await async_post_request(API_URL, headers, payload)
79
  response.raise_for_status()
80
  response_data = response.json()
81
+ booking_data = response_data.get('data', {})
82
 
83
  print("API Response Data:", json.dumps(booking_data, indent=2))
84
 
 
87
  if "error" in booking_data:
88
  raise APIResponseError(booking_data["error"])
89
 
90
+ table = (
 
 
 
 
 
 
 
 
 
 
 
91
  "\n\n## ๐˜ฝ๐™ค๐™ค๐™ ๐™ž๐™ฃ๐™œ ๐™ž๐™ฃ๐™›๐™ค๐™ง๐™ข๐™–๐™จ๐™Ÿ๐™ค๐™ฃ\n\n"
92
  f"| ๐ต๐‘œ๐‘œ๐‘˜๐‘–๐‘›๐‘”๐‘›๐‘ข๐‘š๐‘š๐‘’๐‘Ÿ | {booking_data.get('order_number', 'N/A')} |\n"
93
  f"|:-----|:------------------|\n"
 
117
  await msg.stream_token(table)
118
  await asyncio.sleep(0.07)
119
  await msg.send()
120
+
121
  header = (
122
  "\n\n# ๐—ฉ๐—ถ๐—ธ๐˜๐—ถ๐—ด ๐—œ๐—ป๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐˜€๐—ท๐—ผ๐—ป ๐—ผ๐—บ ๐—ข๐—ฝ๐—ฝ๐—ต๐—ผ๐—น๐—ฑ๐—ฒ๐˜\n\n"
123
  )
124
  await msg.stream_token(header)
125
  await asyncio.sleep(0.07)
126
  await msg.send()
127
+
128
  # ๐˜ฟ๐™š๐™จ๐™˜๐™ง๐™ž๐™ฅ๐™ฉ๐™ž๐™ค๐™ฃ
129
  markdown = (
130
  "\n\n# ๐Ÿ‡ฉ ๐Ÿ‡ช ๐Ÿ‡ธ ๐Ÿ‡จ ๐Ÿ‡ท ๐Ÿ‡ฎ ๐Ÿ‡ต ๐Ÿ‡น ๐Ÿ‡ฎ ๐Ÿ‡ด ๐Ÿ‡ณ\n\n"
 
144
  await asyncio.sleep(0.07)
145
  await msg.send()
146
 
147
+
148
  except (APIConnectionError, APIResponseError, BookingNotFoundError) as e:
149
  error_msg = (
150
  f"๐Ÿ™„ {str(e)} "
 
153
  await msg.stream_token(token + " ")
154
  #await asyncio.sleep(0.01)
155
  await msg.send()
156
+ return True
157
 
158
  except requests.exceptions.RequestException as e:
159
  unexpected = (