Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

import requests import json from flask import Flask, request, Response import time…

  1. User

    import requests import json from flask import Flask, request, Response import time import configparser import uuid

    app = Flask(name)

    读取配置文件

    config = configparser.ConfigParser() config.read('config.ini')

    设置基本URL和头信息

    headers = { "Authorization": config['API']['authorization'], "Content-Type": "application/json" }

    全局变量用于保存 UUID

    saved_uuid = None

    @app.route('/chat', methods=['POST']) def chat(): global saved_uuid

    # 记录开始时间
    start_time = time.time()
    
    # 解析请求
    req_data = request.json
    sid = req_data.get('sid')
    dh_code = req_data.get('dh-code')
    dh_question = req_data.get('dh-question')
    dh_conversation_id = req_data.get('dh-conversation-id')
    dh_context = req_data.get('dh-context', "{}")
    print(dh_context)
    print(dh_conversation_id)
    
    # 检查 dh_context 是否为 None,如果是则生成一个新的 UUID
    if dh_context is None:
        saved_uuid = str(uuid.uuid4())
    dh_conversation_id = saved_uuid
    
    # 构建请求数据
    data = {
        "chatId": dh_conversation_id,
        "stream": True,
        "detail": False,
        "messages": [
            {
                "content": dh_question,
                "role": "user"
            }
        ]
    }
    
    # 发送流式POST请求到xyai.cloud
    url = config['API']['url']
    api_response = requests.post(url, headers=headers, data=json.dumps(data), stream=True)
    
    # 处理流式响应
    def generate():
        buffer = ""
        separators = ",。!?;"  # 分隔符列表
        if api_response.status_code == 200:
            for line in api_response.iter_lines():
                if line:
                    decoded_line = line.decode('utf-8')
                    if decoded_line.startswith("data: "):
                        try:
                            json_data = json.loads(decoded_line[6:])
                            if json_data.get("choices"):
                                delta = json_data["choices"][0].get("delta")
                                if delta and delta.get("content"):
                                    content = delta["content"]
                                    buffer += content
                                    # 检查是否遇到分隔符
                                    for sep in separators:
                                        if sep in buffer:
                                            parts = buffer.split(sep)
                                            for part in parts[:-1]:
                                                yield json.dumps({"answer": part + sep, "isEnd": False}) + "\n"
                                            buffer = parts[-1]
                                            break
                                elif json_data["choices"][0].get("finish_reason") == "stop":
                                    if buffer:
                                        yield json.dumps({"answer": buffer, "isEnd": False}) + "\n"
                                    yield json.dumps({"answer": "", "isEnd": True}) + "\n"
                                    return  # 退出生成器函数
                        except json.JSONDecodeError as e:
                            if decoded_line[6:] != "[DONE]":
                                print(f"JSONDecodeError: {e}, line: {decoded_line[6:]}")
                            else:
                                if buffer:
                                    yield json.dumps({"answer": buffer, "isEnd": False}) + "\n"
                                yield json.dumps({"answer": "", "isEnd": True}) + "\n"
                                return  # 退出生成器函数
        else:
            yield json.dumps({"answer": f"Error: {api_response.status_code}", "isEnd": True}) + "\n"
    
    # 返回流式响应
    response = Response(generate(), content_type='application/json')
    
    # 记录结束时间并计算耗时
    end_time = time.time()
    elapsed_time = end_time - start_time
    print(f"Request processed in {elapsed_time:.2f} seconds")
    
    return response
    

    if name == 'main': app.run(debug=True, port=5001, use_reloader=False)

    这个程序,我想通过?或者其它方式来区分不一样的config.api如何实现?

  2. Assistant

    Certainly! If you want to differentiate and switch between different config.api settings based on the request, you can modify your code to select the appropriate configuration dynamically. Here's how you can achieve this:

    1. Update Your Configuration File

    First, modify your config.ini to include multiple API configurations. For example:

    [API_DEFAULT]
    url = https://default-api.example.com
    authorization = Bearer default_token
    
    [API_ALTERNATE]
    url = https://alternate-api.example.com
    authorization = Bearer alternate_token
    

    2. Modify Your Flask App to Select Configuration Based on Request

    You can use query parameters (the "?" in the URL), JSON payload, or headers to determine which API configuration to use. Below is an example using query parameters:

    @app.route('/chat', methods=['POST'])
    def chat():
        global saved_uuid
    
        # Record start time
        start_time = time.time()
    
        # Parse request
        req_data = request.json
        sid = req_data.get('sid')
        dh_code = req_data.get('dh-code')
        dh_question = req_data.get('dh-question')
        dh_conversation_id = req_data.get('dh-conversation-id')
        dh_context = req_data.get('dh-context', "{}")
    
        # Determine which API configuration to use
        api_name = request.args.get('api')  # From query parameters
        if not api_name:
            api_name = 'API_DEFAULT'  # Default to 'API_DEFAULT' if not specified
    
        # Retrieve appropriate configuration
        if api_name in config:
            api_config = config[api_name]
        else:
            return Response(
                json.dumps({"error": "Invalid API name"}),
                status=400,
                content_type='application/json'
            )
    
        # Set headers and URL based on selected configuration
        headers = {
            "Authorization": api_config['authorization'],
            "Content-Type": "application/json"
        }
        url = api_config['url']
    
        # Rest of your code...
        # ...
    
        return response
    

    3. Update Your Client Requests

    When making requests to your Flask app, include the desired API configuration name as a query parameter. For example:

    • To use the default API configuration:
      POST http://yourdomain.com/chat
      
    • To use the alternate API configuration:
      POST http://yourdomain.com/chat?api=API_ALTERNATE
      

    4. Alternative Methods

    If you prefer not to use query parameters, you can pass the API name in the JSON payload or as a custom header.

    Using JSON Payload:

    api_name = req_data.get('api_name', 'API_DEFAULT')  # Default to 'API_DEFAULT' if not specified
    

    Using Custom Header:

    api_name = request.headers.get('X-API-NAME', 'API_DEFAULT')  # Default to 'API_DEFAULT' if not specified
    

    5. Complete Example

    Here's the modified chat function incorporating the changes:

    @app.route('/chat', methods=['POST'])
    def chat():
        global saved_uuid
    
        # Record start time
        start_time = time.time()
    
        # Parse request
        req_data = request.json
        sid = req_data.get('sid')
        dh_code = req_data.get('dh-code')
        dh_question = req_data.get('dh-question')
        dh_conversation_id = req_data.get('dh-conversation-id')
        dh_context = req_data.get('dh-context', "{}")
    
        # Determine which API configuration to use
        api_name = request.args.get('api', 'API_DEFAULT')  # Default to 'API_DEFAULT' if not specified
    
        # Retrieve appropriate configuration
        if api_name in config:
            api_config = config[api_name]
        else:
            return Response(
                json.dumps({"error": "Invalid API name"}),
                status=400,
                content_type='application/json'
            )
    
        # Set headers and URL based on selected configuration
        headers = {
            "Authorization": api_config['authorization'],
            "Content-Type": "application/json"
        }
        url = api_config['url']
    
        # Rest of your code...
        # ...
    
        # Record end time and calculate elapsed time
        end_time = time.time()
        elapsed_time = end_time - start_time
        print(f"Request processed in {elapsed_time:.2f} seconds")
    
        return response
    

    Summary

    By modifying your code to select the API configuration based on a parameter in the request, you can easily switch between different configurations. This method provides flexibility and allows you to handle multiple APIs within the same application.

    Notes

    • Ensure that the config.ini file includes sections for all the APIs you intend to use.
    • Always validate the api_name parameter to prevent errors.
    • Consider using default values to handle cases where the parameter is not provided.
    • Be cautious with security; ensure that exposing different APIs in this way doesn’t introduce security vulnerabilities.

    Let me know if you need further assistance or clarification!