#!/usr/bin/env python3
import sys
import io
from typing import List, Dict, Any, Optional, Tuple
import multiprocessing

import json
import logging
from pathlib import Path
import time

from curses.panel import top_panel
from email import message
import os
# from langchain.agents import initialize_agent, AgentType
# from langchain.tools import BaseTool, Tool
# from langchain.llms import LlamaCpp
# from langchain.callbacks.manager import CallbackManager
# from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
# from langchain.prompts import PromptTemplate
# from langchain.chains import LLMChain
# import magic  # 用于识别文件类型（辅助文件搜索工具）

# from langchain.vectorstores import Chroma
# from langchain.embeddings import LlamaCppEmbeddings
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# from langchain.document_loaders import (
#     TextLoader, UnstructuredFileLoader, PDFMinerLoader,
#     UnstructuredMarkdownLoader, Docx2txtLoader
# )
# from langchain.chains import RetrievalQA
# from langchain.memory import ConversationBufferWindowMemory
# from langchain.prompts import PromptTemplate
# from langchain.schema import HumanMessage, AIMessage

from cv2 import log
from llama_cpp import Llama, LlamaGrammar
from llama_cpp.llama_chat_format import Llava15ChatHandler,Qwen25VLChatHandler

import argparse
import base64

from PIL import Image
import numpy as np

from fastapi import FastAPI, HTTPException, APIRouter, Depends
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware

from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter

from sympy import content
import uvicorn

# 获取当前脚本的路径（Path 对象）
current_script = Path(__file__).resolve()
# 向上两级找到项目根目录（根据实际结构调整层级）
project_root = current_script.parents[1]  # parents[0] 是当前目录，parents[1] 是父目录，以此类推

print("project_root: ",project_root)

#添加到搜索路径
sys.path.append(str(project_root))

#查看系统环境
print("sys path:",sys.path)

from tools.core.common_functions import read_json_file
from tools.data_process.image_process.image_process import decode_base64_image, preprocess_image,PIL_image_to_base64,PIL_image_to_base64_sizelmt
from models.model_definition import (TextInferenceRequest,MultimodalInferenceRequest,InferenceRequest,InferenceResponse,HealthResponse)

# 配置日志记录器
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


shared_state = multiprocessing.Manager().dict()
shared_state["model_loaded"] = False
shared_state["model_path"] = ""
shared_state["load_start_time"] = 0
shared_state["load_complete"] = False

# 进程内单例变量
_process_instance = None
_process_initialized = False



