这段代码运行后控制台没有关于程序的输出，但是是可以调用的：from flask import Flask, request, Response, abort
import requests
from flask_caching import Cache
import json
from flask_cors import CORS
import logging
from insert_memory.prompt import PromptAssembler
from memory.memorymanager import MemoryManager
import threading
import time

app = Flask(__name__)
CORS(app)

# 配置缓存
app.config['CACHE_TYPE'] = 'simple'
cache = Cache(app)

ALLOWED_PATHS = [
    '/v1/chat/completions',
    '/v1/completions',
    '/chat/completions',
    '/v1/models'
]

TARGET_URL = "http://47.115.211.116:5000"

# 设置日志
logging.basicConfig(filename='error.log', level=logging.ERROR)

# 创建全局 MemoryManager 实例
global_memory_manager = MemoryManager()

def add_memory_async(user_id, messages, memory_manager):
    result = memory_manager.add_memory(messages, user_id)
    print("添加记忆结果：", result)

def make_request_with_retry(method, url, headers=None, data=None, params=None, stream=False, retries=3, backoff_factor=0.3):
    """
    通用的重试请求函数.
    """
    for i in range(retries):
        try:
            if method == 'POST':
                resp = requests.post(url, headers=headers, data=data, stream=stream)
            elif method == 'GET':
                resp = requests.get(url, headers=headers, params=params, stream=stream)
            
            if resp.status_code != 200:
                continue
            
            return resp
        except requests.exceptions.RequestException as e:
            logging.error(f"Request attempt {i + 1} failed: {e}")
            if i < retries - 1:
                time.sleep(backoff_factor * (2 ** i))
            else:
                raise

@app.before_request
def validate_request_path():
    if request.path not in ALLOWED_PATHS:
        abort(404)

@app.route('/v1/models', methods=['GET'])
def get_models():
    target_url = f"{TARGET_URL}/v1/models"
    headers = {key: value for (key, value) in request.headers if key.lower() != 'host'}

    try:
        # 使用通用的重试函数发起请求
        resp = make_request_with_retry('GET', target_url, headers=headers)
        
        # 过滤响应头
        response_headers = [(name, value) for (name, value) in resp.headers.items()
                            if name.lower() not in ['content-encoding', 'transfer-encoding', 'content-length']]

        # 返回响应
        return Response(resp.content, status=resp.status_code, headers=response_headers)
    
    except requests.exceptions.RequestException as e:
        logging.error(f"RequestException: {e}")
        return f"Error: {e}", 500

@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
@app.route('/<path:path>', methods=['GET', 'POST'])
def proxy(path):
    global global_memory_manager
    prompt_assembler = PromptAssembler(global_memory_manager)

    target_url = f"{TARGET_URL}/{path}"

    # 组装prompt
    if request.method == 'POST' and request.is_json:
        data = request.get_json()
        messages = data.get('messages', [])
        
        # 获取第一条role是system的记录
        system_prompt = next((msg['content'] for msg in messages if msg['role'] == 'system'), None)

        # 防止系统消息为空
        if not system_prompt:
            system_prompt = ""

        # 获取最后一条role是user的输入
        user_input = next((msg['content'] for msg in reversed(messages) if msg['role'] == 'user'), None)

        if user_input:
            # 暂时使用默认值
            user_id = "daizihan"

            # 添加记忆至数据库
            threading.Thread(target=add_memory_async, args=(user_id, messages, global_memory_manager)).start()
            
            # 使用PromptAssembler插入记忆
            assembled_prompt = prompt_assembler.assemble_prompt(system_prompt, user_input, user_id, messages)
            
            # 替换或插入新的系统消息
            if messages and messages[0]['role'] == 'system':
                messages[0]['content'] = assembled_prompt
            else:
                messages.insert(0, {'role': 'system', 'content': assembled_prompt})
            
            # 更新请求数据
            data['messages'] = messages

            print(messages)

    headers = {key: value for (key, value) in request.headers if key != 'Host'}

    try:
        use_stream = request.args.get('stream', '').lower() == 'true' or (request.is_json and request.json.get('stream', False))

        json_data = None
        if request.method == 'POST' and request.is_json:
            json_data = json.dumps(data)
        
        # 使用通用的重试函数发起请求
        if use_stream:
            resp = make_request_with_retry('POST' if request.method == 'POST' else 'GET', target_url, headers=headers, data=json_data, params=request.args, stream=True)
        else:
            resp = make_request_with_retry('POST' if request.method == 'POST' else 'GET', target_url, headers=headers, data=json_data, params=request.args, stream=False)

        response_headers = [(name, value) for (name, value) in resp.headers.items()
                            if name.lower() not in ['content-encoding', 'transfer-encoding', 'content-length']]

        if use_stream:
            def generate():
                for chunk in resp.iter_content(chunk_size=8192):
                    yield chunk
            return Response(generate(), status=resp.status_code, headers=response_headers)
        else:
            return Response(resp.content, status=resp.status_code, headers=response_headers)

    except requests.exceptions.RequestException as e:
        logging.error(f"RequestException: {e}")
        return f"Error: {e}", 500

def main():
    app.run(host='0.0.0.0', port=5004)

if __name__ == 'main':
    main()
控制台：* Serving Flask app 'zhuanfa.OpenaiServer'
 * Debug mode: off