-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_style_server.py
More file actions
353 lines (290 loc) · 11.6 KB
/
openai_style_server.py
File metadata and controls
353 lines (290 loc) · 11.6 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import asyncio
import json
import logging
import re
import time
import uuid
import webbrowser
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread
import websockets
# Silence noisy websocket logs.
logging.getLogger("websockets.server").setLevel(logging.CRITICAL)
class BridgeState:
def __init__(self):
self.websocket = None
self.websocket_lock = asyncio.Lock()
async def register_websocket(self, websocket):
if self.websocket is not None:
await self.websocket.close()
self.websocket = websocket
print("\n--- Browser extension connected ---")
async def clear_websocket(self, websocket):
if self.websocket is websocket:
self.websocket = None
print("\n[Extension disconnected. Waiting to reconnect...]")
async def ask_chatgpt(self, prompt):
async with self.websocket_lock:
if self.websocket is None:
raise RuntimeError("Browser extension is not connected")
await self.websocket.send(json.dumps({"type": "prompt", "text": prompt}))
response = await self.websocket.recv()
data = json.loads(response)
if data.get("type") == "response":
return data.get("text", "")
if data.get("type") == "error":
raise RuntimeError(data.get("text", "Unknown extension error"))
raise RuntimeError("Unexpected message received from extension")
state = BridgeState()
event_loop = None
def extract_user_prompt(messages):
user_messages = [m for m in messages if m.get("role") == "user"]
if not user_messages:
return ""
last = user_messages[-1].get("content", "")
if isinstance(last, list):
parts = []
for item in last:
if isinstance(item, dict) and item.get("type") == "text":
parts.append(item.get("text", ""))
return "\n".join(parts).strip()
return str(last).strip()
def flatten_content(content):
if isinstance(content, str):
return content.strip()
if isinstance(content, list):
parts = []
for item in content:
if isinstance(item, dict) and item.get("type") == "text":
parts.append(str(item.get("text", "")))
return "\n".join(parts).strip()
return str(content or "").strip()
def conversation_text(messages):
lines = []
for message in messages:
role = message.get("role", "unknown")
text = flatten_content(message.get("content", ""))
if role == "tool":
name = message.get("name", "tool")
lines.append(f"tool:{name}: {text}")
elif text:
lines.append(f"{role}: {text}")
return "\n".join(lines).strip()
def extract_json_object(text):
text = text.strip()
if not text:
return None
try:
return json.loads(text)
except Exception:
pass
fenced = re.search(r"```(?:json)?\s*(\{[\s\S]*?\})\s*```", text)
if fenced:
try:
return json.loads(fenced.group(1))
except Exception:
return None
return None
def openai_chat_response(model, text):
now = int(time.time())
return {
"id": f"chatcmpl-{uuid.uuid4().hex}",
"object": "chat.completion",
"created": now,
"model": model,
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": text},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
},
}
def openai_tool_call_response(model, tool_name, arguments):
now = int(time.time())
call_id = f"call_{uuid.uuid4().hex[:24]}"
return {
"id": f"chatcmpl-{uuid.uuid4().hex}",
"object": "chat.completion",
"created": now,
"model": model,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": call_id,
"type": "function",
"function": {
"name": tool_name,
"arguments": json.dumps(arguments),
},
}
],
},
"finish_reason": "tool_calls",
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
},
}
def json_response(handler, payload, status=200):
body = json.dumps(payload).encode("utf-8")
handler.send_response(status)
handler.send_header("Content-Type", "application/json")
handler.send_header("Content-Length", str(len(body)))
handler.send_header("Access-Control-Allow-Origin", "*")
handler.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
handler.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
handler.end_headers()
handler.wfile.write(body)
class OpenAICompatibleHandler(BaseHTTPRequestHandler):
server_version = "ChatGPTBridge/1.0"
def log_message(self, format_str, *args):
return
def do_OPTIONS(self):
self.send_response(204)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
self.end_headers()
def do_GET(self):
if self.path == "/health":
return json_response(
self,
{
"ok": True,
"extension_connected": state.websocket is not None,
"service": "chatgpt-browser-bridge",
},
)
if self.path == "/v1/models":
return json_response(
self,
{
"object": "list",
"data": [
{
"id": "chatgpt-browser-bridge",
"object": "model",
"owned_by": "local",
}
],
},
)
return json_response(self, {"error": "Not found"}, status=404)
def do_POST(self):
if self.path != "/v1/chat/completions":
return json_response(self, {"error": "Not found"}, status=404)
try:
length = int(self.headers.get("Content-Length", "0"))
payload = json.loads(self.rfile.read(length) or b"{}")
except Exception:
return json_response(self, {"error": "Invalid JSON payload"}, status=400)
if payload.get("stream"):
return json_response(
self,
{"error": "stream=true is not supported by this backend"},
status=400,
)
messages = payload.get("messages", [])
if not isinstance(messages, list) or not messages:
return json_response(self, {"error": "messages is required"}, status=400)
model = payload.get("model") or "chatgpt-browser-bridge"
tools = payload.get("tools") or []
prompt = extract_user_prompt(messages)
if not prompt:
return json_response(self, {"error": "No user text found in messages"}, status=400)
has_tool_result = any(m.get("role") == "tool" for m in messages)
if tools and not has_tool_result:
tool_specs = []
for tool in tools:
fn = (tool or {}).get("function", {})
if fn.get("name"):
tool_specs.append(
{
"name": fn.get("name"),
"description": fn.get("description", ""),
"parameters": fn.get("parameters", {}),
}
)
planner_prompt = (
"You are an API tool planner. Based on the conversation and available tools, decide "
"whether to call exactly one function now. Return JSON only.\n"
"If a tool call is needed, output: {\"tool_call\": {\"name\": \"...\", \"arguments\": {...}}}\n"
"If no tool call is needed, output: {\"final\": \"...\"}.\n\n"
f"TOOLS:\n{json.dumps(tool_specs)}\n\n"
f"CONVERSATION:\n{conversation_text(messages)}"
)
try:
future = asyncio.run_coroutine_threadsafe(state.ask_chatgpt(planner_prompt), event_loop)
plan_text = future.result(timeout=240)
plan = extract_json_object(plan_text) or {}
if isinstance(plan.get("tool_call"), dict):
tool_call = plan["tool_call"]
tool_name = str(tool_call.get("name", "")).strip()
tool_args = tool_call.get("arguments", {})
if isinstance(tool_args, str):
try:
tool_args = json.loads(tool_args)
except Exception:
tool_args = {}
if tool_name:
return json_response(
self,
openai_tool_call_response(model, tool_name, tool_args if isinstance(tool_args, dict) else {}),
)
if isinstance(plan.get("final"), str) and plan.get("final").strip():
return json_response(self, openai_chat_response(model, plan.get("final").strip()))
except Exception as exc:
return json_response(self, {"error": str(exc)}, status=503)
if has_tool_result:
answer_prompt = (
"You are a helpful assistant. Use the provided tool result(s) to answer the user.\n\n"
f"CONVERSATION:\n{conversation_text(messages)}"
)
else:
answer_prompt = prompt
try:
future = asyncio.run_coroutine_threadsafe(state.ask_chatgpt(answer_prompt), event_loop)
answer = future.result(timeout=240)
except Exception as exc:
return json_response(self, {"error": str(exc)}, status=503)
return json_response(self, openai_chat_response(model, answer))
async def websocket_handler(websocket):
await state.register_websocket(websocket)
try:
await websocket.wait_closed()
finally:
await state.clear_websocket(websocket)
def start_http_server(host="127.0.0.1", port=8000):
server = ThreadingHTTPServer((host, port), OpenAICompatibleHandler)
print(f"OpenAI-compatible API: http://{host}:{port}/v1")
print("No API key validation on this local backend.")
server.serve_forever()
async def main():
global event_loop
event_loop = asyncio.get_running_loop()
print("Starting WebSocket server on ws://localhost:8765 for the extension...")
print("Starting HTTP API on http://127.0.0.1:8000 ...")
print("Opening ChatGPT in your browser...")
webbrowser.open("https://chatgpt.com")
Thread(target=start_http_server, daemon=True).start()
async with websockets.serve(websocket_handler, "localhost", 8765):
await asyncio.Future()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nServer shut down.")