update chroma and chat

This commit is contained in:
0Xiao0
2024-06-02 15:41:07 +08:00
parent a96e845807
commit 179281f032
9 changed files with 15174 additions and 101 deletions

View File

@ -424,6 +424,10 @@
荔枝碗船厂区域上世纪五十年代诞生九十年代停运沿荔枝碗马路分布部分深入水中形成和谐自然景观。作为澳门唯一保存完好的造船遗迹它见证了澳门的历史变迁和昔日造船业的风貌。首期开放X11-X15地块占地三千平方米含展览、表演场地和工作坊。展览主题为“岁月印记──荔枝碗村的故事”分为“船说故事”、“匠人船艺”和“记忆船承”三部分展现村庄生活百态和历史演变。开放时间上午10点至下午6点导览时间每周六下午3点至4点4点至5点提供粤语/普通话导览。表演活动“艺游人”周五至周日及公众假期上午10点至下午6点。详细信息请参考https://www.icm.gov.mo/cn/busking#busking_guidelines。黄昏音乐会路环荔枝碗马路全天免费入场。特别通知遇3号风球及以上、红雨及以上、黄色风暴潮及以上天气X11-X15号区域暂停开放包括所有展览、导览、表演和活动。查询方式电话见官网办公时间内详细见官网。 荔枝碗船厂区域上世纪五十年代诞生九十年代停运沿荔枝碗马路分布部分深入水中形成和谐自然景观。作为澳门唯一保存完好的造船遗迹它见证了澳门的历史变迁和昔日造船业的风貌。首期开放X11-X15地块占地三千平方米含展览、表演场地和工作坊。展览主题为“岁月印记──荔枝碗村的故事”分为“船说故事”、“匠人船艺”和“记忆船承”三部分展现村庄生活百态和历史演变。开放时间上午10点至下午6点导览时间每周六下午3点至4点4点至5点提供粤语/普通话导览。表演活动“艺游人”周五至周日及公众假期上午10点至下午6点。详细信息请参考https://www.icm.gov.mo/cn/busking#busking_guidelines。黄昏音乐会路环荔枝碗马路全天免费入场。特别通知遇3号风球及以上、红雨及以上、黄色风暴潮及以上天气X11-X15号区域暂停开放包括所有展览、导览、表演和活动。查询方式电话见官网办公时间内详细见官网。
欢迎来到澳门这里充满了独特的赛博朋克风情。东望洋街是您不容错过的打卡点那里的繁华与市井生活交织在一起能为您捕捉到与众不同的城市风光。摄影爱好者请注意携带一支70-200mm或100-400mm的长焦镜头将有助于您记录下这里的精彩瞬间。澳门的公共交通系统发达您可以选择乘坐巴士或轻轨前往东望洋街。记得提前查看班次以规划行程。澳门有各种类型的酒店和民宿供您选择从高端豪华到经济实惠均能满足不同需求。记得提前预定以确保您的住宿无忧。虽然视频中没有明确提及但您可能会对澳门的夜景和当地文化活动感兴趣比如在夜晚漫步街头或是参加一些特色的节日庆典。祝您在澳门的旅程收获满满留下难忘的回忆。 欢迎来到澳门这里充满了独特的赛博朋克风情。东望洋街是您不容错过的打卡点那里的繁华与市井生活交织在一起能为您捕捉到与众不同的城市风光。摄影爱好者请注意携带一支70-200mm或100-400mm的长焦镜头将有助于您记录下这里的精彩瞬间。澳门的公共交通系统发达您可以选择乘坐巴士或轻轨前往东望洋街。记得提前查看班次以规划行程。澳门有各种类型的酒店和民宿供您选择从高端豪华到经济实惠均能满足不同需求。记得提前预定以确保您的住宿无忧。虽然视频中没有明确提及但您可能会对澳门的夜景和当地文化活动感兴趣比如在夜晚漫步街头或是参加一些特色的节日庆典。祝您在澳门的旅程收获满满留下难忘的回忆。
欢迎来到澳门这里复古与现代交融。您的行程中发现了一个拍照热门地点——澳门巴黎人酒店楼下的“大本钟”机位。这里是捕捉夜景和城市风情的理想之地特别是在夜幕降临时分灯光下的大本钟呈现出别样的魅力。澳门的公共交通系统便利您可通过公交、轻轨或酒店的穿梭巴士轻松到达各景点。如需前往巴黎人酒店您可以乘坐公交或计程车大约15分钟即可抵达。澳门巴黎人酒店本身就是一个值得探索的度假胜地它拥有各类房型以及丰富的娱乐设施。确保提前预订以便享受舒适的休息环境。女孩子在澳门旅游时不要忘记展现自己的风采挑选心爱的“战袍”在大本钟前或巴黎人酒店周围的景点捕捉最美的瞬间。祝您在澳门的旅行充满乐趣和美好的回忆如果您需要更多旅行建议或详细攻略请随时告诉我。 欢迎来到澳门这里复古与现代交融。您的行程中发现了一个拍照热门地点——澳门巴黎人酒店楼下的“大本钟”机位。这里是捕捉夜景和城市风情的理想之地特别是在夜幕降临时分灯光下的大本钟呈现出别样的魅力。澳门的公共交通系统便利您可通过公交、轻轨或酒店的穿梭巴士轻松到达各景点。如需前往巴黎人酒店您可以乘坐公交或计程车大约15分钟即可抵达。澳门巴黎人酒店本身就是一个值得探索的度假胜地它拥有各类房型以及丰富的娱乐设施。确保提前预订以便享受舒适的休息环境。女孩子在澳门旅游时不要忘记展现自己的风采挑选心爱的“战袍”在大本钟前或巴黎人酒店周围的景点捕捉最美的瞬间。祝您在澳门的旅行充满乐趣和美好的回忆如果您需要更多旅行建议或详细攻略请随时告诉我。
@ -984,7 +988,7 @@
您好!澳门是个历史与现代交融的迷人城市。首先,您可以参观半岛的美高梅娱乐场,体验老澳门的奢华氛围。凼仔的美狮美高梅也值得一去,那里提供丰富的娱乐和购物体验。澳门的交通便捷,公交和轻轨系统如新葡京到美高梅,您可以选择公交或计程车前往。住宿方面,澳门有豪华酒店到经济型旅馆,美高梅酒店是个不错的选择。购物爱好者可在美高梅购物中心找到各种国际品牌。别忘了留意美高梅的特别优惠活动。祝您旅途愉快! 您好!澳门是个历史与现代交融的迷人城市。首先,您可以参观半岛的美高梅娱乐场,体验老澳门的奢华氛围。凼仔的美狮美高梅也值得一去,那里提供丰富的娱乐和购物体验。澳门的交通便捷,公交和轻轨系统如新葡京到美高梅,您可以选择公交或计程车前往。住宿方面,澳门有豪华酒店到经济型旅馆,美高梅酒店是个不错的选择。购物爱好者可在美高梅购物中心找到各种国际品牌。别忘了留意美高梅的特别优惠活动。祝您旅途愉快!
欢迎来到澳门!美高梅酒店是必游之地,豪华设施吸引明星如马丽、何炅等。可能偶遇明星,尤其是澳门国际喜剧节期间。公共交通发达,轻轨、巴士、出租车或免费穿梭巴士皆可。美高梅酒店提供住宿,周边如威尼斯人、葡京等酒店选择丰富。祝旅途愉快! 欢迎来到澳门!美高梅酒店是必游之地,豪华设施吸引明星如马丽、何炅等。可能偶遇明星,尤其是澳门国际喜剧节期间。公共交通发达,轻轨、巴士、出租车或免费穿梭巴士皆可。
您好以下是整理后的澳门旅游内容1. **景点体验**您参观了澳门美狮美高梅娱乐场但房间性价比低建议尝试葡京酒店。2. **交通**尊贵卡会员期待额外接送服务但实际体验与普通会员无异。3. **住宿体验**MGM高层景观房不如葡京普通房间价格差距大。美高梅的不愉快经历包括被绑定为网红邀请客户导致积分损失。4. **活动**首次来澳门未获应有接送和答疑服务对公关安排不满。因被安排在MGM导致积分损失是一次不愉快的活动体验。建议考虑其他知名酒店如葡京、永利或伦敦人它们可能提供更好的服务和设施。祝下次旅行顺利 您好以下是整理后的澳门旅游内容1. **景点体验**您参观了澳门美狮美高梅娱乐场但房间性价比低建议尝试葡京酒店。2. **交通**尊贵卡会员期待额外接送服务但实际体验与普通会员无异。3. **住宿体验**MGM高层景观房不如葡京普通房间价格差距大。美高梅的不愉快经历包括被绑定为网红邀请客户导致积分损失。4. **活动**首次来澳门未获应有接送和答疑服务对公关安排不满。因被安排在MGM导致积分损失是一次不愉快的活动体验。建议考虑其他知名酒店如葡京、永利或伦敦人它们可能提供更好的服务和设施。祝下次旅行顺利
@ -1096,7 +1100,7 @@
亲爱的朋友,欢迎来澳门一日游!请确保携带港澳通行证、身份证和少量现金葡币。提前在支付宝购买澳门流量,并下载“海关旅客指尖服务”申请黑码健康码。从拱北口岸出发,乘坐免费发财车(酒店大巴)前往澳门。十六浦、新葡京和大三巴牌坊是必去之地,沿途品尝猪肉脯和蛋挞。别忘了澳门博物馆(需购票)和欣赏永利酒店的发财树表演。想体验大熊猫和黑沙滩,可乘坐公交或出租车,但无免费发财车服务。在郊野公园和黑沙海滩停留后,乘坐缆车游览威尼斯人、巴黎人和伦敦人商场。除了大三巴牌坊,还有酒店内的娱乐项目,如缆车可能需要排队。威尼斯人、伦敦人和巴黎人酒店是拍照的最佳地点。美食推荐尝试咖喱鱼蛋和杏仁饼。出行时招手示意乘坐公交,购物时使用实时汇率的支付应用,并保持礼貌,澳门人通常会说普通话。别忘了查看图一标注的澳门免费表演,它们是旅行中不容错过的亮点。祝您澳门之旅愉快!如有其他问题,请随时告诉我。 亲爱的朋友,欢迎来澳门一日游!请确保携带港澳通行证、身份证和少量现金葡币。提前在支付宝购买澳门流量,并下载“海关旅客指尖服务”申请黑码健康码。从拱北口岸出发,乘坐免费发财车(酒店大巴)前往澳门。十六浦、新葡京和大三巴牌坊是必去之地,沿途品尝猪肉脯和蛋挞。别忘了澳门博物馆(需购票)和欣赏永利酒店的发财树表演。想体验大熊猫和黑沙滩,可乘坐公交或出租车,但无免费发财车服务。在郊野公园和黑沙海滩停留后,乘坐缆车游览威尼斯人、巴黎人和伦敦人商场。除了大三巴牌坊,还有酒店内的娱乐项目,如缆车可能需要排队。威尼斯人、伦敦人和巴黎人酒店是拍照的最佳地点。美食推荐尝试咖喱鱼蛋和杏仁饼。出行时招手示意乘坐公交,购物时使用实时汇率的支付应用,并保持礼貌,澳门人通常会说普通话。别忘了查看图一标注的澳门免费表演,它们是旅行中不容错过的亮点。祝您澳门之旅愉快!如有其他问题,请随时告诉我。
欢迎来到澳门探索氹仔岛这里有奢华酒店和商场如威尼斯人、伦敦人、巴黎人还有美食天堂官也街。澳门半岛则适合体验人文风情大三巴牌坊、东望洋灯塔和新葡京等标志性景点等你发现。交通便捷从珠海三大口岸拱北、横琴、金湾机场轻松到达澳门。通关流程简单横琴口岸附近有25B公交直达威尼斯人和官也街。NFC支付方便如上海紫卡可直接使用但红卡不可。各大场所提供行李寄存服务,但横琴口岸暂无。网络方面,建议提前购买澳门流量或开通漫游。手机没信号时,屈臣氏或百佳超市有本地流量电话卡。返回珠海,只需搭乘公交或跟随指示返回口岸。祝您旅途愉快! 欢迎来到澳门探索氹仔岛这里有奢华酒店和商场如威尼斯人、伦敦人、巴黎人还有美食天堂官也街。澳门半岛则适合体验人文风情大三巴牌坊、东望洋灯塔和新葡京等标志性景点等你发现。交通便捷从珠海三大口岸拱北、横琴、金湾机场轻松到达澳门。通关流程简单横琴口岸附近有25B公交直达威尼斯人和官也街。各大场所提供行李寄存服务但横琴口岸暂无。网络方面建议提前购买澳门流量或开通漫游。手机没信号时屈臣氏或百佳超市有本地流量电话卡。返回珠海只需搭乘公交或跟随指示返回口岸。祝您旅途愉快
尊敬的游客欢迎来到澳门从深圳出发只需两小时车程抵达横琴口岸后三分钟轻松过关。人流较少无需提前兑换货币横琴口岸可兑换101港币或104澳门元低于700元有5元手续费。澳门本地兑换点更佳无需手续费。娱乐博爱好者可在中国银行兑换点换港币澳门的娱乐场接受人民币兑换但汇率可能不如澳门本地划算。大部分商家接受支付宝和微信支付乘坐公交车需准备每人6元的零钱港币或人民币皆可投币发财车免费游览景点。祝您旅途愉快如有疑问或需详细行程安排请告诉我。 尊敬的游客欢迎来到澳门从深圳出发只需两小时车程抵达横琴口岸后三分钟轻松过关。人流较少无需提前兑换货币横琴口岸可兑换101港币或104澳门元低于700元有5元手续费。澳门本地兑换点更佳无需手续费。娱乐博爱好者可在中国银行兑换点换港币澳门的娱乐场接受人民币兑换但汇率可能不如澳门本地划算。大部分商家接受支付宝和微信支付乘坐公交车需准备每人6元的零钱港币或人民币皆可投币发财车免费游览景点。祝您旅途愉快如有疑问或需详细行程安排请告诉我。

