camparchimedes commited on
Commit
67cba93
Β·
verified Β·
1 Parent(s): 957f6d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -7
app.py CHANGED
@@ -3,14 +3,20 @@
3
  # file: app.py
4
  # ===========================================
5
 
6
- import json
7
- import asyncio
8
  import os
9
  import re
 
 
 
 
10
  import requests
 
 
11
  from dotenv import load_dotenv
 
12
  import chainlit as cl
13
  from chainlit import user_session
 
14
  from langchain import hub
15
  from langchain_openai import OpenAI
16
  from langchain.chains import LLMChain
@@ -26,7 +32,6 @@ from langchain.memory.buffer import ConversationBufferMemory
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
  #If booking information is requested, and with
@@ -80,14 +85,31 @@ async def set_starters():
80
  )
81
  ]
82
 
83
- @cl.on_chat_start
84
- #def setup_multiple_chains():
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def init_session():
86
  user_session.set('chat_history', [])
87
  print("chat_history: ", user_session.get('chat_history'))
88
  user_session.set('path_out', create_output_path())
89
  print("path_out: ", user_session.get('path_out'))
90
 
 
 
 
 
91
  llm = OpenAI(
92
  model="gpt-3.5-turbo-instruct",
93
  temperature=0.7,
@@ -111,7 +133,7 @@ def init_session():
111
 
112
  @cl.on_message
113
  async def handle_message(message: cl.Message):
114
-
115
  user_message = message.content
116
  llm_chain = cl.user_session.get("llm_chain")
117
  memory = cl.user_session.get("memory")
@@ -145,7 +167,7 @@ async def handle_message(message: cl.Message):
145
  await cl.Message(content=f"Error processing booking data: {str(e)}").send()
146
 
147
  else:
148
- await cl.Message(content="Booking not found or invalid response.").send()
149
 
150
  except requests.exceptions.RequestException as e:
151
  await cl.Message(content=f"Request failed: {str(e)}").send()
 
3
  # file: app.py
4
  # ===========================================
5
 
 
 
6
  import os
7
  import re
8
+ import uuid
9
+ import time
10
+ import json
11
+ import asyncio
12
  import requests
13
+ from pathlib import Path
14
+ from datetime import datetime
15
  from dotenv import load_dotenv
16
+
17
  import chainlit as cl
18
  from chainlit import user_session
19
+
20
  from langchain import hub
21
  from langchain_openai import OpenAI
22
  from langchain.chains import LLMChain
 
32
  load_dotenv()
33
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
34
  auth_token = os.environ.get("DAYSOFF_API_TOKEN")
 
35
  API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
36
 
37
  #If booking information is requested, and with
 
85
  )
86
  ]
87
 
88
+ def create_output_path(user=None):
89
+ this_uuid = str(uuid.uuid4())
90
+ ts = round(time.time())
91
+ dt_object = datetime.fromtimestamp(ts)
92
+ date = str(dt_object).replace(" ","_").replace(":","-")
93
+
94
+ base_path = os.environ.get('HOME', '/home/user')
95
+ logs_path = Path(f"{base_path}/logs")
96
+
97
+ path_out = logs_path / f"{date}--{user or 'default'}--{this_uuid}"
98
+ path_out.mkdir(exist_ok=True, parents=True)
99
+
100
+ return path_out
101
+
102
+ @cl.on_chat_start # <β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€” the Chainlit context
103
  def init_session():
104
  user_session.set('chat_history', [])
105
  print("chat_history: ", user_session.get('chat_history'))
106
  user_session.set('path_out', create_output_path())
107
  print("path_out: ", user_session.get('path_out'))
108
 
109
+ #cl.user_session.set("session_created_at", datetime.now())
110
+ #cl.user_session.set("connection_attempts", 0)
111
+ #cl.user_session.set("connection_attempts", connection_attempts + 1)
112
+
113
  llm = OpenAI(
114
  model="gpt-3.5-turbo-instruct",
115
  temperature=0.7,
 
133
 
134
  @cl.on_message
135
  async def handle_message(message: cl.Message):
136
+ #connection_attempts = cl.user_session.get("connection_attempts", 0)
137
  user_message = message.content
138
  llm_chain = cl.user_session.get("llm_chain")
139
  memory = cl.user_session.get("memory")
 
167
  await cl.Message(content=f"Error processing booking data: {str(e)}").send()
168
 
169
  else:
170
+ await cl.Message(content="Booking not found.").send()
171
 
172
  except requests.exceptions.RequestException as e:
173
  await cl.Message(content=f"Request failed: {str(e)}").send()