mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-04-22 22:20:07 +08:00

### Description Following up on https://github.com/infiniflow/ragflow/pull/275, this PR adds support for FastEmbed model configurations. The options are not exhaustive. You can find the full list [here](https://qdrant.github.io/fastembed/examples/Supported_Models/). P.S. I ran into OOM issues when building the image. ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: KevinHuSh <kevinhu.sh@gmail.com>
221 lines
7.8 KiB
Python
221 lines
7.8 KiB
Python
#
|
|
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
#
|
|
from typing import Optional
|
|
from zhipuai import ZhipuAI
|
|
import os
|
|
from abc import ABC
|
|
from ollama import Client
|
|
import dashscope
|
|
from openai import OpenAI
|
|
from fastembed import TextEmbedding
|
|
from FlagEmbedding import FlagModel
|
|
import torch
|
|
import numpy as np
|
|
|
|
from api.utils.file_utils import get_project_base_directory
|
|
from rag.utils import num_tokens_from_string
|
|
|
|
try:
|
|
flag_model = FlagModel(os.path.join(
|
|
get_project_base_directory(),
|
|
"rag/res/bge-large-zh-v1.5"),
|
|
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
|
|
use_fp16=torch.cuda.is_available())
|
|
except Exception as e:
|
|
flag_model = FlagModel("BAAI/bge-large-zh-v1.5",
|
|
query_instruction_for_retrieval="为这个句子生成表示以用于检索相关文章:",
|
|
use_fp16=torch.cuda.is_available())
|
|
|
|
|
|
class Base(ABC):
|
|
def __init__(self, key, model_name):
|
|
pass
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
raise NotImplementedError("Please implement encode method!")
|
|
|
|
def encode_queries(self, text: str):
|
|
raise NotImplementedError("Please implement encode method!")
|
|
|
|
|
|
class HuEmbedding(Base):
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
If you have trouble downloading HuggingFace models, -_^ this might help!!
|
|
|
|
For Linux:
|
|
export HF_ENDPOINT=https://hf-mirror.com
|
|
|
|
For Windows:
|
|
Good luck
|
|
^_-
|
|
|
|
"""
|
|
self.model = flag_model
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
texts = [t[:2000] for t in texts]
|
|
token_count = 0
|
|
for t in texts:
|
|
token_count += num_tokens_from_string(t)
|
|
res = []
|
|
for i in range(0, len(texts), batch_size):
|
|
res.extend(self.model.encode(texts[i:i + batch_size]).tolist())
|
|
return np.array(res), token_count
|
|
|
|
def encode_queries(self, text: str):
|
|
token_count = num_tokens_from_string(text)
|
|
return self.model.encode_queries([text]).tolist()[0], token_count
|
|
|
|
|
|
class OpenAIEmbed(Base):
|
|
def __init__(self, key, model_name="text-embedding-ada-002", base_url="https://api.openai.com/v1"):
|
|
if not base_url: base_url="https://api.openai.com/v1"
|
|
self.client = OpenAI(api_key=key, base_url=base_url)
|
|
self.model_name = model_name
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
res = self.client.embeddings.create(input=texts,
|
|
model=self.model_name)
|
|
return np.array([d.embedding for d in res.data]
|
|
), res.usage.total_tokens
|
|
|
|
def encode_queries(self, text):
|
|
res = self.client.embeddings.create(input=[text],
|
|
model=self.model_name)
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens
|
|
|
|
|
|
class QWenEmbed(Base):
|
|
def __init__(self, key, model_name="text_embedding_v2", **kwargs):
|
|
dashscope.api_key = key
|
|
self.model_name = model_name
|
|
|
|
def encode(self, texts: list, batch_size=10):
|
|
import dashscope
|
|
res = []
|
|
token_count = 0
|
|
texts = [txt[:2048] for txt in texts]
|
|
for i in range(0, len(texts), batch_size):
|
|
resp = dashscope.TextEmbedding.call(
|
|
model=self.model_name,
|
|
input=texts[i:i + batch_size],
|
|
text_type="document"
|
|
)
|
|
embds = [[] for _ in range(len(resp["output"]["embeddings"]))]
|
|
for e in resp["output"]["embeddings"]:
|
|
embds[e["text_index"]] = e["embedding"]
|
|
res.extend(embds)
|
|
token_count += resp["usage"]["total_tokens"]
|
|
return np.array(res), token_count
|
|
|
|
def encode_queries(self, text):
|
|
resp = dashscope.TextEmbedding.call(
|
|
model=self.model_name,
|
|
input=text[:2048],
|
|
text_type="query"
|
|
)
|
|
return np.array(resp["output"]["embeddings"][0]
|
|
["embedding"]), resp["usage"]["total_tokens"]
|
|
|
|
|
|
class ZhipuEmbed(Base):
|
|
def __init__(self, key, model_name="embedding-2", **kwargs):
|
|
self.client = ZhipuAI(api_key=key)
|
|
self.model_name = model_name
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
arr = []
|
|
tks_num = 0
|
|
for txt in texts:
|
|
res = self.client.embeddings.create(input=txt,
|
|
model=self.model_name)
|
|
arr.append(res.data[0].embedding)
|
|
tks_num += res.usage.total_tokens
|
|
return np.array(arr), tks_num
|
|
|
|
def encode_queries(self, text):
|
|
res = self.client.embeddings.create(input=text,
|
|
model=self.model_name)
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens
|
|
|
|
|
|
class OllamaEmbed(Base):
|
|
def __init__(self, key, model_name, **kwargs):
|
|
self.client = Client(host=kwargs["base_url"])
|
|
self.model_name = model_name
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
arr = []
|
|
tks_num = 0
|
|
for txt in texts:
|
|
res = self.client.embeddings(prompt=txt,
|
|
model=self.model_name)
|
|
arr.append(res["embedding"])
|
|
tks_num += 128
|
|
return np.array(arr), tks_num
|
|
|
|
def encode_queries(self, text):
|
|
res = self.client.embeddings(prompt=text,
|
|
model=self.model_name)
|
|
return np.array(res["embedding"]), 128
|
|
|
|
|
|
class FastEmbed(Base):
|
|
def __init__(
|
|
self,
|
|
key: Optional[str] = None,
|
|
model_name: str = "BAAI/bge-small-en-v1.5",
|
|
cache_dir: Optional[str] = None,
|
|
threads: Optional[int] = None,
|
|
**kwargs,
|
|
):
|
|
self._model = TextEmbedding(model_name, cache_dir, threads, **kwargs)
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
# Using the internal tokenizer to encode the texts and get the total number of tokens
|
|
encodings = self._model.model.tokenizer.encode_batch(texts)
|
|
total_tokens = sum(len(e) for e in encodings)
|
|
|
|
embeddings = [e.tolist() for e in self._model.embed(texts, batch_size)]
|
|
|
|
return np.array(embeddings), total_tokens
|
|
|
|
def encode_queries(self, text: str):
|
|
# Using the internal tokenizer to encode the texts and get the total number of tokens
|
|
encoding = self._model.model.tokenizer.encode(text)
|
|
embedding = next(self._model.query_embed(text)).tolist()
|
|
|
|
return np.array(embedding), len(encoding.ids)
|
|
|
|
|
|
class XinferenceEmbed(Base):
|
|
def __init__(self, key, model_name="", base_url=""):
|
|
self.client = OpenAI(api_key="xxx", base_url=base_url)
|
|
self.model_name = model_name
|
|
|
|
def encode(self, texts: list, batch_size=32):
|
|
res = self.client.embeddings.create(input=texts,
|
|
model=self.model_name)
|
|
return np.array([d.embedding for d in res.data]
|
|
), res.usage.total_tokens
|
|
|
|
def encode_queries(self, text):
|
|
res = self.client.embeddings.create(input=[text],
|
|
model=self.model_name)
|
|
return np.array(res.data[0].embedding), res.usage.total_tokens
|
|
|