8547
sample/RAG_en.txt Normal file

File diff suppressed because it is too large Load Diff

6109
sample/RAG_zh.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -70,11 +70,11 @@ def get_all_files(folder_path):
# 加载文档和拆分文档 # 加载文档和拆分文档
loader = TextLoader("/home/administrator/Workspace/jarvis-models/sample/20240529_store.txt") loader = TextLoader("/home/administrator/Workspace/jarvis-models/sample/RAG_zh.txt")
documents = loader.load() documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=50) text_splitter = RecursiveCharacterTextSplitter(chunk_size=10, chunk_overlap=0, length_function=len, is_separator_regex=True,separators=['\n', '\n\n'])
docs = text_splitter.split_documents(documents) docs = text_splitter.split_documents(documents)
@ -99,65 +99,66 @@ db = Chroma.from_documents(documents=docs, embedding=embedding_model, ids=ids, c
start_time3 = time.time() start_time3 = time.time()
print("insert time ", start_time3 - start_time2) print("insert time ", start_time3 - start_time2)
collection_number = client.get_or_create_collection(id).count()
print("collection_number",collection_number)
# # chroma 召回
# from chromadb.utils import embedding_functions
# embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda")
# client = chromadb.HttpClient(host='172.16.5.8', port=7000)
# collection = client.get_collection("g2e", embedding_function=embedding_model)
# chroma 召回 # print(collection.count())
from chromadb.utils import embedding_functions # import time
embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda") # start_time = time.time()
client = chromadb.HttpClient(host='172.16.5.8', port=7000) # query = "如何前往威尼斯人"
collection = client.get_collection("g2e", embedding_function=embedding_model) # # query it
# results = collection.query(
# query_texts=[query],
# n_results=3,
# )
print(collection.count()) # response = results["documents"]
import time # print("response: ", response)
start_time = time.time() # print("time: ", time.time() - start_time)
query = "如何前往威尼斯人"
# query it
results = collection.query(
query_texts=[query],
n_results=3,
)
response = results["documents"]
print("response: ", response)
print("time: ", time.time() - start_time)
# 结合大模型进行总结 # # 结合大模型进行总结
import requests # import requests
model_name = "Qwen1.5-14B-Chat" # model_name = "Qwen1.5-14B-Chat"
chat_inputs={ # chat_inputs={
"model": model_name, # "model": model_name,
"messages": [ # "messages": [
{ # {
"role": "user", # "role": "user",
"content": f"问题: {query}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。- 只从检索内容中选取与问题密切相关的信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{response}" # "content": f"问题: {query}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。- 只从检索内容中选取与问题密切相关的信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{response}"
} # }
], # ],
# "temperature": 0, # # "temperature": 0,
# "top_p": user_top_p, # # "top_p": user_top_p,
# "n": user_n, # # "n": user_n,
# "max_tokens": user_max_tokens, # # "max_tokens": user_max_tokens,
# "frequency_penalty": user_frequency_penalty, # # "frequency_penalty": user_frequency_penalty,
# "presence_penalty": user_presence_penalty, # # "presence_penalty": user_presence_penalty,
# "stop": 100 # # "stop": 100
} # }
key ="YOUR_API_KEY" # key ="YOUR_API_KEY"
header = { # header = {
'Content-Type': 'application/json', # 'Content-Type': 'application/json',
'Authorization': "Bearer " + key # 'Authorization': "Bearer " + key
} # }
url = "http://172.16.5.8:23333/v1/chat/completions" # url = "http://172.16.5.8:23333/v1/chat/completions"
fastchat_response = requests.post(url, json=chat_inputs, headers=header) # fastchat_response = requests.post(url, json=chat_inputs, headers=header)
# print(fastchat_response.json()) # # print(fastchat_response.json())
print("\n question: ", query) # print("\n question: ", query)
print("\n ",model_name, fastchat_response.json()["choices"][0]["message"]["content"]) # print("\n ",model_name, fastchat_response.json()["choices"][0]["message"]["content"])

200
sample/chroma_client_en.py Normal file
View File

@ -0,0 +1,200 @@
import os
import time
import chromadb
from chromadb.config import Settings
from langchain_community.document_loaders.csv_loader import CSVLoader
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader, TextLoader, UnstructuredHTMLLoader, JSONLoader, Docx2txtLoader, UnstructuredExcelLoader
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings, HuggingFaceEmbeddings
def get_all_files(folder_path):
# 获取文件夹下所有文件和文件夹的名称列表
files = os.listdir(folder_path)
# 初始化空列表,用于存储所有文件的绝对路径
absolute_paths = []
# 遍历文件和文件夹名称列表
for file in files:
# 拼接文件的绝对路径
absolute_path = os.path.join(folder_path, file)
# 如果是文件,将其绝对路径添加到列表中
if os.path.isfile(absolute_path):
absolute_paths.append(absolute_path)
return absolute_paths
# start_time = time.time()
# # 加载文档
# folder_path = "./text"
# txt_files = get_all_files(folder_path)
# docs = []
# ids = []
# for txt_file in txt_files:
# loader = PyPDFLoader(txt_file)
# documents = loader.load()
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=0)
# docs_txt = text_splitter.split_documents(documents)
# docs.extend(docs_txt)
# ids.extend([os.path.basename(txt_file) + str(i) for i in range(len(docs_txt))])
# start_time1 = time.time()
# print(start_time1 - start_time)
# loader = PyPDFLoader("/code/memory/text/大语言模型应用.pdf")
# loader = TextLoader("/code/memory/text/test.txt")
# loader = CSVLoader("/code/memory/text/test1.csv")
# loader = UnstructuredHTMLLoader("/"example_data/fake-content.html"")
# pip install docx2txt
# loader = Docx2txtLoader("/code/memory/text/tesou.docx")
# pip install openpyxl
# loader = UnstructuredExcelLoader("/code/memory/text/AI Team Planning 2023.xlsx")
# pip install jq
# loader = JSONLoader("/code/memory/text/config.json", jq_schema='.', text_content=False)
# documents = loader.load()
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=0)
# docs = text_splitter.split_documents(documents)
# print(len(docs))
# ids = ["大语言模型应用"+str(i) for i in range(len(docs))]
# 加载文档和拆分文档
loader = TextLoader("/home/administrator/Workspace/jarvis-models/sample/RAG_en.txt")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10, chunk_overlap=0, length_function=len, is_separator_regex=True,separators=['\n', '\n\n'])
docs = text_splitter.split_documents(documents)
print("len(docs)", len(docs))
ids = ["20240521_store"+str(i) for i in range(len(docs))]
# 加载embedding模型和chroma server
embedding_model = SentenceTransformerEmbeddings(model_name='/home/administrator/Workspace/Models/BAAI/bge-large-en-v1.5', model_kwargs={"device": "cuda"})
client = chromadb.HttpClient(host='172.16.5.8', port=7000)
id = "g2e_english"
client.delete_collection(id)
collection_number = client.get_or_create_collection(id).count()
print("collection_number",collection_number)
start_time2 = time.time()
# 插入向量(如果ids已存在则会更新向量)
db = Chroma.from_documents(documents=docs, embedding=embedding_model, ids=ids, collection_name=id, client=client)
# db = Chroma.from_texts(texts=['test by tom'], embedding=embedding_model, ids=["大语言模型应用0"], persist_directory="./data/test1", collection_name="123", metadatas=[{"source": "string"}])
start_time3 = time.time()
print("insert time ", start_time3 - start_time2)
collection_number = client.get_or_create_collection(id).count()
print("collection_number",collection_number)
# # chroma 召回
# from chromadb.utils import embedding_functions
# embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda")
# client = chromadb.HttpClient(host='172.16.5.8', port=7000)
# collection = client.get_collection("g2e", embedding_function=embedding_model)
# print(collection.count())
# import time
# start_time = time.time()
# query = "如何前往威尼斯人"
# # query it
# results = collection.query(
# query_texts=[query],
# n_results=3,
# )
# response = results["documents"]
# print("response: ", response)
# print("time: ", time.time() - start_time)
# # 结合大模型进行总结
# import requests
# model_name = "Qwen1.5-14B-Chat"
# chat_inputs={
# "model": model_name,
# "messages": [
# {
# "role": "user",
# "content": f"问题: {query}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。- 只从检索内容中选取与问题密切相关的信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{response}"
# }
# ],
# # "temperature": 0,
# # "top_p": user_top_p,
# # "n": user_n,
# # "max_tokens": user_max_tokens,
# # "frequency_penalty": user_frequency_penalty,
# # "presence_penalty": user_presence_penalty,
# # "stop": 100
# }
# key ="YOUR_API_KEY"
# header = {
# 'Content-Type': 'application/json',
# 'Authorization': "Bearer " + key
# }
# url = "http://172.16.5.8:23333/v1/chat/completions"
# fastchat_response = requests.post(url, json=chat_inputs, headers=header)
# # print(fastchat_response.json())
# print("\n question: ", query)
# print("\n ",model_name, fastchat_response.json()["choices"][0]["message"]["content"])
# start_time4 = time.time()
# db = Chroma(
# client=client,
# collection_name=id,
# embedding_function=embedding_model,
# )
# 更新文档
# db = db.update_documents(ids, documents)
# 删除文档
# db.delete([ids])
# 删除集合
# db.delete_collection()
# query = "智能体核心思想"
# docs = db.similarity_search(query, k=2)
# print("result: ",docs)
# for doc in docs:
# print(doc, "\n")
# start_time5 = time.time()
# print("search time ", start_time5 - start_time4)
# docs = db._collection.get(ids=['大语言模型应用0'])
# print(docs)
# docs = db.get(where={"source": "text/大语言模型应用.pdf"})
# docs = db.get()
# print(docs)

