-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathserver.py
More file actions
349 lines (317 loc) · 12.1 KB
/
Copy pathserver.py
File metadata and controls
349 lines (317 loc) · 12.1 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
from flask import Flask, request, jsonify, Response
import subprocess
from threading import Event
import re
import json
import time
VERSION = "0.4"
# Global event flag
message_ready = Event()
# Default values
default_prompt = "hi, what is 1 + 1"
default_model = "GPT-5"
# Store prompt and model values
stored_prompt = default_prompt
stored_model = default_model
stored_message = ""
models = [
"GPT-5",
"GPT-5 mini",
"GPT-5 nano",
"apple_local",
"apple_cloud"
]
# Flask app
app = Flask(__name__)
def model_handler(model_name):
"""
Function to handle model names and return a standardized version.
"""
# Check if the model name is in the list of supported models
# If the model name contains a snapshot, remove it
model_name = re.sub(r'-\d{4}-\d{2}-\d{2}$', '', model_name)
# strip :
model_name = model_name.split(":")[0]
# Try to match with one of our standard model names
model_name = next((m for m in models if model_name.lower() == m.lower()), model_name)
# If the model name contains 'gpt' (case-insensitive), route it to 'GPT-5'
if "gpt" in model_name.lower():
print(f"{model_name} detected as GPT model, routing to GPT-5")
model_name = "GPT-5"
assert model_name in models, f"Model {model_name} is not supported.\n"
return model_name
# OpenAI API
@app.route('/v1/chat/completions', methods=['POST'])
def prompt_model():
global stored_prompt, stored_model, stored_message
message_ready.clear()
if request.method == 'POST':
data = request.get_json()
# Extract the user's prompt from the OpenAI‑style messages list
messages = data.get('messages', [])
# Build conversation history as prompt for the model
if isinstance(messages, list) and messages:
formatted_history = []
for m in messages:
role = m.get('role')
content = m.get('content', "")
if role == "system":
formatted_history.append(f"System: {content}")
elif role == "user":
formatted_history.append(f"User: {content}")
elif role == "assistant":
formatted_history.append(f"Assistant: {content}")
else:
formatted_history.append(f"{role.capitalize()}: {content}")
stored_prompt = "\n".join(formatted_history)
else:
stored_prompt = default_prompt
# Handling model names
stored_model = data.get('model', default_model)
stored_model = model_handler(stored_model)
stored_message = "" # will be filled later by /internal POST
subprocess.Popen(["shortcuts", "run", "MackingJAI"])
message_ready.wait()
stream = data.get('stream', False)
if not stream:
response_payload = {
"id": "chatcmpl-local-001",
"object": "chat.completion",
"created": 0,
"model": stored_model,
"prompt": stored_prompt,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": stored_message
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
},
# Generic signal that processing is done
"finish_signal": True
}
response = jsonify(response_payload)
# Add a generic header to signal completion
response.headers['X-Chat-Complete'] = 'true'
return response
else:
# Streaming: yield chunks of the message
def generate_stream():
import uuid
now = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
chunk_size = 8 # characters per chunk
content = stored_message or ""
for i in range(0, len(content), chunk_size):
chunk = content[i:i+chunk_size]
obj = {
"id": "chatcmpl-local-001",
"object": "chat.completion.chunk",
"created": 0,
"model": stored_model,
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": chunk
},
"finish_reason": None
}
]
}
yield f"data: {json.dumps(obj)}\n\n"
time.sleep(0.001)
# Final chunk with finish_reason and usage
obj = {
"id": "chatcmpl-local-001",
"object": "chat.completion.chunk",
"created": 0,
"model": stored_model,
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": ""
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}
yield f"data: {json.dumps(obj)}\n\n"
return Response(generate_stream(), mimetype='application/x-ndjson')
@app.route('/v1/models', methods=['GET'])
def list_models():
"""
Return a list of supported models in the OpenAI API format.
"""
return jsonify({
"data": [
{"id": m, "object": "model"} for m in models
]
})
# Ollama API
@app.route('/api/chat', methods=['POST'])
def prompt_model_ollama():
global stored_prompt, stored_model, stored_message
message_ready.clear()
if request.method == 'POST':
try:
data = request.get_json(force=True)
except Exception:
# Fallback: try to parse raw data as JSON
data = None
if request.data:
try:
data = json.loads(request.data.decode('utf-8'))
except Exception:
data = {}
if not data:
data = {}
# Extract the user's prompt from the OpenAI‑style messages list
messages = data.get('messages', [])
# Build conversation history as prompt for the model
if isinstance(messages, list) and messages:
formatted_history = []
for m in messages:
role = m.get('role')
content = m.get('content', "")
if role == "system":
formatted_history.append(f"System: {content}")
elif role == "user":
formatted_history.append(f"User: {content}")
elif role == "assistant":
formatted_history.append(f"Assistant: {content}")
else:
formatted_history.append(f"{role.capitalize()}: {content}")
stored_prompt = "\n".join(formatted_history)
else:
stored_prompt = default_prompt
# Handling model names
stored_model = data.get('model', default_model)
stored_model = model_handler(stored_model)
stored_message = "" # will be filled later by /internal POST
subprocess.Popen(["shortcuts", "run", "MackingJAI"])
message_ready.wait()
stream = data.get('stream', True)
if not stream:
# Non-streaming: return the whole message at once
response_payload = {
"model": stored_model,
"created_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"message": {
"role": "assistant",
"content": stored_message
},
"done_reason": "stop",
"done": True,
"total_duration": 0,
"load_duration": 0,
"prompt_eval_count": 0,
"prompt_eval_duration": 0,
"eval_count": 0,
"eval_duration": 0
}
return jsonify(response_payload)
else:
# Streaming: yield chunks of the message
def generate_stream():
import uuid
now = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
chunk_size = 8 # characters per chunk
content = stored_message or ""
for i in range(0, len(content), chunk_size):
chunk = content[i:i+chunk_size]
obj = {
"model": stored_model,
"created_at": now,
"message": {
"role": "assistant",
"content": chunk
},
"done": False
}
yield json.dumps(obj) + '\n'
time.sleep(0.001)
# Final chunk with done: true
obj = {
"model": stored_model,
"created_at": now,
"message": {
"role": "assistant",
"content": ""
},
"done_reason": "stop",
"done": True,
"total_duration": 0,
"load_duration": 0,
"prompt_eval_count": 0,
"prompt_eval_duration": 0,
"eval_count": 0,
"eval_duration": 0
}
yield json.dumps(obj) + '\n'
return Response(generate_stream(), mimetype='application/x-ndjson')
@app.route('/api/tags', methods=['GET'])
@app.route('/api/ps', methods=['GET'])
def list_models_ollama():
"""
Return a list of supported models in the OpenAI API format.
"""
return jsonify({
"models": [
{"name":m, "model": m + ":latest" , "size": 1, "digest": "", "modified_at": "2025-01-01T11:15:24.832444301+03:00"} for m in models
]
})
@app.route('/api/show', methods=['POST'])
def show_model():
data = request.get_json()
model_name = data.get('model', default_model)
model_name = model_handler(model_name)
with open("api_show.json") as f:
json_format = json.load(f)
# Recursively replace all 'modelname_placeholder' with model_name
def replace_placeholders(obj):
if isinstance(obj, dict):
return {k: replace_placeholders(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [replace_placeholders(i) for i in obj]
elif obj == "modelname_placeholder":
return model_name
else:
return obj
json_format = replace_placeholders(json_format)
return jsonify(json_format)
@app.route('/api/version', methods=['GET'])
def version():
return jsonify({"version": VERSION})
@app.route('/internal', methods=['GET', 'POST'])
def internal():
global stored_prompt, stored_model, stored_message
if request.method == 'GET':
return jsonify({
"prompt": stored_prompt,
"model": stored_model
})
else: # POST
data = request.get_json()
stored_message = data.get('message', "")
message_ready.set()
return jsonify({"status": "ok"})
def run_server():
app.run(host="0.0.0.0", debug=False, threaded=True, port=11435, use_reloader=False)
if __name__ == '__main__':
run_server()