新功能: - 创建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助手体验!
50 lines
1.8 KiB
Python
Executable File
50 lines
1.8 KiB
Python
Executable File
import sys
|
||
import os
|
||
import asyncio
|
||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
||
seek = os.path.dirname(os.path.dirname(os.path.dirname(current_directory)))
|
||
sys.path.insert(0, seek)
|
||
|
||
from volcengine.viking_knowledgebase import VikingKnowledgeBaseService, Collection, Doc, Point
|
||
from volcengine.viking_knowledgebase.common import Field, FieldType, IndexType, EmbddingModelType
|
||
|
||
collection_name = ""
|
||
|
||
|
||
viking_knowledgebase_service = VikingKnowledgeBaseService()
|
||
points = viking_knowledgebase_service.search_collection(collection_name=collection_name,query="", rerank_switch=True, retrieve_count=20)
|
||
for point in points:
|
||
print(point.content)
|
||
print(point.chunk_id)
|
||
print(point.point_id)
|
||
print(point.doc_id)
|
||
print(point.project)
|
||
print("=======")
|
||
|
||
#假定用户已经在知识库中创建了一个collection,并上传了文档,A公司_2022年财报.pdf,A公司_2023年财报.pdf
|
||
m_messages = [{
|
||
"role": "system",
|
||
"content": """ system pe """
|
||
},
|
||
{
|
||
"role": "user",
|
||
"content": "22年A公司财报中提到的风险,在23年应对的如何" # 用户提问
|
||
}
|
||
]
|
||
# chat_completion without streaming
|
||
print("#"*10,"chat_completion without streaming","#"*10)
|
||
res = viking_knowledgebase_service.chat_completion(model="Doubao-pro-32k", messages=m_messages, max_tokens=4096,
|
||
temperature=0.1)
|
||
data = res["generated_answer"]
|
||
token_usage = res["usage"]
|
||
print(data)
|
||
print(token_usage)
|
||
# chat_completion with streaming
|
||
print("#"*10,"chat_completion with streaming","#"*10)
|
||
res = viking_knowledgebase_service.chat_completion(model="Doubao-pro-32k", messages=m_messages, max_tokens=4096,
|
||
temperature=0.1,stream=True)
|
||
for data in res:
|
||
print(data,end="",flush=True)
|
||
print("")
|
||
print(res.token_usage())
|
||
|