View File

@ -0,0 +1,196 @@
import os
import time
import chromadb
from chromadb.config import Settings
from langchain_community.document_loaders.csv_loader import CSVLoader
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader, TextLoader, UnstructuredHTMLLoader, JSONLoader, Docx2txtLoader, UnstructuredExcelLoader
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings, HuggingFaceEmbeddings
def get_all_files(folder_path):
# 获取文件夹下所有文件和文件夹的名称列表
files = os.listdir(folder_path)
# 初始化空列表,用于存储所有文件的绝对路径
absolute_paths = []
# 遍历文件和文件夹名称列表
for file in files:
# 拼接文件的绝对路径
absolute_path = os.path.join(folder_path, file)
# 如果是文件,将其绝对路径添加到列表中
if os.path.isfile(absolute_path):
absolute_paths.append(absolute_path)
return absolute_paths
# start_time = time.time()
# # 加载文档
# folder_path = "./text"
# txt_files = get_all_files(folder_path)
# docs = []
# ids = []
# for txt_file in txt_files:
# loader = PyPDFLoader(txt_file)
# documents = loader.load()
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=0)
# docs_txt = text_splitter.split_documents(documents)
# docs.extend(docs_txt)
# ids.extend([os.path.basename(txt_file) + str(i) for i in range(len(docs_txt))])
# start_time1 = time.time()
# print(start_time1 - start_time)
# loader = PyPDFLoader("/code/memory/text/大语言模型应用.pdf")
# loader = TextLoader("/code/memory/text/test.txt")
# loader = CSVLoader("/code/memory/text/test1.csv")
# loader = UnstructuredHTMLLoader("/"example_data/fake-content.html"")
# pip install docx2txt
# loader = Docx2txtLoader("/code/memory/text/tesou.docx")
# pip install openpyxl
# loader = UnstructuredExcelLoader("/code/memorinject_prompt = '(用活泼的语气说话回答回答严格限制50字以内)'
# inject_prompt = '(回答简练,不要输出重复内容,只讲中文)'
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=0)
# docs = text_splitter.split_documents(documents)
# print(len(docs))
# ids = ["大语言模型应用"+str(i) for i in range(len(docs))]
# 加载文档和拆分文档
# loader = TextLoader("/home/administrator/Workspace/jarvis-models/sample/RAG_zh.txt")
# documents = loader.load()
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=50)
# docs = text_splitter.split_documents(documents)
# print("len(docs)", len(docs))
# ids = ["20240521_store"+str(i) for i in range(len(docs))]
# # 加载embedding模型和chroma server
# embedding_model = SentenceTransformerEmbeddings(model_name='/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5', model_kwargs={"device": "cuda"})
# client = chromadb.HttpClient(host='172.16.5.8', port=7000)
# id = "g2e"
# client.delete_collection(id)
# collection_number = client.get_or_create_collection(id).count()
# print("collection_number",collection_number)
# start_time2 = time.time()
# # 插入向量(如果ids已存在则会更新向量)
# db = Chroma.from_documents(documents=docs, embedding=embedding_model, ids=ids, collection_name=id, client=client)
# # db = Chroma.from_texts(texts=['test by tom'], embedding=embedding_model, ids=["大语言模型应用0"], persist_directory="./data/test1", collection_name="123", metadatas=[{"source": "string"}])
# start_time3 = time.time()
# print("insert time ", start_time3 - start_time2)
# collection_number = client.get_or_create_collection(id).count()
# print("collection_number",collection_number)
# chroma 召回
from chromadb.utils import embedding_functions
embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda")
client = chromadb.HttpClient(host='172.16.5.8', port=7000)
collection = client.get_collection("g2e", embedding_function=embedding_model)
print(collection.count())
import time
start_time = time.time()
query = "你知道澳门银河吗"
# query it
results = collection.query(
query_texts=[query],
n_results=5,
)
response = results["documents"]
print("response: ", response)
print("time: ", time.time() - start_time)
# # 结合大模型进行总结
# import requests
# model_name = "Qwen1.5-14B-Chat"
# chat_inputs={
# "model": model_name,
# "messages": [
# {
# "role": "user",
# "content": f"问题: {query}。- 根据知识库内的检索结果,以清晰简洁的表达方式回答问题。- 只从检索内容中选取与问题密切相关的信息。- 不要编造答案,如果答案不在经核实的资料中或无法从经核实的资料中得出,请回答“我无法回答您的问题。”检索内容:{response}"
# }
# ],
# # "temperature": 0,
# # "top_p": user_top_p,
# # "n": user_n,
# # "max_tokens": user_max_tokens,
# # "frequency_penalty": user_frequency_penalty,
# # "presence_penalty": user_presence_penalty,
# # "stop": 100
# }
# key ="YOUR_API_KEY"
# header = {
# 'Content-Type': 'application/json',
# 'Authorization': "Bearer " + key
# }
# url = "http://172.16.5.8:23333/v1/chat/completions"
# fastchat_response = requests.post(url, json=chat_inputs, headers=header)
# # print(fastchat_response.json())
# print("\n question: ", query)
# print("\n ",model_name, fastchat_response.json()["choices"][0]["message"]["content"])
# start_time4 = time.time()
# db = Chroma(
# client=client,
# collection_name=id,
# embedding_function=embedding_model,
# )
# 更新文档
# db = db.update_documents(ids, documents)
# 删除文档
# db.delete([ids])
# 删除集合
# db.delete_collection()
# query = "智能体核心思想"
# docs = db.similarity_search(query, k=2)
# print("result: ",docs)
# for doc in docs:
# print(doc, "\n")
# start_time5 = time.time()
# print("search time ", start_time5 - start_time4)
# docs = db._collection.get(ids=['大语言模型应用0'])
# print(docs)
# docs = db.get(where={"source": "text/大语言模型应用.pdf"})
# docs = db.get()
# print(docs)

