新功能: - 创建AI聊天助手组件(AIChatWidget) - 支持文字+图片多模态对话 - 集成Gemini AI API - 支持图片编辑和内容生成 - 实时对话历史管理 - 优雅的UI设计,支持黑夜模式 技术实现: - 后端API代理Gemini API(/api/ai-chat) - 支持最多5张图片上传 - 流式响应处理 - 对话历史管理 - Base64图片处理 配置: - 在.env中添加Gemini API配置 - GEMINI_API_KEY - GEMINI_API_BASE_URL - GEMINI_MODEL 替换原弹幕功能,提供更强大的AI助手体验!
34 lines
1.1 KiB
Python
Executable File
34 lines
1.1 KiB
Python
Executable File
from typing import Iterator, Optional
|
|
|
|
from volcengine.maas.exception import new_client_sdk_request_error, MaasException
|
|
from volcengine.maas.utils import json_to_object
|
|
|
|
|
|
class BinaryResponseContent:
|
|
def __init__(self, response, request_id) -> None:
|
|
self.response = response
|
|
self.request_id = request_id
|
|
|
|
def stream_to_file(
|
|
self,
|
|
file: str
|
|
) -> None:
|
|
is_first = True
|
|
error_bytes = b''
|
|
with open(file, mode="wb") as f:
|
|
for data in self.response:
|
|
if len(error_bytes) > 0 or (is_first and "\"error\":" in str(data)):
|
|
error_bytes += data
|
|
else:
|
|
f.write(data)
|
|
|
|
if len(error_bytes) > 0:
|
|
resp = json_to_object(str(error_bytes, encoding="utf-8"), req_id=self.request_id)
|
|
raise MaasException(
|
|
resp.error.code_n, resp.error.code, resp.error.message, self.request_id
|
|
)
|
|
|
|
def iter_bytes(self) -> Iterator[bytes]:
|
|
for data in self.response:
|
|
yield data
|