class Models_Server:
    """多模态大模型的服务"""

    _instance = None

    def __new__(cls, *args, **kwargs):
        """确保每个进程只有一个实例"""
        # global _model_server_instance
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance


    def __init__(self,config):

        """
        初始化模型服务，仅在首次实例化时加载模型
        """
        global _process_instance, _process_initialized
        # 每个进程只初始化一次
        if _process_instance and _process_initialized:
            self.model = _process_instance.model
            # self.model_path = _process_instance.model_path
            self.process_id = os.getpid()
            return
        #model: vlm，llm
        self.process_id = os.getpid()
        self.model = None  
        self.model_loaded = False
        self.load_start_time = 0.0
        self.load_end_time = None
        self.model_server_config = config
        # self.inference_start_time = 0.0

        # #定义服务器地址和端口
        # self.model_server_host = None
        # self.model_server_port = None
        # self._wait_for_model_load(config)
        # self.init_model(config["model_inference_framework_type"],config)

        # 标记进程内初始化完成
        _process_initialized = True
        _process_instance = self
        logger.info(f"model server init success! process_id :{self.process_id}")
    
    # def parse_config(self,config_json_file):
    #     """解析model server的config文件"""
        
    #     self.model_server_config= read_json_file(config_json_file)

    def _wait_for_model_load(self,config:Optional[dict[str,Any]]=None):
        """等待首个进程完成模型加载"""

        if config is None:
            config = self.model_server_config

        if not shared_state["load_complete"]:
            # 检查是否是第一个启动的进程
            if shared_state["load_start_time"] == 0:
                # 第一个进程负责设置加载状态
                shared_state["load_start_time"] = time.time()
                shared_state["model_path"] = config["vlm_model_path"]
            else:
                # 其他进程等待加载完成
                print(f"进程 {self.process_id} 等待模型加载...")
                while not shared_state["load_complete"]:
                    if time.time() - shared_state["load_start_time"] > 600:  # 10分钟超时
                        raise Exception("模型加载超时")
                    time.sleep(2)


    def init_vlm_llamacpp(self,config:Optional[dict[str,Any]]=None):
        """
        初始化加载多模态大模型
        """
        if config is None:
            config = self.model_server_config
        
        self.load_start_time = time.time()
        
        try:

            chat_handler=None
            if config["multi_modal"]:
                if "Qwen25VLChatHandler" == config["chat_handler"]:
                    chat_handler = Qwen25VLChatHandler(clip_model_path=config["mmproj_model_path"])
                else :
                    logger.error("模型chat_format未知")
                    
            # 加载模型
            model= Llama(
                model_path=config["vlm_model_path"],
                chat_handler = chat_handler,
                n_ctx=config["n_ctx"],  # 上下文窗口大小
                n_threads=config["n_threads"],  # 线程数
                n_gpu_layers=config["n_gpu_layers"],  # GPU层数量，根据显卡调整
                verbose=config["verbose"],# 减少日志输出
                # chat_format=config["chat_format"] # 多模态模型通常使用llava格式
                n_batch = config["n_batch"],
                n_ubatch = config["n_ubatch"]
            )
            
            logger.info(f"模型加载成功，耗时: {time.time() - self.load_start_time:.2f}秒")
            return model
        except Exception as e:
            logger.error(f"模型加载失败: {str(e)}")
            return None

    def init_model(self, model_inference_framework_type:Optional[str]=None,
                   config:Optional[dict[str,Any]]=None):
        """初始化模型"""

        if config is None:
            config = self.model_server_config
        
        if model_inference_framework_type is None:
            model_inference_framework_type = config["model_inference_framework_type"]

        
        try:
            logger.info(f"进程 {self.process_id} 开始加载模型...")
            if "llama_cpp" == model_inference_framework_type:
                self.model = self.init_vlm_llamacpp(config)
            
            #TODO 其他加载模型的推理框架入口
            else:
                logger.error(f"加载模型的框架未知，加载模型失败。")
                self.model_loaded = False

            if self.model:
                logger.info(" model load success!!!!")
                self.model_loaded = True
                # 如果是第一个完成加载的进程，更新共享状态
                if not shared_state["load_complete"]:
                    shared_state["load_complete"] = True
                    shared_state["model_loaded"] = True
                    shared_state["load_time"] = time.time() - shared_state["load_start_time"]
            
                    logger.info(f"进程 {self.process_id} 模型加载完成")

            else:
                self.model_loaded = False
                logger.error(f"进程 {self.process_id} 模型加载失败（model 为 None）")
        except Exception as e:
            print(f"进程 {self.process_id} 模型加载失败: {str(e)}")
            raise
    
    def _build_prompt(self, usr_prompt: str, system_prompt: Optional[str]) -> str:
        """构建符合QwenVL2格式的提示词"""
        if system_prompt:
            return f"SYSTEM:{system_prompt}\nUSER:{usr_prompt}\nASSISTANT:"
        return f"USER:{usr_prompt}\nASSISTANT:"
    
    def _build_message(self,request:InferenceRequest,config:Optional[dict[str,Any]]=None):
        """构建模型推理的message"""

        if config is None:
            config = self.model_server_config
        try:
            messages = []
            has_images = request.image_data and len(request.image_data) > 0
            chat_handler = config.get("chat_handler", "")

            # 构建 system message
            if request.system_prompt:
                if has_images and "Qwen25VLChatHandler" == chat_handler:
                    # 多模态情况下，system message 的 content 也应该是列表
                    system_message = {
                        "role": "system",
                        "content": [{"type": "text", "text": request.system_prompt}]
                    }
                else:
                    # 纯文本情况下，system message 的 content 应该是字符串
                    system_message = {
                        "role": "system",
                        "content": request.system_prompt
                    }
                messages.append(system_message)
            
            # 构建 user message
            if has_images:
                # 多模态消息：content 是列表
                user_content = []
                
                if "Qwen25VLChatHandler" == chat_handler:
                    # Qwen25VL 格式
                    len_images = len(request.image_data)
                    logger.info(f"添加 {len_images} 张图像到消息 (Qwen25VL格式)")
                    for i in range(0, len_images):
                        logger.info(f"add image {i}")
                        user_content.append({
                            "type": "image_url",
                            "image_url": {"url": f"data:image/jpeg;base64,{request.image_data[i]}"}
                        })
                else:
                    # 其他多模态格式
                    len_images = len(request.image_data)
                    logger.info(f"添加 {len_images} 张图像到消息")
                    for i in range(0, len_images):
                        logger.info(f"add image {i}")
                        user_content.append({
                            "type": "image",
                            "image": f"data:image/jpeg;base64,{request.image_data[i]}"
                        })
                
                # 添加文本内容
                user_content.append({"type": "text", "text": request.user_prompt})
                
                user_message = {
                    "role": "user",
                    "content": user_content
                }
            else:
                # 纯文本消息：content 是字符串
                user_message = {
                    "role": "user",
                    "content": request.user_prompt
                }
            
            messages.append(user_message)
            return messages
        except Exception as e:
            logger.error(f"进程 {self.process_id} 构建模型推理message失败: {str(e)}", exc_info=True)
            return None

    def text_prompt_inference(self, request: TextInferenceRequest):
        """处理纯文本提示词非格式化推理"""

        # global model, model_loaded
        logger.info(f"model_loaded={self.model_loaded}!!flag1")
        logger.info(f"model={self.model}!!flag1")
        
        if not self.model_loaded or self.model is None:
            logger.error(f"model_loaded={self.model_loaded}!!flag")
            raise HTTPException(status_code=503, detail="模型未加载完成，请稍后再试")
        
        try:
            inference_start_time = time.time()
            
            # 构建提示
            prompt = f"USER: {request.user_prompt}\nASSISTANT:"
            
            # 生成响应
            output = self.model.create_completion(
                prompt=prompt,
                max_tokens=request.max_tokens,
                temperature=request.temperature,
                top_p=request.top_p,
                stop=request.stop ,
                stream=False
            )
            
            inference_time = time.time() - inference_start_time
            result = output["choices"][0]["text"].strip()
            
            return {
                "result": result,
                "metadata": {
                    "inference_time_seconds": inference_time,
                    "tokens_generated": len(self.model.tokenize(result.encode())),
                    "model": self.model.model_path.split("/")[-1],
                    "process_id": self.process_id

                }
            }
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"推理失败: {str(e)}")
    
    def text_inference(self, request: TextInferenceRequest):
        """处理纯文本提示词格式化推理"""

        # global model, model_loaded
        logger.info(f"model_loaded={self.model_loaded}!!flag1")
        logger.info(f"model={self.model}!!flag1")
        
        if not self.model_loaded or self.model is None:
            logger.error(f"model_loaded={self.model_loaded}!!flag")
            raise HTTPException(status_code=503, detail="模型未加载完成，请稍后再试")
        
        try:
            inference_request = InferenceRequest(
                user_prompt = request.user_prompt,
                max_tokens = request.max_tokens,
                temperature = request.temperature,
                top_p = request.top_p,
                system_prompt = request.system_prompt,
                stop = request.stop,
                image_data = None,  # 纯文本推理，没有图像
                json_schema = request.json_schema  # 传递 JSON Schema
            )
            messages = self._build_message(inference_request, self.model_server_config)
            
            # 检查 messages 是否为 None
            if messages is None:
                logger.error("构建消息失败：_build_message 返回 None")
                raise HTTPException(status_code=500, detail="构建推理消息失败")
            
            logger.info(f"构建的消息: {messages}")
            inference_start_time = time.time()
            
            # 准备 response_format 参数（如果提供了 json_schema）
            response_format = None
            if request.json_schema:
                response_format = {
                    "type": "json_object",
                    "schema": request.json_schema
                }
                logger.info(f"使用 JSON Schema 约束输出格式")
            
            # 生成响应
            output = self.model.create_chat_completion(
                messages=messages,
                max_tokens=request.max_tokens,
                temperature=request.temperature,
                top_p=request.top_p,
                stop=request.stop,
                stream=False,
                response_format=response_format
            )
            
            inference_time = time.time() - inference_start_time
            result = output["choices"][0]["message"]["content"].strip()
            
            return {
                "result": result,
                "metadata": {
                    "inference_time_seconds": inference_time,
                    "tokens_generated": len(self.model.tokenize(result.encode())),
                    "model": self.model.model_path.split("/")[-1],
                    "process_id": self.process_id

                }
            }
        except HTTPException:
            raise
        except Exception as e:
            logger.error(f"推理失败: {str(e)}", exc_info=True)
            raise HTTPException(status_code=500, detail=f"推理失败: {str(e)}")
    
    def multimodal_inference(self,request: MultimodalInferenceRequest):
        """处理大模型多模态推理（文本+图像）"""

        logger.info("multimodal_inference run")

        
        if not self.model_loaded or self.model is None:
            raise HTTPException(status_code=503, detail="模型未加载完成，请稍后再试")
        
        try:
            inference_start_time = time.time()
            
            # 解码并预处理所有图像，equest.image_data中图像是jpeg的base64格式
            # processed_base64_list = []
            # for img_b64 in request.image_data:
            #     try:
            #         image = decode_base64_image(img_b64)
            #         image.save("/home/ubuntu/Workspace/Projects/VLM_VLA/Datas/image/output_image.jpg")
            #         # 预处理（缩放、去透明通道等，确保模型兼容）
            #         processed_b64 = PIL_image_to_base64_sizelmt(image=image)
            #         # 将预处理后的图像重新编码为 Base64（关键：确保格式正确）
            #         processed_base64_list.append(processed_b64)
            #         logger.info("multimodal_inference decode image and save!")

            #         # processed_img = preprocess_image(image)
            #         # processed_base64_list.append(processed_img)
            #     except Exception as e:
            #         raise HTTPException(status_code=400, detail=f"处理图像失败: {str(e)}")
            
            
            # 构建多模态提示
            messages = self._build_message(request,self.model_server_config)

            len_images = len(request.image_data)
            # print(f"full_prompt: {messages}")
            
            # 准备 response_format 参数（如果提供了 json_schema）
            response_format = None
            if hasattr(request, 'json_schema') and request.json_schema:
                response_format = {
                    "type": "json_object",
                    "schema": request.json_schema
                }
                logger.info(f"使用 JSON Schema 约束输出格式")
            
            # 生成响应
            output = self.model.create_chat_completion(
                messages=messages,
                max_tokens=request.max_tokens,
                temperature=request.temperature,
                top_p=request.top_p,
                stop=request.stop,
                stream=False,
                response_format=response_format
            )
            
            inference_time = time.time() - inference_start_time
            result = output["choices"][0]["message"]["content"].strip()
            
            return {
                "result": result,
                "metadata": {
                    "inference_time_seconds": inference_time,
                    "num_images_processed": len_images,
                    "tokens_generated": len(self.model.tokenize(result.encode())),
                    "model": self.model.model_path.split("/")[-1]
                }
            }
        except HTTPException:
            raise
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"多模态推理失败: {str(e)}")
        

    def model_inference(self,request: InferenceRequest):
        """处理大模型(纯文本和多模态（文本+图像））推理"""

        logger.info("model_inference run")

        
        if not self.model_loaded or self.model is None:
            raise HTTPException(status_code=503, detail="模型未加载完成，请稍后再试")
        
        try:
            inference_start_time = time.time()
            

            messages = self._build_message(request,self.model_server_config)

            len_images = len(request.image_data)
            # print(f"full_prompt: {messages}")
            
            # 准备 response_format 参数（如果提供了 json_schema）
            response_format = None
            if hasattr(request, 'json_schema') and request.json_schema:
                response_format = {
                    "type": "json_object",
                    "schema": request.json_schema
                }
                logger.info(f"使用 JSON Schema 约束输出格式")
            
            # 生成响应
            output = self.model.create_chat_completion(
                messages=messages,
                max_tokens=request.max_tokens,
                temperature=request.temperature,
                top_p=request.top_p,
                stop=request.stop,
                stream=False,
                response_format=response_format
            )
            
            inference_time = time.time() - inference_start_time
            result = output["choices"][0]["message"]["content"].strip()
            
            return {
                "result": result,
                "metadata": {
                    "inference_time_seconds": inference_time,
                    "num_images_processed": len_images,
                    "tokens_generated": len(self.model.tokenize(result.encode())),
                    "model": self.model.model_path.split("/")[-1]
                }
            }
        except HTTPException:
            raise
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"多模态推理失败: {str(e)}")
    

