import requests
API_BASE1 = "http://8.209.215.15/api/openai/v1/chat/completions"
API_BASE2 = "https://api.openai.com/v1"
API_BASE3 = "http://47.251.63.29:8083/api/openai/"
OPENAI_API_KEY0 = "sk-svcacct-Cnz3hjiTXZzXuwUXnThVSyjPSC66byKALZzJrUOOcUOjT3BlbkFJn2b1V5pfBMdx_M52dW_o9oPp1q-uXdwz4fUwouuXRWZJgA"
OPENAI_API_KEY1 = "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"
OPENAI_API_KEY2 = "sk-ling-agent-server-efdvHRcWdNWo5ftZ88vxT3BlbkFJUgWXoYOeRKnsIfpa5TKN"
# 配置 OpenAI API 密钥和代理 URL
# openai_key = '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'
# openai_proxy_url = 'http://47.251.70.106:8080/proxy/v1.0/chatgpt/chat'
# openai_proxy_url_v2 = 'https://api.gptsapi.net/v1/chat/completions'

openai_key = OPENAI_API_KEY1
openai_proxy_url = API_BASE1

def call_openai_api(prompt, use_v2=False):
    """
    调用 OpenAI API，获取模型的响应。

    参数：
    - prompt: 用户输入的提示信息。
    - use_v2: 是否使用 openai_proxy_url_v2。

    返回：
    - 模型的响应内容。
    """
    # 选择使用的代理 URL
    url = openai_proxy_url

    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {openai_key}'
    }

    # 构建请求数据
    data = {
        'model': 'gpt-3.5-turbo',  # 可以根据需要更改模型名称
        'messages': [
            {'role': 'user', 'content': prompt}
        ],
        'temperature': 0.7,
        'max_tokens': 150,
        'n': 1,
        'stop': None,
    }

    try:
        # 发送 POST 请求
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()  # 如果响应状态码不是 200，抛出异常

        # 解析响应数据
        result = response.json()
        content = result['choices'][0]['message']['content']
        return content.strip()
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP 错误：{http_err}")
    except Exception as err:
        print(f"其他错误：{err}")

    return None

# 示例用法
if __name__ == "__main__":
    prompt = "请为我写一首关于春天的诗。"
    response = call_openai_api(prompt)
    if response:
        print("模型的回答：")
        print(response)