Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,20 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 4 |
from openai import OpenAI
|
|
|
|
| 5 |
|
| 6 |
client = OpenAI(
|
| 7 |
base_url="https://openrouter.ai/api/v1",
|
| 8 |
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 9 |
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
self._set_headers()
|
| 22 |
-
|
| 23 |
-
def do_GET(self):
|
| 24 |
-
if self.path == "/":
|
| 25 |
-
self._set_headers()
|
| 26 |
-
self.wfile.write(json.dumps({"status": "ok"}).encode())
|
| 27 |
-
else:
|
| 28 |
-
self.send_error(404)
|
| 29 |
-
|
| 30 |
-
def do_POST(self):
|
| 31 |
-
if self.path != "/chat":
|
| 32 |
-
self.send_error(404)
|
| 33 |
-
return
|
| 34 |
-
|
| 35 |
-
# Read body
|
| 36 |
-
content_length = int(self.headers["Content-Length"])
|
| 37 |
-
body = self.rfile.read(content_length)
|
| 38 |
-
data = json.loads(body.decode())
|
| 39 |
-
|
| 40 |
-
user_message = data.get("message", "")
|
| 41 |
-
|
| 42 |
-
# OpenRouter call
|
| 43 |
-
completion = client.chat.completions.create(
|
| 44 |
-
model="kwaipilot/kat-coder-pro:free",
|
| 45 |
-
messages=[
|
| 46 |
-
{"role": "user", "content": user_message}
|
| 47 |
-
]
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
reply = completion.choices[0].message.content
|
| 51 |
-
|
| 52 |
-
# Return response
|
| 53 |
-
self._set_headers()
|
| 54 |
-
self.wfile.write(json.dumps({"reply": reply}).encode())
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def run():
|
| 58 |
-
port = int(os.getenv("PORT", 7860))
|
| 59 |
-
server = HTTPServer(("0.0.0.0", port), Handler)
|
| 60 |
-
print(f"Server running on port {port}...")
|
| 61 |
-
server.serve_forever()
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
run()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
+
import os
|
| 3 |
|
| 4 |
client = OpenAI(
|
| 5 |
base_url="https://openrouter.ai/api/v1",
|
| 6 |
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 7 |
)
|
| 8 |
|
| 9 |
+
resp = client.chat.completions.create(
|
| 10 |
+
model="kwaipilot/kat-coder-pro:free",
|
| 11 |
+
messages=[
|
| 12 |
+
{"role": "user", "content": "What is the meaning of life?"}
|
| 13 |
+
],
|
| 14 |
+
extra_headers={
|
| 15 |
+
"HTTP-Referer": "https://example.com",
|
| 16 |
+
"X-Title": "MyApp"
|
| 17 |
+
}
|
| 18 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
print(resp.choices[0].message["content"])
|
|
|