Update app.py
Browse files102 unmodified lines10310410510610710810911011111211310 unmodified lines1241251261271281293 unmodified lines13313413513613713813914014124 unmodified lines16616716816917017143 unmodified lines2152162172182192202212222232242252262278 unmodified lines236237238239102 unmodified linesdata = request.get_json()if not data:return jsonify({"error": {"message": "Invalid JSON", "type": "invalid_request"}}), 400messages = data.get("messages", [])model = data.get("model", "claude-sonnet-4-5-20250929")stream = data.get("stream", False)if model not in SUPPORTED_MODELS:return jsonify({"error": {"message": f"Model '{model}' not supported", "type": "model_not_found"}}), 400client_ip = get_real_ip()user_agent = get_user_agent()prompt = messages[-1]["content"] if messages else "问"10 unmodified linestext = cache_responses[i][0]["content"]cache_hit = Truebreakif text is None:try:dog_resp = requests.get(DOG_API_TEMPLATE.format(msg=quote(prompt)), timeout=5)3 unmodified linestext = f"{prompt}的存在,是一种不可避免的趋势。我认为,{prompt}对我们来说意义重大。了解清楚{prompt}到底是一种怎么样的存在,是解决一切问题的关键。"except:text = f"{prompt}的本质,不能不说非常重大。我们必须认真思考{prompt}这个问题。"response_data = [{"role": "assistant", "content": text}]cache_questions.append(prompt)cache_responses.append(response_data)response_data = [{"role": "assistant", "content": text}]24 unmodified linesdef generate_stream():chunk_id = CHUNK_ID_PREFIX + str(int(time.time()))chunk_size = 5for i in range(0, len(text), chunk_size):chunk_text = text[i:i+chunk_size]chunk = {43 unmodified lines'Cache-Control': 'no-cache','X-Accel-Buffering': 'no'})except Exception as e:logger.error(f"ERROR: {str(e)}")return jsonify({"id": CHUNK_ID_PREFIX + str(int(time.time())),"object": "chat.completion","created": int(time.time()),"model": model,"choices": [{"index": 0,"message": {"role": "assistant", "content": "这个问题,值得我们深入思考。"},8 unmodified lines@app.route("/", methods=["GET"])def index():return "<h1>AI Service Running</h1>", 200102 unmodified lines10310410510610710810911011111211311411511610 unmodified lines1271281291301311321333 unmodified lines13713813914014114214314414524 unmodified lines17017117217317417517643 unmodified lines2202212222232242252262272282292302312322338 unmodified lines242243244245246247248102 unmodified linesdata = request.get_json()if not data:return jsonify({"error": {"message": "Invalid JSON", "type": "invalid_request"}}), 400messages = data.get("messages", [])model = data.get("model", "claude-sonnet-4-5-20250929")stream = data.get("stream", False)if model not in SUPPORTED_MODELS:return jsonify({"error": {"message": f"Model '{model}' not supported", "type": "model_not_found"}}), 400client_ip = get_real_ip()user_agent = get_user_agent()prompt = messages[-1]["content"] if messages else "问"10 unmodified linestext = cache_responses[i][0]["content"]cache_hit = Truebreakif text is None:try:dog_resp = requests.get(DOG_API_TEMPLATE.format(msg=quote(prompt)), timeout=5)3 unmodified linestext = f"{prompt}的存在,是一种不可避免的趋势。我认为,{prompt}对我们来说意义重大。了解清楚{prompt}到底是一种怎么样的存在,是解决一切问题的关键。"except:text = f"{prompt}的本质,不能不说非常重大。我们必须认真思考{prompt}这个问题。"cache_questions.append(prompt)cache_responses.append([{"role": "assistant", "content": text}])response_data = [{"role": "assistant", "content": text}]24 unmodified linesdef generate_stream():chunk_id = CHUNK_ID_PREFIX + str(int(time.time()))chunk_size = 5for i in range(0, len(text), chunk_size):chunk_text = text[i:i+chunk_size]chunk = {43 unmodified lines'Cache-Control': 'no-cache','X-Accel-Buffering': 'no'})except Exception as e:logger.error(f"ERROR: {str(e)}")return jsonify({"id": CHUNK_ID_PREFIX + str(int(time.time())),"object": "chat.completion","created": int(time.time()),"model": model if 'model' in locals() else "claude-sonnet-4-5-20250929","choices": [{"index": 0,"message": {"role": "assistant", "content": "这个问题,值得我们深入思考。"},8 unmodified lines@app.route("/", methods=["GET"])def index():return "<h1>AI Service Running</h1>", 200if __name__ == "__main__":app.run(host="0.0.0.0", port=8080)
app.py+13-4