Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
84 lines (71 loc) · 2.94 KB
/
Copy pathserver.py
File metadata and controls
84 lines (71 loc) · 2.94 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
"""
Локальный сервер для работы с Ollama (qwen3:4b).
Запуск: python3 server.py
Открой: http://localhost:8080
"""
from http.server import HTTPServer, SimpleHTTPRequestHandler
import json
import urllib.request
OLLAMA_URL = "http://localhost:11434/api/chat"
MODEL = "llama3.2:3b"
class RequestHandler(SimpleHTTPRequestHandler):
def do_POST(self):
if self.path == "/api/chat":
content_length = int(self.headers["Content-Length"])
body = self.rfile.read(content_length)
data = json.loads(body)
messages = data.get("messages", [])
model = data.get("model", MODEL)
ollama_payload = json.dumps({
"model": model,
"messages": messages,
"stream": False,
"think": False,
"options": {
"num_predict": 1500,
"temperature": 0.3
}
}).encode("utf-8")
try:
req = urllib.request.Request(
OLLAMA_URL,
data=ollama_payload,
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, timeout=120) as resp:
result = json.loads(resp.read().decode("utf-8"))
content = result.get("message", {}).get("content", "")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps({"content": content}).encode("utf-8"))
except Exception as e:
self.send_response(500)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
self.wfile.write(json.dumps({"error": str(e)}).encode("utf-8"))
else:
self.send_response(404)
self.end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "POST, OPTIONS")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def log_message(self, format, *args):
print(f"[{self.log_date_time_string()}] {format % args}")
if __name__ == "__main__":
port = 8080
server = HTTPServer(("localhost", port), RequestHandler)
print(f"Сервер запущен: http://localhost:{port}")
print(f"Ollama URL: {OLLAMA_URL}")
print(f"Модель: {MODEL}")
print("Нажми Ctrl+C для остановки")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nСервер остановлен")
server.server_close()