##############################################################################################################################

model_router = InferringRouter()  # 支持自动推断参数类型


# # 依赖注入：获取单例模型服务
def get_model_server() -> Models_Server:
    """依赖注入：获取当前进程的单例服务实例"""
    global _process_instance  # 必须在函数开始处声明
    logger.info(f"=== get_model_server 被触发！Process ID: {os.getpid()} ===")

    if _process_instance is None:
        config_json_file = f"{project_root}/config/model_config.json"  # ✅ 修正路径
        print("config_json_file: ", config_json_file)
        
        # 读取配置文件
        config_data = read_json_file(config_json_file)
        if config_data is None:
            logger.error(f"get_model_server: 无法读取配置文件 '{config_json_file}'")
            raise HTTPException(status_code=500, detail=f"无法读取配置文件: {config_json_file}")
        
        # 检查 models_server 配置是否存在
        if "models_server" not in config_data:
            logger.error("get_model_server: 配置文件中缺少 'models_server' 字段")
            raise HTTPException(status_code=500, detail="配置文件中缺少 'models_server' 字段")
        
        # 如果 models_server 是列表，取第一个；如果是字典，直接使用
        models_server_data = config_data["models_server"]
        model_server_config = models_server_data[0] if isinstance(models_server_data, list) else models_server_data

        if model_server_config is None:
            logger.error("get_model_server: 配置未初始化！")
            raise HTTPException(status_code=500, detail="服务配置未初始化")
        
        # 获取单例实例
        model_server = Models_Server(model_server_config)
        if not model_server.model_loaded:
            model_server._wait_for_model_load(model_server_config)
            model_server.init_model(model_server_config["model_inference_framework_type"],
                                    model_server_config)
        # 保存到全局变量
        _process_instance = model_server
    else:
        model_server = _process_instance
        if not model_server.model_loaded:
            # 重新读取配置以获取 model_server_config
            config_json_file = f"{project_root}/config/model_config.json"
            config_data = read_json_file(config_json_file)
            if config_data is None or "models_server" not in config_data:
                logger.error("get_model_server: 无法读取配置文件")
                raise HTTPException(status_code=500, detail="无法读取配置文件")
            models_server_data = config_data["models_server"]
            model_server_config = models_server_data[0] if isinstance(models_server_data, list) else models_server_data
            model_server._wait_for_model_load(model_server_config)
            model_server.init_model(model_server_config["model_inference_framework_type"],
                                    model_server_config)

    logger.info(f"get_model_server 返回实例状态: 加载={model_server.model_loaded}, Process ID={model_server.process_id}")
    return model_server

