This commit is contained in:
2025-08-27 22:22:18 +08:00
parent f6c7c65d6c
commit e5362b80e2
32 changed files with 914 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
from llama_index.core import SimpleDirectoryReader
from llama_index.readers.dashscope.base import DashScopeParse
from llama_index.readers.dashscope.utils import ResultType
from llama_index.indices.managed.dashscope import DashScopeCloudIndex
def read_parse_upload_local_documents(dir, num_workers=1):
"""读取、解析、上传本地文件到百炼数据管理平台。
Args:
dir (str): 本地文件存储的路径。
num_workers (int, optional): 执行的并发数。
Returns:
已上传到云端的文件列表
"""
parse = DashScopeParse(result_type=ResultType.DASHSCOPE_DOCMIND)
file_extractor = {'.txt': parse, '.docx': parse, ".pdf": parse} # 设置需要读取解析的文件格式,请根据实际需求调整
documents = SimpleDirectoryReader(input_dir=dir, file_extractor=file_extractor).load_data(num_workers=num_workers)
return documents
if __name__ == '__main__':
dir = "./docs/" # 本例中业务相关文件存储在当前路径下的docs文件夹请根据实际情况调整。
documents = read_parse_upload_local_documents(dir)
cloud_index_name = "my_first_index" # 设置云端知识库索引名称
index = DashScopeCloudIndex.from_documents(documents, cloud_index_name, verbose=True) # 创建云端知识库索引

View File

@@ -0,0 +1,11 @@
百炼系列智能音箱产品介绍
踏入智能家居的核心地带,百炼智能音箱系列以其创新设计与智能交互技术,重新定义家庭生活的每一个角落。
百炼EchoSphere S1 —— 智慧家居的指挥官: 设计简约而不失时尚采用360度全向发声技术配备高端音频单元提供深沉低音与清澈高音的完美平衡。支持Wi-Fi与蓝牙5.0双连接轻松接入您的智能家居生态。内置智能语音助手只需一声令下即可控制家中电器、查询天气、播放音乐等。长达20小时的续航能力让音乐与便利伴您一整天。参考售价999 - 1299。
通义VoiceBox V7 —— 生活小秘书: 采用8个远场麦克风阵列即使在嘈杂环境中也能精准识别您的指令。拥有7英寸触摸屏不仅能听更能看查看菜谱、视频通话、浏览新闻一应俱全。4GB内存与64GB存储空间存储您喜爱的歌曲与节目。内置电池支持8小时连续使用从厨房到卧室随心移动。参考售价1499 - 1799。
星尘SoundWave SW Pro —— 音乐发烧友的选择: 采用HiFi级音频解码芯片支持aptX HD与LDAC高品质音频传输还原录音室级别的音质。独特的声学结构设计带来震撼的3D环绕声效果。12GB RAM与256GB存储存储海量音乐资源。AI音效调节根据环境自动优化音质。内置电池续航高达12小时户外派对也能尽兴。参考售价2999 - 3499。
百炼SmartHalo H1 —— 家庭互动的中心: 独特的环形灯效设计不仅是一款音箱更是家居装饰的艺术品。支持多种语音助手兼容iOS与Android系统家庭成员均可个性化设置。内置丰富的儿童教育资源成为孩子成长的好伙伴。5000mAh电池满足全家人的日常使用需求。参考售价1299 - 1599。
百炼MiniBoom MB Plus —— 便携式智能乐趣: 极致小巧仅手掌大小却拥有令人惊喜的音质表现。IP67防水防尘设计无论户外探险还是浴室放松都能陪伴左右。支持语音控制与触控操作内置电池续航达10小时。无线串联功能轻松构建环绕立体声场。参考售价299 - 399。
每一款百炼智能音箱都是对智能生活美学的诠释,旨在为您的家庭带来前所未有的便捷与愉悦。选择百炼,让智能生活的声音更加悦耳动听。

View File

@@ -0,0 +1,57 @@
from llama_index.core import Settings
from llama_index.llms.dashscope import DashScope
from llama_index.indices.managed.dashscope import DashScopeCloudIndex
from llama_index.core.postprocessor import SimilarityPostprocessor
from llama_index.postprocessor.dashscope_rerank import DashScopeRerank
'''
本例中构建检索引擎时,需要手动设置下列参数,请根据实际效果调整。
'''
Settings.llm = DashScope(model_name="qwen-max") # 设置检索引擎生成回答时调用的大模型。
similarity_top_k = 5 # 检索引擎找到的相似度最高的结果数
similarity_cutoff = 0.4 # 过滤检索结果时使用的最低相似度阈值
top_n = 1 # 进行重排后返回语义上相关度最高的结果数
'''
本例中构建RAG应用时设置如下问答模板请根据实际需求调整。
'''
init_chat = "\n您好我是AI助手可以回答关于百炼系列产品的提问。有什么可以帮您的请输入问题退出请输入'q'\n> "
resp_with_no_answer = "很抱歉,知识库未提供相关信息。" + "\n"
prompt_template = "回答如下问题: {0}\n如果根据提供的信息无法回答,请返回:{1}"
'''
格式化输出。
'''
def prettify_rag(resp):
output = ""
output += "\n回答:{0}\n".format(resp.response)
for j in range(len(resp.source_nodes)):
output += "\n产品知识库中的相关文本:\n{0}\n".format(resp.source_nodes[j].text)
return output
'''
基于云端知识库的向量索引,构建检索引擎,能够接收终端用户的提问,从云端知识库中检索相关的文本片段,再将提问和检索结果合并后输入到大模型,并生成回答。
RAG应用提供与终端用户的交互界面如果无法检索到相关的文本片段或根据检索到的文本片段无法回答终端用户的提问则返回适当的错误信息。
'''
if __name__ == '__main__':
index = DashScopeCloudIndex("my_first_index") # 读取百炼平台上已创建的知识库索引
query_engine = index.as_query_engine( # 构建检索引擎
similarity_top_k=similarity_top_k,
node_postprocessors=[ # 默认检索结果可能不满足需求本例中通过加入node_postprocessors对检索结果进行后处理。
SimilarityPostprocessor(similarity_cutoff=similarity_cutoff), # 过滤不满足最低相似度阈值的检索结果。
DashScopeRerank(top_n=top_n, model="gte-rerank") # 对检索结果进行重排,返回语义上相关度最高的结果。
],
response_mode="tree_summarize"
)
while True:
user_prompt = input(init_chat)
if user_prompt in ['q','Q']:
break
resp = query_engine.query(prompt_template.format(user_prompt, resp_with_no_answer))
if len(resp.source_nodes) == 0:
output = resp_with_no_answer # 如果未找到相关上下文信息,则返回适当的报错信息。
else:
output = prettify_rag(resp)
print(output)

View File

@@ -0,0 +1,12 @@
llama-index==0.10.65
dashscope==1.20.4
llama-index-embeddings-dashscope==0.1.4
llama-index-indices-managed-llama-cloud==0.2.7
llama-index-llms-dashscope==0.1.2
llama-index-node-parser-dashscope==0.1.2
llama-index-postprocessor-dashscope-rerank==0.1.4
llama-index-readers-dashscope==0.1.2
llama-index-readers-file==0.1.33
llama-index-readers-llama-parse==0.1.6
llama-parse==0.4.9
llama-index-indices-managed-dashscope==0.1.2