Vue+Flask实现一个最基础的LLM请求响应以及渲染

后端请求 后端封装 from abc import ABC, abstractmethod from openai import OpenAI import config class BaseAIService(ABC): """AI服务的抽象基类""" @abstractmethod def get_stream_completion(self, messages): """获取流式响应""" pass @abstractmethod async def process_stream_response(self, response): """处理流式响应""" pass class HuoShanAIService(BaseAIService): """火山引擎 AI 服务实现""" def __init__(self): self.config = config.HUO_SHAN_COMPLETION_CONFIG self.client = OpenAI( api_key=self.config["api_key"], base_url=self.config["base_url"] ) def get_stream_completion(self, messages): """获取流式响应""" return self.client.chat.completions.create( model=self.config["fast_llm"], messages=messages, temperature=0.3, top_p=1, max_tokens=8192, stream=True ) async def process_stream_response(self, response): """处理流式响应""" reasoning_content = "" answer_content = "" is_answering = False try: for chunk in response: try: if not chunk.choices or not chunk.choices[0].delta: continue delta = chunk.choices[0].delta # 处理思考过程 if hasattr(delta, 'reasoning_content') and delta.reasoning_content: reasoning_text = delta.reasoning_content print(reasoning_text, end='', flush=True) reasoning_content += reasoning_text yield f"__r__{reasoning_text}".encode('utf-8', errors='ignore') # 处理回复内容 elif hasattr(delta, 'content') and delta.content: if not is_answering: print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n") is_answering = True content_text = delta.content print(content_text, end='', flush=True) answer_content += content_text yield f"__a__{content_text}".encode('utf-8', errors='ignore') except UnicodeEncodeError as e: print(f"Encoding error: {e}") continue except Exception as e: print(f"Error processing chunk: {e}") yield f"__a__处理响应时发生错误: {str(e)}".encode('utf-8', errors='ignore') except Exception as e: print(f"Stream processing error: {e}") yield f"__a__处理流式响应时发生错误: {str(e)}".encode('utf-8', errors='ignore') def get_ai_service(provider="huoshan"): """工厂函数,根据提供商返回对应的 AI 服务实例""" providers = { "huoshan": HuoShanAIService, # 未来可以添加其他提供商 # "openai": OpenAIService, # "azure": AzureAIService, } service_class = providers.get(provider) if not service_class: raise ValueError(f"不支持的 AI 提供商: {provider}") return service_class() 调用并且返回值 ...

2025-03-28 · 3 min · 1209 words · Waite Wang