chenpei d0dc94e36d feat: 将弹幕功能替换为AI聊天助手
新功能:
- 创建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助手体验!
2025-10-27 17:45:37 +08:00

138 lines
5.0 KiB
Python
Executable File

# -*- coding: utf-8 -*-
import json
import threading
from volcengine.ApiInfo import ApiInfo
from volcengine.Credentials import Credentials
from volcengine.base.Service import Service
from volcengine.ServiceInfo import ServiceInfo
service_info_map = {
"cn-beijing": ServiceInfo("open.volcengineapi.com", {'accept': 'application/json', },
Credentials('', '', "emr", "cn-beijing"), 60 * 5, 60 * 5, "https"),
"cn-guangzhou": ServiceInfo("open.volcengineapi.com", {'accept': 'application/json', },
Credentials('', '', "emr", "cn-guangzhou"), 60 * 5, 60 * 5, "https"),
"cn-shanghai": ServiceInfo("open.volcengineapi.com", {'accept': 'application/json', },
Credentials('', '', "emr", "cn-shanghai"), 60 * 5, 60 * 5, "https"),
}
api_info = {
# https://www.volcengine.com/docs/6491/145561
"CreateCluster": ApiInfo("POST", "/", {
"Action": "CreateCluster", "Version": "2022-06-30"}, {}, {}),
# https://www.volcengine.com/docs/6491/145562
"ResizeCluster": ApiInfo("GET", "/", {
"Action": "ResizeCluster", "Version": "2022-04-15"}, {}, {}),
# https://www.volcengine.com/docs/6491/145563
"DescribeCluster": ApiInfo("GET", "/", {
"Action": "DescribeCluster", "Version": "2022-04-15"}, {}, {}),
# https://www.volcengine.com/docs/6491/145564
"ListInstances": ApiInfo("POST", "/", {
"Action": "ListInstances", "Version": "2022-06-30"}, {}, {}),
# https://www.volcengine.com/docs/6491/145565
"ListClusters": ApiInfo("GET", "/", {
"Action": "ListClusters", "Version": "2021-09-15"}, {}, {}),
# https://www.volcengine.com/docs/6491/145566
"ListInstanceGroups": ApiInfo("POST", "/", {
"Action": "ListInstanceGroups", "Version": "2022-06-30"}, {}, {}),
# https://www.volcengine.com/docs/6491/145567
"ReleaseCluster": ApiInfo("POST", "/", {
"Action": "ReleaseCluster", "Version": "2022-04-15"}, {}, {}),
# https://www.volcengine.com/docs/6491/145568
"AddTags": ApiInfo("POST", "/", {
"Action": "AddTags", "Version": "2022-06-30"}, {}, {}),
# https://www.volcengine.com/docs/6491/145569
"RemoveTags": ApiInfo("POST", "/", {
"Action": "RemoveTags", "Version": "2022-06-30"}, {}, {}),
}
class EMRService(Service):
_instance_lock = threading.Lock()
def __new__(cls, *args, **kwargs):
if not hasattr(EMRService, "_instance"):
with EMRService._instance_lock:
if not hasattr(EMRService, "_instance"):
EMRService._instance = object.__new__(cls)
return EMRService._instance
def __init__(self, region="cn-beijing"):
self.service_info = EMRService.get_service_info(region)
self.api_info = EMRService.get_api_info()
super(EMRService, self).__init__(self.service_info, self.api_info)
@staticmethod
def get_service_info(region):
service_info = service_info_map.get(region, None)
if not service_info:
raise Exception('do not support region %s' % region)
return service_info
@staticmethod
def get_api_info():
return api_info
def create_cluster(self, params, body):
res = self.json("CreateCluster", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def describe_cluster(self, params):
res = self.get("DescribeCluster", params)
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def list_instances(self, params, body):
res = self.json("ListInstances", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def list_clusters(self, params):
res = self.get("ListClusters", params)
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def list_instance_groups(self, params, body):
res = self.json("ListInstanceGroups", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def release_cluster(self, params, body):
res = self.json("ReleaseCluster", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def add_tags(self, params, body):
res = self.json("AddTags", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json
def remove_tags(self, params, body):
res = self.json("RemoveTags", params, json.dumps(body))
if res == '':
raise Exception("empty response")
res_json = json.loads(res)
return res_json