mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 20:51:48 +00:00
[improvement][chat] Move python code out of chat-core module
This commit is contained in:
21
chat/python/instances/chromadb_instance.py
Normal file
21
chat/python/instances/chromadb_instance.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from typing import Any, List, Mapping, Optional, Union
|
||||
|
||||
import chromadb
|
||||
from chromadb.api import Collection
|
||||
from chromadb.config import Settings
|
||||
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from config.config_parse import CHROMA_DB_PERSIST_PATH
|
||||
|
||||
|
||||
client = chromadb.Client(
|
||||
Settings(
|
||||
chroma_db_impl="duckdb+parquet",
|
||||
persist_directory=CHROMA_DB_PERSIST_PATH, # Optional, defaults to .chromadb/ in the current directory
|
||||
)
|
||||
)
|
||||
21
chat/python/instances/llm_instance.py
Normal file
21
chat/python/instances/llm_instance.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from langchain import llms
|
||||
|
||||
import os
|
||||
import sys
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from config.config_parse import LLM_PROVIDER_NAME, llm_config_dict
|
||||
|
||||
|
||||
def get_llm_provider(llm_provider_name: str, llm_config_dict: dict):
|
||||
if llm_provider_name in llms.type_to_cls_dict:
|
||||
llm_provider = llms.type_to_cls_dict[llm_provider_name]
|
||||
llm = llm_provider(**llm_config_dict)
|
||||
return llm
|
||||
else:
|
||||
raise Exception("llm_provider_name is not supported: {}".format(llm_provider_name))
|
||||
|
||||
|
||||
llm = get_llm_provider(LLM_PROVIDER_NAME, llm_config_dict)
|
||||
6
chat/python/instances/logging_instance.py
Normal file
6
chat/python/instances/logging_instance.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from loguru import logger
|
||||
import sys
|
||||
|
||||
logger.remove() #remove the old handler. Else, the old one will work along with the new one you've added below'
|
||||
logger.add(sys.stdout, format="{time:YYYY-MM-DD at HH:mm:ss} | {level} | {message}", level="INFO")
|
||||
|
||||
23
chat/python/instances/text2vec.py
Normal file
23
chat/python/instances/text2vec.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from typing import List
|
||||
|
||||
from chromadb.api.types import Documents, EmbeddingFunction, Embeddings
|
||||
from langchain.embeddings import HuggingFaceEmbeddings
|
||||
|
||||
from config.config_parse import HF_TEXT2VEC_MODEL_NAME
|
||||
|
||||
hg_embedding = HuggingFaceEmbeddings(model_name=HF_TEXT2VEC_MODEL_NAME)
|
||||
|
||||
|
||||
class Text2VecEmbeddingFunction(EmbeddingFunction):
|
||||
def __call__(self, texts: Documents) -> Embeddings:
|
||||
|
||||
embeddings = hg_embedding.embed_documents(texts)
|
||||
|
||||
return embeddings
|
||||
|
||||
|
||||
def get_embeddings(documents: List[str]) -> List[List[float]]:
|
||||
embeddings = hg_embedding.embed_documents(documents)
|
||||
|
||||
return embeddings
|
||||
Reference in New Issue
Block a user