Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
-
#
|
2 |
-
#
|
3 |
# chainlit==1.1.404
|
4 |
-
#
|
5 |
-
#
|
|
|
6 |
|
7 |
import os
|
8 |
import re
|
@@ -18,7 +19,7 @@ from dotenv import load_dotenv
|
|
18 |
import chainlit as cl
|
19 |
from chainlit import user_session
|
20 |
from chainlit.context import init_ws_context
|
21 |
-
from chainlit.session import WebsocketSession
|
22 |
from langchain import hub
|
23 |
from langchain_openai import OpenAI
|
24 |
from langchain.chains import LLMChain
|
@@ -32,51 +33,36 @@ from langchain_core.messages import BaseMessage
|
|
32 |
from pydantic import BaseModel, Field
|
33 |
from typing import List
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
#chat_histories = {}
|
46 |
-
|
47 |
-
#def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
48 |
-
#if session_id not in chat_histories:
|
49 |
-
#chat_histories[session_id] = InMemoryHistory()
|
50 |
-
#return chat_histories[session_id]
|
51 |
-
|
52 |
-
# ---------------------------------------------------for backend looks, example file:----------------------------------
|
53 |
-
#with open('/home/user/.local/lib/python3.10/site-packages/socketio/async_server.py', 'r') as file:
|
54 |
-
#content = file.read()
|
55 |
-
#print("see line 640:", content)
|
56 |
-
# ------------------------------------------------------the end--------------------------------------------------------
|
57 |
|
|
|
|
|
|
|
|
|
58 |
|
59 |
load_dotenv()
|
60 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
61 |
auth_token = os.environ.get("DAYSOFF_API_TOKEN")
|
62 |
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
63 |
|
64 |
-
#If booking information is requested, and with
|
65 |
-
#retrieved booking information: {table} in mind, provide a conversational answer.
|
66 |
-
#If no booking information is requested, provide a conversational answer.
|
67 |
-
#combined_message = f"### Informasjon for Bestillingskode:\n\n{table}"
|
68 |
-
|
69 |
daysoff_assistant_template = """
|
70 |
-
You are a customer support assistant for Daysoff
|
71 |
-
|
72 |
-
|
73 |
-
This way you directly address the user's question in a manner that reflects the professionalism and warmth
|
74 |
-
of a customer support representative (female).
|
75 |
============================
|
76 |
Chat History: {chat_history}
|
77 |
Question: {question}
|
78 |
============================
|
79 |
-
Answer
|
80 |
"""
|
81 |
|
82 |
daysoff_assistant_prompt = PromptTemplate(
|
@@ -114,21 +100,16 @@ async def set_starters():
|
|
114 |
|
115 |
|
116 |
|
117 |
-
@cl.on_chat_start # <βββββββββββββββββ
|
118 |
def setup_multiple_chains():
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
# --config Chainlit logging
|
125 |
-
cl.config.run.log_level = "INFO"
|
126 |
print("Session id:", cl.user_session.get("id"))
|
127 |
-
|
128 |
-
|
129 |
-
#
|
130 |
-
|
131 |
-
#init_ws_context(ws_session)
|
132 |
|
133 |
llm = OpenAI(
|
134 |
model="gpt-3.5-turbo-instruct",
|
@@ -172,14 +153,12 @@ async def handle_message(message: cl.Message):
|
|
172 |
payload = {"booking_id": bestillingskode}
|
173 |
|
174 |
try:
|
175 |
-
# --async POST request
|
176 |
response = await async_post_request(API_URL, headers, payload)
|
177 |
response.raise_for_status()
|
178 |
booking_data = response.json()
|
179 |
|
180 |
if "booking_id" in booking_data:
|
181 |
try:
|
182 |
-
# --markdown_table
|
183 |
table = (
|
184 |
"| πππππ
| ππ»π³πΌ |\n"
|
185 |
"|:-----------|:---------------------|\n"
|
@@ -194,7 +173,6 @@ async def handle_message(message: cl.Message):
|
|
194 |
f"| ππ£ππ‘πͺπππ | {booking_data.get('included', 'N/A')} |"
|
195 |
)
|
196 |
|
197 |
-
# --send both as combined_message
|
198 |
combined_message = f"### Her er informasjon for {bestillingskode}:\n\n{table}"
|
199 |
await cl.Message(content=combined_message).send()
|
200 |
|
@@ -215,8 +193,6 @@ async def handle_message(message: cl.Message):
|
|
215 |
"chat_history": ""
|
216 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
217 |
|
218 |
-
# Send the LLM response as a message
|
219 |
-
#await cl.Message(content=response.get("text")).send()
|
220 |
await cl.Message(content=response["text"]).send()
|
221 |
|
222 |
except Exception as e:
|
|
|
1 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ>
|
2 |
+
# Comment: various test-blocks to prevent session-outage with more recent UI;
|
3 |
# chainlit==1.1.404
|
4 |
+
# new imports: user_session, init_ws_context, chainlit.session/ WebsocketSession
|
5 |
+
# ref--'Session is disconnected' rt7E1rI_uhficQm8AAAA ..
|
6 |
+
# <βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
7 |
|
8 |
import os
|
9 |
import re
|
|
|
19 |
import chainlit as cl
|
20 |
from chainlit import user_session
|
21 |
from chainlit.context import init_ws_context
|
22 |
+
from chainlit.session import WebsocketSession
|
23 |
from langchain import hub
|
24 |
from langchain_openai import OpenAI
|
25 |
from langchain.chains import LLMChain
|
|
|
33 |
from pydantic import BaseModel, Field
|
34 |
from typing import List
|
35 |
|
36 |
+
class InMemoryHistory(BaseChatMessageHistory, BaseModel):
|
37 |
+
messages: List[BaseMessage] = Field(default_factory=list)
|
38 |
|
39 |
+
def add_messages(self, messages: List[BaseMessage]) -> None:
|
40 |
+
self.messages.extend(messages)
|
41 |
|
42 |
+
def clear(self) -> None:
|
43 |
+
self.messages = []
|
44 |
|
45 |
+
chat_histories = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
48 |
+
if session_id not in chat_histories:
|
49 |
+
chat_histories[session_id] = InMemoryHistory()
|
50 |
+
return chat_histories[session_id]
|
51 |
|
52 |
load_dotenv()
|
53 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
54 |
auth_token = os.environ.get("DAYSOFF_API_TOKEN")
|
55 |
API_URL = "https://aivisions.no/data/daysoff/api/v1/booking/"
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
daysoff_assistant_template = """
|
58 |
+
You are a customer support assistant for Daysoff ('Daysoff Kundeservice AI Support') who helps users retrieve booking information based on their bookingnummer.
|
59 |
+
You should concisely use the term βbookingnummerβ. Maintain a friendly and professional tone, **reflecting the warmth of a female customer support
|
60 |
+
representative archetype.** By default, you answer in **Norwegian**.
|
|
|
|
|
61 |
============================
|
62 |
Chat History: {chat_history}
|
63 |
Question: {question}
|
64 |
============================
|
65 |
+
Answer:
|
66 |
"""
|
67 |
|
68 |
daysoff_assistant_prompt = PromptTemplate(
|
|
|
100 |
|
101 |
|
102 |
|
103 |
+
@cl.on_chat_start # <βββββββββββββββββ Chainlit context | cl.config.run.log_level = "INFO"
|
104 |
def setup_multiple_chains():
|
105 |
+
cl.user_session.set("connection_attempts", 0)
|
106 |
+
cl.user_session.set("last_connection_time", datetime.now())
|
|
|
107 |
|
|
|
|
|
|
|
108 |
print("Session id:", cl.user_session.get("id"))
|
109 |
+
|
110 |
+
Session id = "37223c22-1ea4-48f5-880f-8201ae67034b" # session_id: str
|
111 |
+
ws_session = WebsocketSession.get_by_id(session_id=session_id) # find other methods for WebsocketSession() to prevent oytage
|
112 |
+
init_ws_context(ws_session)
|
|
|
113 |
|
114 |
llm = OpenAI(
|
115 |
model="gpt-3.5-turbo-instruct",
|
|
|
153 |
payload = {"booking_id": bestillingskode}
|
154 |
|
155 |
try:
|
|
|
156 |
response = await async_post_request(API_URL, headers, payload)
|
157 |
response.raise_for_status()
|
158 |
booking_data = response.json()
|
159 |
|
160 |
if "booking_id" in booking_data:
|
161 |
try:
|
|
|
162 |
table = (
|
163 |
"| πππππ
| ππ»π³πΌ |\n"
|
164 |
"|:-----------|:---------------------|\n"
|
|
|
173 |
f"| ππ£ππ‘πͺπππ | {booking_data.get('included', 'N/A')} |"
|
174 |
)
|
175 |
|
|
|
176 |
combined_message = f"### Her er informasjon for {bestillingskode}:\n\n{table}"
|
177 |
await cl.Message(content=combined_message).send()
|
178 |
|
|
|
193 |
"chat_history": ""
|
194 |
}, callbacks=[cl.AsyncLangchainCallbackHandler()])
|
195 |
|
|
|
|
|
196 |
await cl.Message(content=response["text"]).send()
|
197 |
|
198 |
except Exception as e:
|