from typing import Any, Coroutine

from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox

import requests
import json

from injector import singleton
@singleton
class Fastchat(Blackbox):
    
    def __call__(self, *args, **kwargs):
        return self.processing(*args, **kwargs)

    def valid(self, *args, **kwargs) -> bool:
        data = args[0]
        return isinstance(data, list)

    # model_name有 Qwen1.5-14B-Chat , internlm2-chat-20b
    def processing(self, model_name, prompt, template, context: list)  -> str:
        if context == None:
            context = []
        url = 'http://120.196.116.194:48892/v1/chat/completions'

        # history可以为空列表，也可以是用户的对话历史
        # history = [
        #     {
        #         "role": "user",
        #         "content": "你吃饭了吗"
        #     },
        #     {
        #         "role": "assistant", 
        #         "content": "作为一个AI模型，我没有吃饭的需要，因为我并不具备实体形态。我专注于提供信息和帮助回答你的问题。你有什么需要帮助的吗？"
        #     },
        # ]

        fastchat_inputs={
            "model": model_name,
            "messages": context + [
                {
                    "role": "user",
                    "content": template + prompt
                }
            ]
        }

        fastchat_response = requests.post(url, json=fastchat_inputs)

        user_message = fastchat_inputs["messages"]
        context.append(user_message)

        assistant_message = fastchat_response.json()["choices"][0]["message"]
        context.append(assistant_message)

        fastchat_content = assistant_message["content"]

        return fastchat_content
    
    async def fast_api_handler(self, request: Request) -> Response:
        try:
            data = await request.json()
        except:
            return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)

        user_model_name = data.get("model_name")
        user_context = data.get("context")
        user_prompt = data.get("prompt")
        user_template = data.get("template")

        if user_prompt is None:
            return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST)

        if user_model_name is None or user_model_name.isspace():
            user_model_name = "Qwen1.5-14B-Chat"

        if user_template is None or user_template.isspace():
            # user_template 是定义LLM的语气，例如template = "使用小丑的语气说话。"，user_template可以为空字串，或者是用户自定义的语气，或者是使用我们提供的语气
            user_template = ""
        else:
            user_template = f"使用{user_template}的语气说话。"

        return JSONResponse(content={"response": self.processing(user_model_name, user_prompt, user_template, user_context)}, status_code=status.HTTP_200_OK)