import requests
import json

api_key = "Bearer fcc424e5-58af-494d-9683-5787413a26c9"


def get_ai_chat_history_lite(messages, stream=False):
    url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"

    payload = json.dumps({
        "model": "ep-m-20250408105049-7dj9r",
        "stream": stream,
        "messages": messages
    })
    headers = {
        'Authorization': api_key,
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    data = response.json()
    ai_reply = data.get('choices', [{}])[0].get('message', {})
    total_tokens = data.get('usage', {}).get('total_tokens')
    return ai_reply, total_tokens


# chat
def get_ai_chat_history(messages, stream=False):
    url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"

    payload = json.dumps({
        "model": "ep-20241214232538-x27zp",
        "stream": stream,
        "messages": messages
    })
    headers = {
        'Authorization': api_key,
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=payload)
    data = response.json()
    ai_reply = data.get('choices', [{}])[0].get('message', {})
    total_tokens = data.get('usage', {}).get('total_tokens')
    return ai_reply, total_tokens


# rag_联网插件
def get_bot_with_history(messages):
    url = "https://ark.cn-beijing.volces.com/api/v3/bots/chat/completions"

    payload = json.dumps({
        "model": "bot-20250730164611-nbsng",
        "stream": False,
        "stream_options": {
            "include_usage": False
        },
        "messages":messages
    })
    headers = {
        'Authorization': api_key,
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    data = response.json()
    ai_reply = data.get('choices', [{}])[0].get('message', {})
    total_tokens = data.get('bot_usage', {}).get('model_usage')[0].get('total_tokens')
    return ai_reply, total_tokens


def get_bot_with_history_rag(messages):
    url = "https://ark.cn-beijing.volces.com/api/v3/bots/chat/completions"

    payload = json.dumps({
        "model": "bot-20250725180737-mjxbx",
        "stream": False,
        "stream_options": {
            "include_usage": False
        },
        "messages":messages
    })
    headers = {
        'Authorization': api_key,
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    data = response.json()
    ai_reply = data.get('choices', [{}])[0].get('message', {})
    total_tokens = data.get('bot_usage', {}).get('model_usage')[0].get('total_tokens')
    return ai_reply, total_tokens




def get_image_to_text(content):
    url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"

    payload = json.dumps({
       "model": "doubao-seed-2-0-lite-260215",
       "messages": [
        {
            "content": content,
            "role": "user"
        }
    ]
    })
    headers = {
       'Authorization': api_key,
       'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)
    data = response.json()
    ai_reply = data.get('choices', [{}])[0].get('message', {})
    total_tokens = data.get('usage', {}).get('total_tokens')
    return ai_reply, total_tokens



if __name__ == '__main__':
    content = [
                {"type": "image_url","image_url": {"url":  "https://douchacha-web.tos-cn-beijing.volces.com/assets/6583982362302876935/cover.jpg"}},
                # {"type": "image_url","image_url": {"url":  "https://douchacha-web.tos-cn-beijing.volces.com/assets/7283099688222379304/1.jpg"}},
                {"type": "text", "text": "提取图片中的文字"},
            ]
    print(get_image_to_text(content))
    # list = []
    # for  i in range(10):
    #     list.append(f"https://douchacha-web.tos-cn-beijing.volces.com/assets/7283099688222379304/{i}.jpg")
    # print(list)


