Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from flask import Flask, request, jsonify, send_from_directory, Request
|
| 4 |
+
from flask_cors import CORS
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from werkzeug.exceptions import BadRequest
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ForceJSONRequest(Request):
|
| 12 |
+
def on_json_loading_failed(self, e):
|
| 13 |
+
if e is None:
|
| 14 |
+
return {}
|
| 15 |
+
return super().on_json_loading_failed(e)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class CustomFlask(Flask):
|
| 19 |
+
request_class = ForceJSONRequest
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
app = CustomFlask(__name__)
|
| 23 |
+
CORS(app) # This will enable CORS for all routes
|
| 24 |
+
|
| 25 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 26 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 27 |
+
APP_API_KEY = os.getenv("APP_API_KEY", "your-api-key-here") # Set default if not in env
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def check_api_key():
|
| 31 |
+
api_key = request.headers.get("api-key") or request.headers.get("Api-Key")
|
| 32 |
+
if api_key != APP_API_KEY:
|
| 33 |
+
return jsonify({"error": "Invalid or missing API key"}), 401
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.route("/")
|
| 38 |
+
def home():
|
| 39 |
+
return send_from_directory(".", "index.html")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.route("/chat/completions", methods=["POST"])
|
| 43 |
+
def create_chat_completion():
|
| 44 |
+
error_response = check_api_key()
|
| 45 |
+
if error_response:
|
| 46 |
+
return error_response
|
| 47 |
+
|
| 48 |
+
data = request.get_json(force=True)
|
| 49 |
+
|
| 50 |
+
groq_payload = {
|
| 51 |
+
"messages": data["messages"],
|
| 52 |
+
"model": "llama3-8b-8192",
|
| 53 |
+
"max_tokens": data.get("max_tokens", 500),
|
| 54 |
+
"temperature": data.get("temperature", 1.0),
|
| 55 |
+
"n": data.get("n", 1),
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
response = requests.post(
|
| 60 |
+
GROQ_API_URL,
|
| 61 |
+
headers={"Authorization": f"Bearer {GROQ_API_KEY}"},
|
| 62 |
+
json=groq_payload,
|
| 63 |
+
)
|
| 64 |
+
response.raise_for_status()
|
| 65 |
+
except requests.RequestException as e:
|
| 66 |
+
error_message = f"Error calling Groq API: {str(e)}"
|
| 67 |
+
return jsonify({"error": {"message": error_message, "type": "api_error"}}), 500
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
groq_response = response.json()
|
| 71 |
+
except ValueError:
|
| 72 |
+
return (
|
| 73 |
+
jsonify(
|
| 74 |
+
{
|
| 75 |
+
"error": {
|
| 76 |
+
"message": "Invalid JSON response from Groq API",
|
| 77 |
+
"type": "api_error",
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
),
|
| 81 |
+
500,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Remove logprobs from choices
|
| 85 |
+
choices = groq_response["choices"]
|
| 86 |
+
for choice in choices:
|
| 87 |
+
choice.pop("logprobs", None)
|
| 88 |
+
|
| 89 |
+
# Simplify the usage information
|
| 90 |
+
simplified_usage = {
|
| 91 |
+
"completion_tokens": groq_response["usage"]["completion_tokens"],
|
| 92 |
+
"prompt_tokens": groq_response["usage"]["prompt_tokens"],
|
| 93 |
+
"total_tokens": groq_response["usage"]["total_tokens"],
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
return (
|
| 97 |
+
jsonify(
|
| 98 |
+
{
|
| 99 |
+
"id": groq_response["id"],
|
| 100 |
+
"object": "chat.completion",
|
| 101 |
+
"created": groq_response["created"],
|
| 102 |
+
"model": "llama3-8b-8192",
|
| 103 |
+
"choices": choices,
|
| 104 |
+
"usage": simplified_usage,
|
| 105 |
+
}
|
| 106 |
+
),
|
| 107 |
+
200,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
app.run(debug=True)
|