-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
149 lines (121 loc) · 4.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import chainlit as cl
from dotenv import load_dotenv
from chainlit.types import ThreadDict
from src.select_starters import select_starter
from src.select_chat_profiles import select_chat_profile
from src.select_chat_profiles import initialize_chat_profile
from src.process_user_messages import process_user_message
from src.process_user_audios import process_audio_chunk
from src.process_user_audios import audio_answer
from src.process_user_files import handle_attachment
from src.resume_chats import resume_chat
@cl.password_auth_callback
async def password_auth_callback(username: str) -> cl.User:
"""
Logs in and authenticates all users.
Parameters:
----------
username : str
Username provided by the user during login.
password : str
Password provided by the user during login.
Returns:
-------
User
An instance of the authenticated user with admin access.
"""
load_dotenv()
return cl.User(identifier=username, metadata={'role': 'admin', 'provider': 'credentials'})
@cl.set_chat_profiles
async def chat_profile() -> str:
"""
Allows the user to select a chat profile (LLM model).
Enables the selected model's functionalities within the chatbot.
Returns the selected chat profile (LLM) to be used for chatting.
Returns:
-------
ChatProfile
The chat profile (LLM) selected by the user.
"""
return await select_chat_profile()
@cl.on_chat_start
async def on_chat_start() -> None:
"""
Initializes the chat session with the selected chat profile.
Sets up the session with the chosen model and returns the
chat session with the selected LLM.
Parameters:
----------
None
"""
chat_profile = cl.user_session.get("chat_profile")
await initialize_chat_profile(chat_profile=chat_profile)
@cl.set_starters
async def set_starters() -> list[str]:
"""
Sets and returns a list of starter messages.
These are predefined messages sent to the selected chat profile.
They are randomly assigned for each session.
Returns:
-------
list of str
A list of starter messages to initialize the chat.
"""
return await select_starter()
@cl.on_audio_chunk
async def on_audio_chunk(audio_chunk: cl.AudioChunk) -> None:
"""
Handles incoming audio chunks during user input.
Receives audio chunks, stores the audio data in a buffer, and
updates the session with the buffer.
Parameters:
----------
audio_chunk : AudioChunk
The audio chunk to process.
"""
await process_audio_chunk(audio_chunk=audio_chunk)
@cl.on_audio_end
async def on_audio_end(elements: list) -> None:
"""
Processes the voice message and analyzes user intent.
Converts the audio to text using the selected chat profile.
Handles document analysis (file attachments) and determines
user intent for chatbot functionalities. Returns text and
voice responses depending on attached file types and user intents.
Parameters:
----------
elements : list
A list of elements related to the audio message.
"""
chat_profile = cl.user_session.get("chat_profile")
model_name = await initialize_chat_profile(chat_profile=chat_profile)
await audio_answer(elements=elements, model_name=model_name)
@cl.on_message
async def on_message(user_message: cl.Message) -> None:
"""
Processes text messages, file attachments, and user intent.
Handles text input, detects files in the user's message,
and processes them. It also interacts with the LLM chat profile
to respond based on the attached files and user intent for
chatbot functionalities.
Parameters:
----------
user_message : Message
The incoming message with potential file attachments.
"""
chat_profile = cl.user_session.get("chat_profile")
await handle_attachment(user_message=user_message)
model_name = await initialize_chat_profile(chat_profile=chat_profile)
await process_user_message(user_message=user_message, model_name=model_name)
@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict) -> None:
"""
Resumes archived chat conversations.
Retrieves previous chat threads to load them into memory and
enables users to continue a conversation.
Parameters:
----------
thread : ThreadDict
A dictionary containing the thread's information and messages.
"""
await resume_chat(thread=thread)