23
23
import traceback
24
24
from typing import Any , Dict , List
25
25
26
- import uvicorn
27
26
from fastapi import FastAPI , Header , WebSocket
28
27
from fastapi .middleware .cors import CORSMiddleware
29
28
from openai import OpenAI
30
29
from pydantic import BaseModel
30
+ from uvicorn import Config , Server
31
31
32
32
# import argparse
33
33
# from profiles.default import interpreter
@@ -63,8 +63,6 @@ def __init__(self, interpreter):
63
63
# engine = OpenAIEngine()
64
64
# self.tts = TextToAudioStream(engine)
65
65
66
- self .active_chat_messages = []
67
-
68
66
# Clock
69
67
# clock()
70
68
@@ -82,7 +80,9 @@ def __init__(self, interpreter):
82
80
False # Tracks whether interpreter is trying to use the keyboard
83
81
)
84
82
85
- self .loop = asyncio .get_event_loop ()
83
+ # print("oksskk")
84
+ # self.loop = asyncio.get_event_loop()
85
+ # print("okkk")
86
86
87
87
async def _add_to_queue (self , queue , item ):
88
88
print (f"Adding item to output" , item )
@@ -134,7 +134,6 @@ async def run(self):
134
134
Runs OI on the audio bytes submitted to the input. Will add streaming LMC chunks to the _output_queue.
135
135
"""
136
136
print ("heyyyy" )
137
- self .interpreter .messages = self .active_chat_messages
138
137
# interpreter.messages = self.active_chat_messages
139
138
# self.beeper.start()
140
139
@@ -147,10 +146,8 @@ async def run(self):
147
146
148
147
def generate (message ):
149
148
last_lmc_start_flag = self ._last_lmc_start_flag
150
- self .interpreter .messages = self .active_chat_messages
151
149
# interpreter.messages = self.active_chat_messages
152
150
print ("🍀🍀🍀🍀GENERATING, using these messages: " , self .interpreter .messages )
153
- print ("🍀 🍀 🍀 🍀 active_chat_messages: " , self .active_chat_messages )
154
151
print ("passing this in:" , message )
155
152
for chunk in self .interpreter .chat (message , display = False , stream = True ):
156
153
print ("FROM INTERPRETER. CHUNK:" , chunk )
@@ -165,7 +162,10 @@ def generate(message):
165
162
166
163
# Handle message blocks
167
164
if chunk .get ("type" ) == "message" :
168
- self .add_to_output_queue_sync (chunk ) # To send text, not just audio
165
+ self .add_to_output_queue_sync (
166
+ chunk .copy ()
167
+ ) # To send text, not just audio
168
+ # ^^^^^^^ MUST be a copy, otherwise the first chunk will get modified by OI >>while<< it's in the queue. Insane
169
169
if content :
170
170
# self.beeper.stop()
171
171
@@ -216,8 +216,7 @@ async def output(self):
216
216
217
217
218
218
def server (interpreter ):
219
- interpreter .llm .model = "gpt-4"
220
- interpreter = AsyncInterpreter (interpreter )
219
+ async_interpreter = AsyncInterpreter (interpreter )
221
220
222
221
app = FastAPI ()
223
222
app .add_middleware (
@@ -228,18 +227,12 @@ def server(interpreter):
228
227
allow_headers = ["*" ], # Allow all headers
229
228
)
230
229
231
- @app .post ("/load" )
232
- async def load (messages : List [Dict [str , Any ]], settings : Settings ):
233
- # Load messages
234
- interpreter .interpreter .messages = messages
235
- print ("🪼🪼🪼🪼🪼🪼 Messages loaded: " , interpreter .interpreter .messages )
236
-
237
- # Load Settings
238
- interpreter .interpreter .llm .model = settings .model
239
- interpreter .interpreter .llm .custom_instructions = settings .custom_instructions
240
- interpreter .interpreter .auto_run = settings .auto_run
241
-
242
- interpreter .interpreter .llm .api_key = "<openai_key>"
230
+ @app .post ("/settings" )
231
+ async def settings (payload : Dict [str , Any ]):
232
+ for key , value in payload .items ():
233
+ print ("Updating interpreter settings with the following:" )
234
+ print (key , value )
235
+ setattr (async_interpreter .interpreter , key , value )
243
236
244
237
return {"status" : "success" }
245
238
@@ -253,13 +246,16 @@ async def receive_input():
253
246
data = await websocket .receive ()
254
247
print (data )
255
248
if isinstance (data , bytes ):
256
- await interpreter .input (data )
257
- else :
258
- await interpreter .input (data ["text" ])
249
+ await async_interpreter .input (data )
250
+ elif "text" in data :
251
+ await async_interpreter .input (data ["text" ])
252
+ elif data == {"type" : "websocket.disconnect" , "code" : 1000 }:
253
+ print ("Websocket disconnected with code 1000." )
254
+ break
259
255
260
256
async def send_output ():
261
257
while True :
262
- output = await interpreter .output ()
258
+ output = await async_interpreter .output ()
263
259
if isinstance (output , bytes ):
264
260
# await websocket.send_bytes(output)
265
261
# we dont send out bytes rn, no TTS
@@ -306,4 +302,6 @@ async def rename_chat(body_content: Rename, x_api_key: str = Header(None)):
306
302
traceback .print_exc ()
307
303
return {"error" : str (e )}
308
304
309
- uvicorn .run (app , host = "0.0.0.0" , port = 8000 )
305
+ config = Config (app , host = "0.0.0.0" , port = 8000 )
306
+ interpreter .uvicorn_server = Server (config )
307
+ interpreter .uvicorn_server .run ()
0 commit comments