CoolFace
Apppublic

EinsteinCoder/llm-open-connector-together

sourceHugging Faceotherupdated 2y agoView on Hugging Face
0likes
app.py116 linesDownload Raw Back to root
1import os2import requests3from flask import Flask, request, jsonify, send_from_directory, Request4from flask_cors import CORS5from dotenv import load_dotenv6from werkzeug.exceptions import BadRequest7 8load_dotenv()9 10 11class 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 18class CustomFlask(Flask):19    request_class = ForceJSONRequest20 21 22app = CustomFlask(__name__)23CORS(app)  # This will enable CORS for all routes24 25GROQ_API_KEY = os.getenv("GROQ_API_KEY")26GROQ_API_URL = "https://api.together.xyz/v1/chat/completions"27APP_API_KEY = os.getenv("APP_API_KEY")  # Set default if not in env28#MODEL = "deepseek-ai/DeepSeek-R1"29MODEL = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free"30 31 32def check_api_key():33    api_key = request.headers.get("api-key") or request.headers.get("Api-Key")34    if api_key != APP_API_KEY:35        return jsonify({"error": "Invalid or missing API key"}), 40136    return None37 38 39@app.route("/")40def home():41    return send_from_directory(".", "index.html")42    #return """Flask Server running with GROQ API"""43 44 45@app.route("/chat/completions", methods=["POST"])46def create_chat_completion():47    error_response = check_api_key()48    if error_response:49        return error_response50 51    data = request.get_json(force=True)52 53    groq_payload = {54        "messages": data["messages"],55        "model": MODEL,56        "temperature": data.get("temperature", 0.1),57        "n": data.get("n", 1),58    }59 60    try:61        response = requests.post(62            GROQ_API_URL,63            headers={"Authorization": f"Bearer {GROQ_API_KEY}"},64            json=groq_payload,65        )66        response.raise_for_status()67    except requests.RequestException as e:68        error_message = f"Error calling Groq API: {str(e)}"69        return jsonify({"error": {"message": error_message, "type": "api_error"}}), 50070 71    try:72        groq_response = response.json()73    except ValueError:74        return (75            jsonify(76                {77                    "error": {78                        "message": "Invalid JSON response from Groq API",79                        "type": "api_error",80                    }81                }82            ),83            500,84        )85 86    # Remove logprobs from choices87    choices = groq_response["choices"]88    for choice in choices:89        choice.pop("logprobs", None)90 91    # Simplify the usage information92    simplified_usage = {93        "completion_tokens": groq_response["usage"]["completion_tokens"],94        "prompt_tokens": groq_response["usage"]["prompt_tokens"],95        "total_tokens": groq_response["usage"]["total_tokens"],96    }97 98    return (99        jsonify(100            {101                "id": groq_response["id"],102                "object": "chat.completion",103                "created": groq_response["created"],104                "model": MODEL,105                "choices": choices,106                "usage": simplified_usage,107            }108        ),109        200,110    )111 112 113if __name__ == "__main__":114    #app.run(debug=False)115    app.run(debug=True)116    #uvicorn.run(app,host='0.0.0.0', port=5075)