@cbv(model_router)  # 将类与路由绑定
class Models_Controller:

    def __init__(self, model_server: Models_Server = Depends(get_model_server)):
        self.model_server = model_server  # 实例变量，确保每个请求可用
        logger.info(f"Models_Controller init success! Process ID: {os.getpid()}")

        """
        初始化成员变量
        """
        #model: vlm，llm
        # model_server = Depends(get_model_server)  
        # self.model_loaded = False
        # self.load_start_time = 0.0
        # self.inference_start_time = 0.0

        # #定义服务器地址和端口
        # self.model_server_host = None
        # self.model_server_port = None
        # logger.info("Models_Controller init success!")
        
        # def parse_config(self,config_path):

    def set_model_server_host(self,model_server_host):
        """
        设置模型推理服务器的地址
        """
        try:
            self.model_server_host = model_server_host
            return True
        except Exception as e:
            logger.error(f"设置模型推理服务器的地址失败: {str(e)}")
            return False
    
    def set_model_server_port(self,model_server_port):
        """
        设置模型推理服务器的端口
        """
        try:
            self.model_server_port = model_server_port
            return True
        except Exception as e:
            logger.error(f"设置模型推理服务器的端口失败: {str(e)}")
            return False

    @model_router.post("/text/inference", response_model=Dict[str, Any])
    async def text_inference(self, request: TextInferenceRequest):
        """纯文本推理端点"""
        try:
            return self.model_server.text_inference(request)
        except HTTPException:
            raise
        except Exception as e:
            logger.error(f"端点处理失败: {str(e)}", exc_info=True)
            raise HTTPException(status_code=500, detail=f"端点处理失败: {str(e)}")

    @model_router.post("/multimodal/inference", response_model=Dict[str, Any])
    async def multimodal_inference(self,request: MultimodalInferenceRequest):
        """大模型多模态推理（文本+图像）端点"""
        
        return self.model_server.multimodal_inference(request)
    
    @model_router.post("/model/inference", response_model=Dict[str, Any])
    async def model_inference(self,request: InferenceRequest):
        """大模型(纯文本或多模态（文本+图像））推理端点"""
        
        return self.model_server.model_inference(request)
    
    # API端点
    @model_router.get("/model/health", response_model=Dict[str, Any])
    async def model_health_check(self):
        """检查大模型服务健康状态"""

        return {
            "status": "healthy" if shared_state["model_loaded"] else "loading",
            "model_loaded": shared_state["model_loaded"],
            "process_id": self.model_server.process_id,
            "load_time": shared_state.get("load_time", 0),
            "model_path": shared_state.get("model_path", "")
        }

  
