| 312 | return html |
| 313 | # 使用类来管理全局状态,避免全局变量问题 |
| 314 | class ModelManager: |
| 315 | def __init__(self): |
| 316 | self.session = None |
| 317 | self.is_initialized = False |
| 318 | |
| 319 | def initialize_model(self, model_path, retriver_url, api_url, api_key, tool_model_name, stop_token): |
| 320 | """初始化模型""" |
| 321 | try: |
| 322 | # 初始化OpenAI客户端 |
| 323 | client = OpenAI( |
| 324 | api_key=api_key if api_key else "EMPTY", |
| 325 | base_url=api_url + "/v1", |
| 326 | ) |
| 327 | |
| 328 | # 配置字典 |
| 329 | config = { |
| 330 | 'num_passages_one_retrieval': 3, |
| 331 | 'num_passages_one_split_retrieval': 5, |
| 332 | 'max_num_passages': 80, |
| 333 | 'num_search_one_attempt': 10, |
| 334 | 'api_try_counter': 3, |
| 335 | 'retriver_url': retriver_url, |
| 336 | 'model_name': tool_model_name, |
| 337 | 'api_url': api_url, |
| 338 | 'openai_suffix': "/v1", |
| 339 | 'client': client, |
| 340 | } |
| 341 | |
| 342 | # 初始化LLM |
| 343 | llm = LLM(model=model_path, trust_remote_code=True) |
| 344 | |
| 345 | # 解析stop_token |
| 346 | stop_token_ids = [] |
| 347 | if stop_token.strip(): |
| 348 | try: |
| 349 | stop_token_ids = [int(stop_token.strip())] |
| 350 | except ValueError: |
| 351 | return "❌ Error: Stop token must be a valid integer" |
| 352 | |
| 353 | sampling_params = SamplingParams( |
| 354 | temperature=0.3, |
| 355 | max_tokens=512, |
| 356 | stop_token_ids=stop_token_ids if stop_token_ids else [128009] |
| 357 | ) |
| 358 | |
| 359 | self.session = VisualizationChatSession(llm, sampling_params, config) |
| 360 | self.is_initialized = True |
| 361 | |
| 362 | return "✅ Model initialized successfully!" |
| 363 | except Exception as e: |
| 364 | self.is_initialized = False |
| 365 | return f"❌ Error initializing model: {str(e)}" |
| 366 | |
| 367 | def process_question(self, question, max_steps): |
| 368 | """处理问题并返回可视化结果""" |
| 369 | if not self.is_initialized or self.session is None: |
| 370 | return "❌ Please initialize the model first!", "" |
| 371 |
no outgoing calls
no test coverage detected