File size: 5,641 Bytes
ee1b98b
a3e88ad
 
ee1b98b
 
 
 
2d5c9d2
ee1b98b
 
 
 
 
 
 
 
 
 
 
095c939
 
 
 
 
96070f4
095c939
be575a2
 
 
fbbff2c
961a741
be575a2
e5b34a0
ee1b98b
 
 
 
 
2d5c9d2
 
 
ee1b98b
 
2d5c9d2
 
a3e88ad
2d5c9d2
ee1b98b
a3e88ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d5c9d2
a3e88ad
 
 
 
 
 
 
 
2d5c9d2
a3e88ad
 
 
 
 
 
 
 
2d5c9d2
a3e88ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d5c9d2
ee1b98b
2d5c9d2
 
 
 
 
 
 
 
ee1b98b
 
2d5c9d2
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import json
from flask import Flask, request, jsonify, Response, stream_with_context
from huggingface_hub import InferenceClient

app = Flask(__name__)
HF_TOKEN = os.environ.get("HF_TOKEN", "")
DEFAULT_MODEL = os.environ.get("DEFAULT_MODEL", "Qwen/Qwen2.5-72B-Instruct")
client = InferenceClient(token=HF_TOKEN)

@app.route("/", methods=["GET"])
def index():
    return jsonify({"status": "ok", "message": "InfinityLLM API is running"})

@app.route("/v1/models", methods=["GET"])
def models():
    return jsonify({
        "object": "list",
        "data": [
            {"id": "Qwen/Qwen2.5-72B-Instruct",               "object": "model"},
            {"id": "Qwen/Qwen2.5-7B-Instruct",                "object": "model"},
            {"id": "Qwen/Qwen2.5-Coder-32B-Instruct",         "object": "model"},
            {"id": "meta-llama/Llama-3.3-70B-Instruct",       "object": "model"},
            {"id": "meta-llama/Llama-3.1-8B-Instruct",        "object": "model"},
            {"id": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "object": "model"},
            {"id": "deepseek-ai/DeepSeek-R1-Distill-Llama-70B","object": "model"},
            {"id": "moonshotai/Kimi-K2.6",                     "object": "model"},  
            {"id": "MiniMaxAI/MiniMax-M2.7",                   "object": "model"},  
            {"id": "Qwen/Qwen3-Coder-30B-A3B-Instruct",        "object": "model"},  
            {"id": "deepseek-ai/DeepSeek-V4-Pro",        "object": "model"},  
            {"id": "zai-org/GLM-5.1",        "object": "model"},  

        ]
    })

@app.route("/v1/chat/completions", methods=["POST"])
def chat():
    data = request.json
    if not data:
        return jsonify({"error": "No JSON body"}), 400

    messages = data.get("messages", [])
    model = data.get("model", DEFAULT_MODEL)
    max_tokens = int(data.get("max_tokens", 2048))
    temperature = float(data.get("temperature", 0.7))
    stream = data.get("stream", False)

    try:
        if stream:
            def generate():
                full_content = ""
                try:
                    for chunk in client.chat_completion(
                        model=model,
                        messages=messages,
                        max_tokens=max_tokens,
                        temperature=temperature,
                        stream=True,
                    ):
                        delta_content = ""
                        if chunk.choices and chunk.choices[0].delta:
                            delta_content = chunk.choices[0].delta.content or ""
                        full_content += delta_content
                        chunk_data = {
                            "id": "chatcmpl-hf",
                            "object": "chat.completion.chunk",
                            "model": model,
                            "choices": [{
                                "index": 0,
                                "delta": {"role": "assistant", "content": delta_content},
                                "finish_reason": None
                            }]
                        }
                        yield f"data: {json.dumps(chunk_data)}\n\n"
                    
                    final = {
                        "id": "chatcmpl-hf",
                        "object": "chat.completion.chunk",
                        "model": model,
                        "choices": [{
                            "index": 0,
                            "delta": {},
                            "finish_reason": "stop"
                        }]
                    }
                    yield f"data: {json.dumps(final)}\n\n"
                    yield "data: [DONE]\n\n"
                except Exception as e:
                    yield f"data: {json.dumps({'error': str(e)})}\n\n"

            return Response(
                stream_with_context(generate()),
                mimetype="text/event-stream",
                headers={
                    "Cache-Control": "no-cache",
                    "X-Accel-Buffering": "no"
                }
            )

        else:
            response = client.chat_completion(
                model=model,
                messages=messages,
                max_tokens=max_tokens,
                temperature=temperature,
                stream=False,
            )

            content = ""
            if response.choices and len(response.choices) > 0:
                choice = response.choices[0]
                if hasattr(choice, "message") and choice.message:
                    content = choice.message.content or ""

            if not content:
                return jsonify({"error": "Empty response from model"}), 500

            return jsonify({
                "id": "chatcmpl-hf",
                "object": "chat.completion",
                "model": model,
                "choices": [{
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": content
                    },
                    "finish_reason": "stop"
                }],
                "usage": {
                    "prompt_tokens": 0,
                    "completion_tokens": 0,
                    "total_tokens": 0
                }
            })

    except Exception as e:
        return jsonify({
            "error": str(e),
            "choices": [{
                "index": 0,
                "message": {"role": "assistant", "content": f"Error: {str(e)}"},
                "finish_reason": "stop"
            }]
        }), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=False)