# def start_http_model_controller( model_server_host="localhost",model_server_port=8000,model_workers=1):
#     """
#     初始化并启用FastAPI应用
#     """
#     # #初始化加载模型
#     # self.init_model(config["model_inference_framework_type"],config)
#     # 初始化FastAPI应用
#     model_app = FastAPI(title="多模态大模型推理服务") 
#     # 配置CORS
#     # model_app.add_middleware(
#     #     CORSMiddleware,
#     #     allow_origins=["*"],
#     #     allow_credentials=True,
#     #     allow_methods=["*"],
#     #     allow_headers=["*"],
#     # )
#     try:
#         # 将路由注册到 app
#         model_app.include_router(model_router)

#         
#         uvicorn.run(app=model_app, host=model_server_host, port=model_server_port,workers=model_workers)
#         return True
#     except Exception as e:
#         logger.error(f"初始化并启用FastAPI应用失败: {str(e)}")
#         return False

def create_app() -> FastAPI:
    """创建FastAPI应用"""
    app = FastAPI(title="Multimodal API", version="1.0")
    
    # 配置CORS
    # app.add_middleware(
    #     CORSMiddleware,
    #     allow_origins=["*"],
    #     allow_credentials=True,
    #     allow_methods=["*"],
    #     allow_headers=["*"],
    # )
    
    # 注册路由
    app.include_router(model_router)
    
    return app


