feat: verified high-speed G4F server with Gemini, Cohere, HuggingSpace live models
This commit is contained in:
+37
-14
@@ -3,14 +3,31 @@
|
||||
"""
|
||||
Local G4F API Server for Oracle 999
|
||||
Provides an OpenAI-compatible /v1/chat/completions endpoint on port 1337
|
||||
Routing specifically through verified live providers:
|
||||
- Gemini
|
||||
- CohereForAI_C4AI_Command
|
||||
- HuggingSpace
|
||||
- Yqcloud
|
||||
"""
|
||||
|
||||
import json
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
import g4f
|
||||
from g4f.client import Client
|
||||
from g4f.Provider import (
|
||||
Gemini,
|
||||
CohereForAI_C4AI_Command,
|
||||
HuggingSpace,
|
||||
Yqcloud,
|
||||
)
|
||||
|
||||
client = Client()
|
||||
PRIORITY_PROVIDERS = [
|
||||
('Gemini', Gemini),
|
||||
('CohereForAI', CohereForAI_C4AI_Command),
|
||||
('HuggingSpace', HuggingSpace),
|
||||
('Yqcloud', Yqcloud),
|
||||
]
|
||||
|
||||
class G4FHandler(BaseHTTPRequestHandler):
|
||||
def do_OPTIONS(self):
|
||||
@@ -25,7 +42,7 @@ class G4FHandler(BaseHTTPRequestHandler):
|
||||
self.send_header('Content-Type', 'application/json')
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps({'status': 'running', 'service': 'Oracle 999 G4F Server'}).encode('utf-8'))
|
||||
self.wfile.write(json.dumps({'status': 'running', 'service': 'Oracle 999 G4F Server', 'providers': ['Gemini', 'CohereForAI', 'HuggingSpace', 'Yqcloud']}).encode('utf-8'))
|
||||
|
||||
def do_POST(self):
|
||||
content_length = int(self.headers.get('Content-Length', 0))
|
||||
@@ -34,30 +51,38 @@ class G4FHandler(BaseHTTPRequestHandler):
|
||||
try:
|
||||
req_json = json.loads(post_data)
|
||||
messages = req_json.get('messages', [])
|
||||
model = req_json.get('model', 'gpt-4o-mini')
|
||||
|
||||
print(f"[G4F Server] Request received for model: {model}, messages: {len(messages)}")
|
||||
print(f"[G4F Server] Processing request ({len(messages)} messages)...")
|
||||
|
||||
# 1. Try with requested model
|
||||
response_text = None
|
||||
for p_name, prov in PRIORITY_PROVIDERS:
|
||||
try:
|
||||
chat_res = client.chat.completions.create(
|
||||
model=model,
|
||||
model=g4f.models.default,
|
||||
provider=prov,
|
||||
messages=messages,
|
||||
timeout=12,
|
||||
)
|
||||
response_text = chat_res.choices[0].message.content
|
||||
except Exception as e1:
|
||||
print(f"[G4F Server] Primary model failed ({e1}), falling back to default...")
|
||||
content = chat_res.choices[0].message.content
|
||||
if content and len(content.strip()) > 15:
|
||||
response_text = content.strip()
|
||||
print(f"[G4F Server] Generated successfully via provider: {p_name}!")
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"[G4F Server] Provider {p_name} failed ({e}), trying next...")
|
||||
|
||||
if not response_text or len(response_text.strip()) == 0:
|
||||
# Ultimate fallback to default client
|
||||
try:
|
||||
chat_res = client.chat.completions.create(
|
||||
model=g4f.models.default,
|
||||
messages=messages,
|
||||
)
|
||||
response_text = chat_res.choices[0].message.content
|
||||
except Exception as e2:
|
||||
print(f"[G4F Server] Default model failed ({e2})")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not response_text or len(response_text.strip()) == 0:
|
||||
if not response_text:
|
||||
response_text = "Оракул открывает знаки: путь разворачивается перед вами. Сосредоточьтесь на ключевом символе и следуйте зову сердца."
|
||||
|
||||
resp_payload = {
|
||||
@@ -78,7 +103,6 @@ class G4FHandler(BaseHTTPRequestHandler):
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(resp_payload, ensure_ascii=False).encode('utf-8'))
|
||||
print("[G4F Server] Successfully responded to client!")
|
||||
|
||||
except Exception as err:
|
||||
print(f"[G4F Server] Handler error: {err}")
|
||||
@@ -89,13 +113,12 @@ class G4FHandler(BaseHTTPRequestHandler):
|
||||
self.wfile.write(json.dumps({'error': str(err)}).encode('utf-8'))
|
||||
|
||||
def log_message(self, format, *args):
|
||||
# Clean server logs
|
||||
return
|
||||
|
||||
def run(port=1337):
|
||||
server_address = ('127.0.0.1', port)
|
||||
httpd = HTTPServer(server_address, G4FHandler)
|
||||
print(f"🔮 [Oracle 999] Local G4F Server is LIVE on http://127.0.0.1:{port}/v1/chat/completions")
|
||||
print(f"🔮 [Oracle 999] High-Speed G4F Server is LIVE on http://127.0.0.1:{port}/v1/chat/completions")
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
|
||||
Reference in New Issue
Block a user