Skip to content

Commit

Permalink
refactored core chat functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
c0sogi committed Jul 16, 2023
1 parent f39123d commit 01f53de
Show file tree
Hide file tree
Showing 17 changed files with 6,868 additions and 7,416 deletions.
12 changes: 2 additions & 10 deletions app/utils/api/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,7 @@ async def acreate_completion(
async with ClientSession(
timeout=ChatConfig.timeout
) as session: # initialize client
async with session.post(
url,
headers=headers,
data=data,
) as response:
async with session.post(url, headers=headers, data=data) as response:
await _handle_error_response(response)
if stream:
async for json_data in _extract_json_from_streaming_response(
Expand Down Expand Up @@ -478,11 +474,7 @@ async def acreate_chat_completion(
)
)
async with ClientSession(timeout=ChatConfig.timeout) as session:
async with session.post(
url,
headers=headers,
data=data,
) as response:
async with session.post(url, headers=headers, data=data) as response:
await _handle_error_response(response)
if stream:
async for json_data in _extract_json_from_streaming_response(
Expand Down
58 changes: 35 additions & 23 deletions app/utils/chat/managers/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,48 +118,55 @@ async def _harvest_done_tasks(buffer: BufferedUserContext) -> None:
@asynccontextmanager
async def _chat_cycle_context_manager(buffer: BufferedUserContext):
try:
# Send Start-of-Chat message to client
await asyncio.gather(
SendToWebsocket.init(buffer=buffer, send_tokens=True),
_harvest_done_tasks(buffer=buffer),
)

# Run a chat cycle
yield
except ChatException as chat_exception:
if isinstance(chat_exception, ChatTextGenerationException):
# Send error message to client when text generation fails
await SendToWebsocket.message(
websocket=buffer.websocket,
msg=f"\n\nAn error occurred while generating text: **{chat_exception.msg}**",
chat_room_id=buffer.current_chat_room_id,
finish=True,
model_name=buffer.current_user_chat_context.llm_model.value.name,
)
elif isinstance(chat_exception, ChatInterruptedException):
if buffer.done.is_set():
# If the chat is interrupted outside of AI's turn, then
# we need to send EOS to the client.
buffer.done.clear()
await SendToWebsocket.message(
websocket=buffer.websocket,
msg="",
chat_room_id=buffer.current_chat_room_id,
finish=True,
model_name=buffer.current_user_chat_context.llm_model.value.name,
)

if chat_exception.msg:
await MessageManager.add_message_history_safely(
user_chat_context=buffer.current_user_chat_context,
role=ChatRoles.AI,
content=chat_exception.msg,
)
elif (
isinstance(chat_exception, ChatInterruptedException)
and chat_exception.msg
):
# if msg is not None, save the message generated before interruption
await MessageManager.add_message_history_safely(
user_chat_context=buffer.current_user_chat_context,
role=ChatRoles.AI,
content=chat_exception.msg,
)
else:
# Send error message to client when other errors occur
await SendToWebsocket.message(
websocket=buffer.websocket,
msg=str(chat_exception.msg),
chat_room_id=buffer.current_chat_room_id,
) # send too much token exception message to websocket
)
finally:
# Clean up buffer
buffer.done.clear()
buffer.optional_info["uuid"] = None

# Send End-of-Chat message to client
await SendToWebsocket.message(
websocket=buffer.websocket,
msg="",
chat_room_id=buffer.current_chat_room_id,
finish=True,
model_name=buffer.current_user_chat_context.llm_model.value.name,
)


async def _change_context(
buffer: BufferedUserContext, changed_chat_room_id: str
Expand Down Expand Up @@ -267,7 +274,7 @@ async def _websocket_receiver(buffer: BufferedUserContext) -> None:
await buffer.queue.put(
await VectorStoreManager.embed_file_to_vectorstore(
file=received_bytes,
filename=buffer.optional_info.get("filename", ""),
filename=buffer.optional_info.get("filename") or "",
collection_name=buffer.current_user_chat_context.user_id,
)
)
Expand All @@ -291,13 +298,18 @@ async def _websocket_sender(buffer: BufferedUserContext) -> None:
buffer.optional_info["uuid"] = item.uuid
buffer.optional_info["translate"] = item.translate
splitted: list[str] = item.msg[1:].split(" ")
if not item.msg.startswith("/retry"):
if not item.msg.startswith("/") or not any(
(
item.msg[1:].startswith(command)
for command in ChatCommands.special_commands
)
):
buffer.last_user_message = item.msg
await _interruption_event_watcher(
MessageHandler.command(
callback_name=splitted[0],
callback_args=splitted[1:],
callback_finder=ChatCommands._find_callback_with_command,
callback_finder=ChatCommands.find_callback_with_command,
buffer=buffer,
),
event=buffer.done,
Expand Down
6 changes: 3 additions & 3 deletions app/web/flutter_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const RESOURCES = {"assets/AssetManifest.bin": "284b20e6d03cd963cd59b97cd5c30232
"icons/Icon-512.png": "c9242a8fc92d16303b04ac108c9ac581",
"icons/Icon-maskable-192.png": "710e5d264b437fe72bdb99ba514e2266",
"icons/Icon-maskable-512.png": "c9242a8fc92d16303b04ac108c9ac581",
"index.html": "bb4ce1c60128741b67981dbaebffc493",
"/": "bb4ce1c60128741b67981dbaebffc493",
"main.dart.js": "7eb5b144c528dc3418a18d5a6e43dd8f",
"index.html": "250e17f831a60e3b3bf7001ce2419cc3",
"/": "250e17f831a60e3b3bf7001ce2419cc3",
"main.dart.js": "c047b86ea714cdafd09c239ea9f6f419",
"manifest.json": "06f358efd455414c8c1e8618081bce05",
"site_logo.png": "0fb92091fe8696aa5cfeafb0d1a01cbc",
"version.json": "9094aacdae789dccd67fa32109ff1a18"};
Expand Down
2 changes: 1 addition & 1 deletion app/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<script>
// The value below is injected by flutter build, do not touch.
var serviceWorkerVersion = "3199002286";
var serviceWorkerVersion = "2626822474";
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
Expand Down
Loading

0 comments on commit 01f53de

Please sign in to comment.