def main():
    print(f"project_root111:{project_root}")
    config_json_file= f"{project_root}/config/model_config.json"  # ✅ 修正路径
    print("config_json_file: ",config_json_file)
    config_data = read_json_file(config_json_file)["models_server"]
    # 如果 models_server 是列表，取第一个；如果是字典，直接使用
    model_server_config = config_data[0] if isinstance(config_data, list) else config_data

    uvicorn.run(
        app="models_server:create_app",
        host=model_server_config["model_server_host"],
        port=model_server_config["model_server_port"],
        workers=model_server_config["model_controller_workers"],
        factory=True,
        log_level="info"
    )

def test_inference(inference_type=0):
    from tools.core.common_functions import read_json_file
    config_json_file="/home/ubuntu/Workspace/Projects/VLM_VLA/UAV_AI/src/Model/AI_Agent/scripts/ai_agent/models/model_config.json"
    model_server_config = read_json_file(config_json_file)["models_server"][0]
    model_server = Models_Server(model_server_config)
    model_server._wait_for_model_load(model_server_config)
    if not model_server.model_loaded:
        model_server.init_model(model_inference_framework_type=model_server_config["model_inference_framework_type"],
                                config=model_server_config)
    system_prompt = ""
    if 2==inference_type:
        prompt = "检测图中的蓝色气球，并按照固定的结构(<|object_ref_start|>蓝色气球<|object_ref_end|><|box_start|>(xmin,ymin),(xamx,ymax)<|box_end|>)给出2D框坐标"
        # "请对图片进行场景描述！"
        # "请检测图片中蓝色的气球，请给出准确的2D坐标框的坐标！"
        # "检测图中的蓝色气球，并按照固定的结构(<|object_ref_start|>蓝色气球<|object_ref_end|><|box_start|>(xmin,ymin),(xamx,ymax)<|box_end|>)给出2D框坐标"
        test_image_path = "/home/ubuntu/Workspace/Projects/VLM_VLA/UAV_AI/datas/test1/balloon2.jpg"
        
        img_base64 = PIL_image_to_base64_sizelmt(image_path=test_image_path,need_size_lmt=False)
        multimodal_request = MultimodalInferenceRequest(
                user_prompt = prompt,
                image_data = [img_base64],
                max_tokens = 24576,
                temperature = 0.7,
                top_p = 0.95,
                system_prompt= system_prompt,
                stop = None)
    
        response = model_server.multimodal_inference(request=multimodal_request)
        logger.info(response)
    elif 1==inference_type:
        prompt = "请你描述一下《西游记》中孙悟空的特点！"
        text_request = TextInferenceRequest(
                user_prompt= prompt,
                max_tokens= 2048,
                temperature = 0.7,
                top_p= 0.95,
                system_prompt= system_prompt,
                stop = None)
        response = model_server.text_inference(request=text_request)
        logger.info(response)
    elif 0==inference_type:
        prompt = "请你描述一下《西游记》中孙悟空的特点！"
        text_request = TextInferenceRequest(
                user_prompt= prompt,
                max_tokens= 2048,
                temperature = 0.7,
                top_p= 0.95,
                system_prompt= system_prompt,
                stop = None)
        response = model_server.text_prompt_inference(request=text_request)
        logger.info(response)


if __name__ == "__main__":
    # main()
    # test_inference(2)
    logger.info("work finish")