update blackbox chroma and chat

This commit is contained in:
ACBBZ
2024-05-28 08:23:10 +00:00
parent 347eea2f9c
commit ca34962376
4 changed files with 113 additions and 48 deletions

View File

@ -17,7 +17,12 @@ import chromadb
import os
import tempfile
from ..utils import chroma_setting
import logging
from ..log.logging_time import logging_time
import re
logger = logging.getLogger
DEFAULT_COLLECTION_ID = "123"
from injector import singleton
@singleton
@ -26,9 +31,9 @@ class ChromaUpsert(Blackbox):
def __init__(self, *args, **kwargs) -> None:
# config = read_yaml(args[0])
# load embedding model
self.embedding_model = SentenceTransformerEmbeddings(model_name='/model/Weight/BAAI/bge-small-en-v1.5', model_kwargs={"device": "cuda"})
self.embedding_model_1 = SentenceTransformerEmbeddings(model_name="/model/Weight/BAAI/bge-large-zh-v1.5", model_kwargs={"device": "cuda"})
# load chroma db
self.client = chromadb.HttpClient(host='10.6.82.192', port=8000)
self.client_1 = chromadb.HttpClient(host='10.6.82.192', port=8000)
def __call__(self, *args, **kwargs):
return self.processing(*args, **kwargs)
@ -37,30 +42,59 @@ class ChromaUpsert(Blackbox):
data = args[0]
return isinstance(data, list)
def processing(self, collection_id, file, string, context, setting: chroma_setting) -> str:
@logging_time(logger=logger)
def processing(self, file, string, context: list, settings: dict) -> str:
# 用户的操作历史
if context is None:
context = []
context = [
{
"collection_id": "123",
"action": "query",
"content": "你吃饭了吗",
"answer": "吃了",
},
{
"collection_id": "123",
"action": "upsert",
"content": "file_name or string",
"answer": "collection 123 has 12472 documents. /tmp/Cheap and QuickEfficient Vision-Language Instruction Tuning for Large Language Models.pdf ids is 0~111",
},
]
# context = [
# {
# "collection_id": "123",
# "action": "query",
# "content": "你吃饭了吗",
# "answer": "吃了",
# },
# {
# "collection_id": "123",
# "action": "upsert",
# "content": "file_name or string",
# "answer": "collection 123 has 12472 documents. /tmp/Cheap and QuickEfficient Vision-Language Instruction Tuning for Large Language Models.pdf ids is 0~111",
# },
# ]
if collection_id is None and setting.ChromaSetting.collection_ids[0] != []:
collection_id = setting.ChromaSetting.collection_ids[0]
if settings is None:
settings = {}
# # chroma_query settings
chroma_embedding_model = settings.get("chroma_embedding_model")
chroma_host = settings.get("chroma_host")
chroma_port = settings.get("chroma_port")
chroma_collection_id = settings.get("chroma_collection_id")
if chroma_embedding_model is None or chroma_embedding_model.isspace() or chroma_embedding_model == "":
chroma_embedding_model = "/model/Weight/BAAI/bge-large-zh-v1.5"
if chroma_host is None or chroma_host.isspace() or chroma_host == "":
chroma_host = "10.6.82.192"
if chroma_port is None or chroma_port.isspace() or chroma_port == "":
chroma_port = "8000"
if chroma_collection_id is None or chroma_collection_id.isspace() or chroma_collection_id == "":
chroma_collection_id = "g2e"
# load client and embedding model from init
if re.search(r"10.6.82.192", chroma_host) and re.search(r"8000", chroma_port):
client = self.client_1
else:
collection_id = "123"
client = chromadb.HttpClient(host=chroma_host, port=chroma_port)
if re.search(r"/model/Weight/BAAI/bge-large-zh-v1.5", chroma_embedding_model):
embedding_model = self.embedding_model_1
else:
embedding_model = SentenceTransformerEmbeddings(model_name=chroma_embedding_model, device = "cuda")
if file is not None:
file_type = file.split(".")[-1]
@ -79,7 +113,6 @@ class ChromaUpsert(Blackbox):
loader = Docx2txtLoader(file)
elif file_type == "xlsx":
loader = UnstructuredExcelLoader(file)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=0)
@ -88,21 +121,21 @@ class ChromaUpsert(Blackbox):
ids = [str(file)+str(i) for i in range(len(docs))]
Chroma.from_documents(documents=docs, embedding=self.embedding_model, ids=ids, collection_name=collection_id, client=self.client)
Chroma.from_documents(documents=docs, embedding=embedding_model, ids=ids, collection_name=chroma_collection_id, client=client)
collection_number = self.client.get_collection(collection_id).count()
response_file = f"collection {collection_id} has {collection_number} documents. {file} ids is 0~{len(docs)-1}"
collection_number = client.get_collection(chroma_collection_id).count()
response_file = f"collection {chroma_collection_id} has {collection_number} documents. {file} ids is 0~{len(docs)-1}"
if string is not None:
# 生成一个新的id ids_string: 1
# ids = setting.ChromaSetting.string_ids[0] + 1
ids = "1"
Chroma.from_texts(texts=[string], embedding=self.embedding_model, ids=[ids], collection_name=collection_id, client=self.client)
Chroma.from_texts(texts=[string], embedding=embedding_model, ids=[ids], collection_name=chroma_collection_id, client=client)
collection_number = self.client.get_collection(collection_id).count()
response_string = f"collection {collection_id} has {collection_number} documents. {string} ids is {ids}"
collection_number = client.get_collection(chroma_collection_id).count()
response_string = f"collection {chroma_collection_id} has {collection_number} documents. {string} ids is {ids}"
if file is not None and string is not None:
@ -116,14 +149,10 @@ class ChromaUpsert(Blackbox):
async def fast_api_handler(self, request: Request) -> Response:
user_collection_id = (await request.form()).get("collection_id")
user_file = (await request.form()).get("file")
user_string = (await request.form()).get("string")
user_context = (await request.form()).get("context")
user_setting = (await request.form()).get("setting")
if user_collection_id is None and user_setting["collections"] == []:
return JSONResponse(content={"error": "The first creation requires a collection id"}, status_code=status.HTTP_400_BAD_REQUEST)
context = (await request.form()).get("context")
setting: dict = (await request.form()).get("settings")
if user_file is None and user_string is None:
return JSONResponse(content={"error": "file or string is required"}, status_code=status.HTTP_400_BAD_REQUEST)
@ -142,5 +171,5 @@ class ChromaUpsert(Blackbox):
return JSONResponse(
content={"response": self.processing(user_collection_id, safe_filename, user_string, user_context, user_setting)},
content={"response": self.processing(safe_filename, user_string, context, setting)},
status_code=status.HTTP_200_OK)