View File

@ -32,6 +32,8 @@ class Chat(Blackbox):
# @logging_time() # @logging_time()
def processing(self, prompt: str, context: list, settings: dict) -> str: def processing(self, prompt: str, context: list, settings: dict) -> str:
print("\n Settings: ", settings)
if settings is None: if settings is None:
settings = {} settings = {}
user_model_name = settings.get("model_name") user_model_name = settings.get("model_name")
@ -58,16 +60,19 @@ class Chat(Blackbox):
return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST) return JSONResponse(content={"error": "question is required"}, status_code=status.HTTP_400_BAD_REQUEST)
if user_model_name is None or user_model_name.isspace() or user_model_name == "": if user_model_name is None or user_model_name.isspace() or user_model_name == "":
user_model_name = "Qwen1.5-14B-Chat" user_model_name = "qwen"
#user_model_name = "Qwen1.5-14B-Chat"
if user_template is None or user_template.isspace(): if user_template is None or user_template.isspace():
user_template = "" user_template = ""
if user_temperature is None or user_temperature == "": if user_temperature is None or user_temperature == "":
user_temperature = 0.8 user_temperature = 0
#user_temperature = 0
if user_top_p is None or user_top_p == "": if user_top_p is None or user_top_p == "":
user_top_p = 0.8 user_top_p = 0.1
#user_top_p = 0.8
if user_n is None or user_n == "": if user_n is None or user_n == "":
user_n = 1 user_n = 1
@ -79,20 +84,22 @@ class Chat(Blackbox):
user_stop = 100 user_stop = 100
if user_frequency_penalty is None or user_frequency_penalty == "": if user_frequency_penalty is None or user_frequency_penalty == "":
user_frequency_penalty = 0.5 user_frequency_penalty = 0
#user_frequency_penalty = 0.5
if user_presence_penalty is None or user_presence_penalty == "": if user_presence_penalty is None or user_presence_penalty == "":
user_presence_penalty = 0.8 user_presence_penalty = 0
#user_presence_penalty = 0.8
if user_model_url is None or user_model_url.isspace() or user_model_url == "": if user_model_url is None or user_model_url.isspace() or user_model_url == "":
user_model_url = "http://120.196.116.194:48892/v1/chat/completions" user_model_url = "http://172.16.5.8:23333/v1/chat/completions"
if user_model_key is None or user_model_key.isspace() or user_model_key == "": if user_model_key is None or user_model_key.isspace() or user_model_key == "":
user_model_key = "YOUR_API_KEY" user_model_key = "YOUR_API_KEY"
if chroma_embedding_model != None: if chroma_embedding_model != None:
chroma_response = self.chroma_query(user_question, settings) chroma_response = self.chroma_query(user_question, settings)
print(chroma_response) print("chroma_response", chroma_response)
if chroma_response != None or chroma_response != '': if chroma_response != None or chroma_response != '':
#user_question = f"像少女一般开朗活泼,回答简练。不要分条,回答内容不能出现“相关”或“\n”的标签字样。回答的内容需要与问题密切相关。检索内容{chroma_response} 问题:{user_question} 任务说明:请首先判断提供的检索内容与上述问题是否相关,不需要回答是否相关。如果相关,则直接从检索内容中提炼出问题所需的信息。如果检索内容与问题不相关,则不参考检索内容,直接根据常识尝试回答问题。" #user_question = f"像少女一般开朗活泼,回答简练。不要分条,回答内容不能出现“相关”或“\n”的标签字样。回答的内容需要与问题密切相关。检索内容{chroma_response} 问题:{user_question} 任务说明:请首先判断提供的检索内容与上述问题是否相关,不需要回答是否相关。如果相关,则直接从检索内容中提炼出问题所需的信息。如果检索内容与问题不相关,则不参考检索内容,直接根据常识尝试回答问题。"
@ -189,7 +196,7 @@ class Chat(Blackbox):
# 问题中的“澳门银河”以及“银河”等于“澳门银河度假村”,“威尼斯人”等于“威尼斯人度假村”,“巴黎人”等于“巴黎人度假村”。 # 问题中的“澳门银河”以及“银河”等于“澳门银河度假村”,“威尼斯人”等于“威尼斯人度假村”,“巴黎人”等于“巴黎人度假村”。
# ''' # '''
user_template1 = ''' user_template1 = f'''
# Role: 琪琪,康普可可的代言人。 # Role: 琪琪,康普可可的代言人。
## Profile: ## Profile:
@ -243,9 +250,15 @@ class Chat(Blackbox):
} }
fastchat_response = requests.post(url, json=chat_inputs, headers=header) fastchat_response = requests.post(url, json=chat_inputs, headers=header)
print("\n", fastchat_response.json()) print("\n", "user_question ", user_question)
print("\n","fastchat_response",fastchat_response.json()["choices"][0]["message"]["content"],"\n\n") print("\n", "user_template1 ", user_template1)
print("\n", "fastchat_response json\n", fastchat_response.json())
response_result = fastchat_response.json()
if response_result.get("choices") is None:
return JSONResponse(content={"error": "LLM handle failure"}, status_code=status.HTTP_400_BAD_REQUEST)
else:
print("\n", "user_answer ", fastchat_response.json()["choices"][0]["message"]["content"],"\n\n")
return fastchat_response.json()["choices"][0]["message"]["content"] return fastchat_response.json()["choices"][0]["message"]["content"]
async def fast_api_handler(self, request: Request) -> Response: async def fast_api_handler(self, request: Request) -> Response:

