-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/gpt-hey/fine_tuned into d…
…ocker
- Loading branch information
Showing
10 changed files
with
70 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler | ||
class WebsocketCallbackHandler(StreamingStdOutCallbackHandler): | ||
_instance = None | ||
def __new__(cls): | ||
if cls._instance is None: | ||
cls._instance = super(WebsocketCallbackHandler, cls).__new__(cls) | ||
# Initialize the singleton instance here | ||
return cls._instance | ||
|
||
def set_websocket(self, websocket): | ||
def __init__(self, websocket): | ||
self.websocket = websocket | ||
async def on_llm_new_token(self, token, **kwargs): | ||
"""Run on new LLM token. Only available when streaming is enabled.""" | ||
await self.websocket.send(token) | ||
|
||
websocket_callback_singleton = WebsocketCallbackHandler() | ||
await self.websocket.send(token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,55 @@ | ||
import asyncio | ||
import websockets | ||
from callbacks import websocket_callback_singleton | ||
from callbacks import WebsocketCallbackHandler | ||
from models.llama2_gguf_model import Llama2GGUFModel | ||
from remindme_parser import Remindme | ||
|
||
PORT = 8081 | ||
ADDRESS = "0.0.0.0" | ||
|
||
async def handle_websocket_close(websocket): | ||
try: | ||
await websocket.close() | ||
|
||
except websockets.exceptions.ConnectionClosedOK: | ||
# Handle the case where the connection is already closed | ||
pass | ||
except Exception as e: | ||
# Handle other exceptions as needed | ||
print(f"Error: {e}") | ||
|
||
r = Remindme() | ||
gguf_model = Llama2GGUFModel() | ||
async def message(websocket, path): | ||
print(f"Client connected to path: {path}") | ||
websocket_callback_singleton.set_websocket(websocket) | ||
|
||
try: | ||
async for message in websocket: | ||
print(f"Received message from client: {message}") | ||
r = Remindme() | ||
|
||
if r.is_matched(message): | ||
resp = r.execute_action(message) | ||
resp = await asyncio.to_thread(r.execute_action, message) | ||
await websocket.send(f'remindme executed: {resp}') | ||
return | ||
|
||
gguf_model = Llama2GGUFModel() | ||
callback_handler = WebsocketCallbackHandler(websocket) | ||
gguf_model.update_callback_handler(callback_handler) | ||
if gguf_model.is_matched(message): | ||
gguf_model.execute_action(message) | ||
await asyncio.to_thread(gguf_model.execute_action, message) | ||
return | ||
|
||
await websocket.send('voided conversation with no match') | ||
|
||
except websockets.exceptions.ConnectionClosed: | ||
print("Connection closed by the client.") | ||
finally: | ||
await handle_websocket_close(websocket) | ||
|
||
async def main(): | ||
# Start the WebSocket server on 127.0.0.1, port 8081 | ||
server = await websockets.serve(message, "127.0.0.1", PORT) | ||
|
||
print(f"WebSocket server started on ws://127.0.0.1:{PORT}") | ||
|
||
# Keep the server running until it's manually stopped | ||
await server.wait_closed() | ||
async with websockets.serve(message, ADDRESS, PORT): | ||
await asyncio.Future() | ||
print(f"WebSocket server started on ws://{ADDRESS}:{PORT}") | ||
|
||
# Run the WebSocket server | ||
asyncio.run(main()) | ||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters