-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
433 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
chat: | ||
- name: gpt-3.5-turbo | ||
path: Qwen/Qwen-14B-Chat | ||
device: | ||
- 0 | ||
maxlen: 4096 | ||
agent_type: react | ||
template: templates/qwen.jinja | ||
gen_config: generation_config/qwen | ||
port: 8020 | ||
|
||
embed: | ||
- name: text-embedding-ada-002 | ||
path: /home/incoming/zhengyw/bge-base-zh-v1.5 | ||
device: | ||
- 1 | ||
batchsize: 64 | ||
port: 8030 | ||
|
||
service: | ||
host: 127.0.0.1 | ||
port: 8010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
numpy | ||
sse-starlette | ||
transformers>=4.34.0 | ||
vllm>=0.2.6 | ||
transformers>=4.37.2 | ||
vllm>=0.3.0 | ||
infinity-emb[torch] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
from .service import Imitater | ||
|
||
|
||
__all__ = ["Imitater"] | ||
__version__ = "0.1.5" | ||
__version__ = "0.1.6" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .chat_model import ChatModel | ||
from .embed_model import EmbedModel | ||
from .chat_model import ChatConfig, ChatModel | ||
from .embed_model import EmbedConfig, EmbedModel | ||
|
||
|
||
__all__ = ["ChatModel", "EmbedModel"] | ||
__all__ = ["ChatConfig", "ChatModel", "EmbedConfig", "EmbedModel"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,59 @@ | ||
import asyncio | ||
from typing import TYPE_CHECKING, List, Optional | ||
from dataclasses import dataclass, fields | ||
from typing import TYPE_CHECKING, List | ||
|
||
import torch | ||
from transformers import AutoModel, AutoTokenizer | ||
from infinity_emb import AsyncEmbeddingEngine | ||
from typing_extensions import Self | ||
|
||
|
||
if TYPE_CHECKING: | ||
from transformers import BatchEncoding, PreTrainedModel | ||
from argparse import ArgumentParser, Namespace | ||
|
||
from ..config import Config | ||
from numpy import float32 | ||
from numpy.typing import NDArray | ||
|
||
|
||
@torch.inference_mode() | ||
def _get_embeddings(model: "PreTrainedModel", batch_encoding: "BatchEncoding") -> List[List[float]]: | ||
output = model(**batch_encoding.to(model.device)) | ||
embeddings = output[0][:, 0] | ||
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1).tolist() | ||
return embeddings | ||
@dataclass | ||
class EmbedConfig: | ||
name: str | ||
path: str | ||
device: List[int] | ||
batch_size: int | ||
port: int | ||
|
||
@staticmethod | ||
def add_cli_args(parser: "ArgumentParser") -> None: | ||
parser.add_argument("--name", type=str) | ||
parser.add_argument("--path", type=str) | ||
parser.add_argument("--device", type=int, nargs="+") | ||
parser.add_argument("--batch_size", type=int, default=64) | ||
parser.add_argument("--port", type=int) | ||
|
||
@classmethod | ||
def from_cli_args(cls, args: "Namespace") -> Self: | ||
attrs = [attr.name for attr in fields(cls)] | ||
return cls(**{attr: getattr(args, attr) for attr in attrs}) | ||
|
||
|
||
class EmbedModel: | ||
def __init__(self, config: "Config", max_tasks: Optional[int] = 5) -> None: | ||
self._semaphore = asyncio.Semaphore(max_tasks) | ||
self._batch_size = config.embed_batch_size | ||
self._model: "PreTrainedModel" = AutoModel.from_pretrained( | ||
config.embed_model_path, | ||
device_map={"": config.embed_model_device[0]}, | ||
torch_dtype=torch.float16, | ||
def __init__(self, config: "EmbedConfig") -> None: | ||
self.config = config | ||
self.name = config.name | ||
if len(config.device) != 1: | ||
raise ValueError("Embedding model only accepts one device.") | ||
|
||
self._engine = AsyncEmbeddingEngine( | ||
model_name_or_path=config.path, | ||
batch_size=config.batch_size, | ||
engine="torch", | ||
device="cuda", | ||
) | ||
self._model.eval() | ||
self._tokenizer = AutoTokenizer.from_pretrained(config.embed_model_path) | ||
self._tokenizer.padding_side = "right" | ||
|
||
async def _run_task(self, batch_encoding: "BatchEncoding") -> List[List[float]]: | ||
async with self._semaphore: | ||
loop = asyncio.get_running_loop() | ||
return await loop.run_in_executor(None, _get_embeddings, self._model, batch_encoding) | ||
|
||
async def embed(self, texts: List[str]) -> List[List[float]]: | ||
results = [] | ||
for i in range(0, len(texts), self._batch_size): | ||
batch_encoding = self._tokenizer( | ||
texts[i : i + self._batch_size], padding=True, truncation=True, return_tensors="pt" | ||
) | ||
embeddings = await self._run_task(batch_encoding) | ||
results.extend(embeddings) | ||
|
||
return results | ||
|
||
async def startup(self) -> None: | ||
await self._engine.astart() | ||
|
||
async def shutdown(self) -> None: | ||
await self._engine.astop() | ||
|
||
async def embed(self, texts: List[str]) -> List["NDArray[float32]"]: | ||
embeddings, _ = await self._engine.embed(texts) | ||
return embeddings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +0,0 @@ | ||
from .app import Imitater | ||
|
||
|
||
__all__ = ["Imitater"] | ||
Oops, something went wrong.