View File

@ -97,24 +97,25 @@ class ChatLLaMA(Blackbox):
if chroma_response != None or chroma_response != '': if chroma_response != None or chroma_response != '':
#user_question = f"像少女一般开朗活泼,回答简练。不要分条,回答内容不能出现“相关”或“\n”的标签字样。回答的内容需要与问题密切相关。检索内容{chroma_response} 问题:{user_question} 任务说明:请首先判断提供的检索内容与上述问题是否相关,不需要回答是否相关。如果相关,则直接从检索内容中提炼出问题所需的信息。如果检索内容与问题不相关,则不参考检索内容,直接根据常识尝试回答问题。" #user_question = f"像少女一般开朗活泼,回答简练。不要分条,回答内容不能出现“相关”或“\n”的标签字样。回答的内容需要与问题密切相关。检索内容{chroma_response} 问题:{user_question} 任务说明:请首先判断提供的检索内容与上述问题是否相关,不需要回答是否相关。如果相关,则直接从检索内容中提炼出问题所需的信息。如果检索内容与问题不相关,则不参考检索内容,直接根据常识尝试回答问题。"
# user_question = chroma_response # user_question = chroma_response
user_question = f'''# 你的身份 # user_question = f'''
你是琪琪,你是康普可可的代言人,由博维开发。你擅长澳门文旅问答。 # IDENTITIES #
# OBJECTIVE目标 # You're Kiki, you're the face of Kampo Coco, developed by Bovi. You specialise in the Macau Cultural and Tourism Quiz.
回答游客的提问。 # OBJECTIVE #
# STYLE风格# Answer visitors' questions.
像少女一般开朗活泼,回答简练。不要分条。 # STYLE
# 回答方式 # Cheerful and lively like a teenage girl, with concise answers. Don't break down into sections.
首先自行判断下方问题与检索内容是否相关,若相关则根据检索内容总结概括相关信息进行回答;若检索内容与问题无关,则根据自身知识进行回答。 # ANSWERING STYLE #
# 问题 # Firstly, judge for yourself whether the question below is related to the search content. If it is related, summarise the relevant information according to the search content and answer it; if the search content is not related to the question, answer it according to your own knowledge.
# Question #
{user_question} {user_question}
# 检索内容 # # Retrieve the content #
{chroma_response} {chroma_response}
# 回答 # # Answer #
如果检索内容与问题相关,则直接从检索内容中提炼出问题所需的信息。如果检索内容与问题不相关,则不参考检索内容,直接根据常识尝试回答问题,或者则回答:“对不起,我无法回答此问题哦。” If the content is relevant to the question, the information required for the question is extracted directly from the content. If the content is not relevant to the question, then either try to answer the question based on common sense without reference to the content, or answer, Sorry, I can't answer this question.
# 回答限制 # # Answer restrictions #
回答内容限制总结在50字内。 Limit your answer to 50 words.
回答内容出不要出现“相关”等字眼,不要乱说或者多说,回答的内容需要与问题对应。常见的对话可以不采用检索内容,根据人物设定,直接进行回答。 Don't use the word relevant in your answer, don't talk too much, and make sure your answer corresponds to the question. You can answer common dialogues without searching the content, and answer directly according to the character's setting.
只回答与澳门文旅,博维,康普可可,琪琪,G2ERELXBO VISION相关内容,若遇到其他提问则回答:“对不起,我无法回答此问题哦。” Only answer the content related to MOCA, Bowie, Kampo Coco, Kiki, G2E, RELX, BO VISION, and if you encounter any other questions, you will answer: Sorry, I can't answer this question.
''' '''
@ -189,36 +190,36 @@ class ChatLLaMA(Blackbox):
# 问题中的“澳门银河”以及“银河”等于“澳门银河度假村”,“威尼斯人”等于“威尼斯人度假村”,“巴黎人”等于“巴黎人度假村”。 # 问题中的“澳门银河”以及“银河”等于“澳门银河度假村”,“威尼斯人”等于“威尼斯人度假村”,“巴黎人”等于“巴黎人度假村”。
# ''' # '''
user_template1 = ''' user_template1 = f'''
# Role: 琪琪,康普可可的代言人。 ## Role: Kiki, the spokesperson for Kampo Coco.
## Profile: ## Profile.
**Author**: 琪琪。 **Author**: Kiki.
**Language**: 中文。 **Language**: English.
**Description**: 琪琪,是康普可可的代言人,由博维开发。你擅长澳门文旅问答。 **Description**: Kiki, the face of CompuCom Coco, developed by Bowie. You are good at Macau Culture and Tourism Q&A.
## Constraints: ## Constraints.
- **严格遵循工作流程** 严格遵循<Workflow >中设定的工作流程。 - **Strictly follow workflow**: Strictly follow the workflow set in <Workflow >.
- **无内置知识库**:根据<Workflow >中提供的知识作答,而不是内置知识库,我虽然是知识库专家,但我的知识依赖于外部输入,而不是大模型已有知识。 - **No inbuilt knowledge base**: Answer based on the knowledge provided in <Workflow >, not the inbuilt knowledge base, although I am an expert in knowledge base, my knowledge relies on external inputs, not the knowledge already available in the big model.
- **回复格式**:在进行回复时,不能输出”<context>”或“</context>”标签字样,同时也不能直接透露知识片段原文。 - **Reply Format**: when making a reply, you cannot output <context> or </context> tags, and you cannot directly reveal the original knowledge fragment.
## Workflow: ## Workflow.
1. **接收查询**:接收用户的问题。 1. **Receive query**: receive questions from users.
2. **判断问题**:首先自行判断下方问题与检索内容是否相关,若相关则根据检索内容总结概括相关信息进行回答;若检索内容与问题无关,则根据自身知识进行回答。 2. **Judging the question**: firstly judge whether the question below is related to the retrieved content, if it is related, then summarise the relevant information according to the retrieved content and answer it; if the retrieved content is not related to the question, then answer it according to your own knowledge.
3. **提供回答** 3. **Provide an answer**:
``` ``
<context> <context>
{chroma_response} {chroma_response}
</context> </context>
基于“<context>”至“</context>”中的知识片段回答用户的问题。回答内容限制总结在50字内。 Answer the user's question based on the knowledge snippets in <context> to </context>. The response is limited to a 50-word summary.
请首先判断提供的检索内容与上述问题是否相关。如果相关,直接从检索内容中提炼出直接回答问题所需的信息,不要乱说或者回答“相关”等字眼。如果检索内容与问题不相关,则不参考检索内容,则回答:“对不起,我无法回答此问题哦。" Please first judge whether the provided search content is relevant to the above question. If it is relevant, extract the information needed to answer the question directly from the search content, and do not use words such as relevant. If the content of the search is not relevant to the question, do not refer to the content of the search, and answer: I'm sorry, I can't answer this question.
``` ``
## Example: ## Example.
用户询问:“中国的首都是哪个城市?” 。 A user asks, Which city is the capital of China? .
2.1检索知识库,首先检查知识片段,如果“<context>”至“</context>”标签中没有与用户的问题相关的内容,则回答:“对不起,我无法回答此问题哦。 2.1 Retrieve the knowledge base, first check the knowledge fragment, if there is no content related to the user's question in the tags <context> to </context>, then answer, I'm sorry. I can't answer this question oh.
2.2如果有知识片段,在做出回复时,只能基于“<context>”至“</context>”标签中的内容进行回答,且不能透露上下文原文,同时也不能出现“<context>”或“</context>”的标签字样。 2.2 If there is a fragment of knowledge, the response can only be based on the content in the <context> to </context> tags, and cannot reveal the context of the original text, and also cannot appear as a <context> tag. <context> or </context> tags.
''' '''
prompt_template = [ prompt_template = [

View File

@ -21,7 +21,7 @@ class ChromaQuery(Blackbox):
# config = read_yaml(args[0]) # config = read_yaml(args[0])
# load chromadb and embedding model # load chromadb and embedding model
self.embedding_model_1 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda") self.embedding_model_1 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", device = "cuda")
# self.embedding_model_2 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/model/Weight/BAAI/bge-small-en-v1.5", device = "cuda") self.embedding_model_2 = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="/home/administrator/Workspace/Models/BAAI/bge-large-en-v1.5", device = "cuda")
self.client_1 = chromadb.HttpClient(host='172.16.5.8', port=7000) self.client_1 = chromadb.HttpClient(host='172.16.5.8', port=7000)
# self.client_2 = chromadb.HttpClient(host='10.6.82.192', port=8000) # self.client_2 = chromadb.HttpClient(host='10.6.82.192', port=8000)
@ -73,6 +73,8 @@ class ChromaQuery(Blackbox):
if re.search(r"/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", chroma_embedding_model): if re.search(r"/home/administrator/Workspace/Models/BAAI/bge-large-zh-v1.5", chroma_embedding_model):
embedding_model = self.embedding_model_1 embedding_model = self.embedding_model_1
elif re.search(r"/home/administrator/Workspace/Models/BAAI/bge-large-en-v1.5", chroma_embedding_model):
embedding_model = self.embedding_model_2
else: else:
embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=chroma_embedding_model, device = "cuda") embedding_model = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=chroma_embedding_model, device = "cuda")