chore: 添加虚拟环境到仓库
- 添加 backend_service/venv 虚拟环境 - 包含所有Python依赖包 - 注意:虚拟环境约393MB,包含12655个文件
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from .batch_text_embedding import BatchTextEmbedding
|
||||
from .batch_text_embedding_response import BatchTextEmbeddingResponse
|
||||
from .text_embedding import TextEmbedding
|
||||
|
||||
__all__ = [TextEmbedding, BatchTextEmbedding, BatchTextEmbeddingResponse]
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,208 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from typing import Union
|
||||
|
||||
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
||||
from dashscope.client.base_api import BaseAsyncApi
|
||||
from dashscope.common.error import InputRequired
|
||||
from dashscope.common.utils import _get_task_group_and_task
|
||||
from dashscope.embeddings.batch_text_embedding_response import \
|
||||
BatchTextEmbeddingResponse
|
||||
|
||||
|
||||
class BatchTextEmbedding(BaseAsyncApi):
|
||||
task = 'text-embedding'
|
||||
function = 'text-embedding'
|
||||
"""API for async text embedding.
|
||||
"""
|
||||
class Models:
|
||||
text_embedding_async_v1 = 'text-embedding-async-v1'
|
||||
text_embedding_async_v2 = 'text-embedding-async-v2'
|
||||
|
||||
@classmethod
|
||||
def call(cls,
|
||||
model: str,
|
||||
url: str,
|
||||
api_key: str = None,
|
||||
workspace: str = None,
|
||||
**kwargs) -> BatchTextEmbeddingResponse:
|
||||
"""Call async text embedding service and get result.
|
||||
|
||||
Args:
|
||||
model (str): The model, reference ``Models``.
|
||||
url (Any): The async request file url, which contains text
|
||||
to embedding line by line.
|
||||
api_key (str, optional): The api api_key. Defaults to None.
|
||||
workspace (str): The dashscope workspace id.
|
||||
**kwargs:
|
||||
text_type(str, `optional`): [query|document], After the
|
||||
text is converted into a vector, it can be applied to
|
||||
downstream tasks such as retrieval, clustering, and
|
||||
classification. For asymmetric tasks such as retrieval,
|
||||
in order to achieve better retrieval results, it is
|
||||
recommended to distinguish between query text (query)
|
||||
and bottom database text (document) types, clustering
|
||||
Symmetric tasks such as , classification, etc. do not
|
||||
need to be specially specified, and the system
|
||||
default value "document" can be used
|
||||
Raises:
|
||||
InputRequired: The url cannot be empty.
|
||||
|
||||
Returns:
|
||||
AsyncTextEmbeddingResponse: The async text embedding task result.
|
||||
"""
|
||||
return super().call(model,
|
||||
url,
|
||||
api_key=api_key,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
|
||||
@classmethod
|
||||
def async_call(cls,
|
||||
model: str,
|
||||
url: str,
|
||||
api_key: str = None,
|
||||
workspace: str = None,
|
||||
**kwargs) -> BatchTextEmbeddingResponse:
|
||||
"""Create a async text embedding task, and return task information.
|
||||
|
||||
Args:
|
||||
model (str): The model, reference ``Models``.
|
||||
url (Any): The async request file url, which contains text
|
||||
to embedding line by line.
|
||||
api_key (str, optional): The api api_key. Defaults to None.
|
||||
workspace (str): The dashscope workspace id.
|
||||
**kwargs:
|
||||
text_type(str, `optional`): [query|document], After the
|
||||
text is converted into a vector, it can be applied to
|
||||
downstream tasks such as retrieval, clustering, and
|
||||
classification. For asymmetric tasks such as retrieval,
|
||||
in order to achieve better retrieval results, it is
|
||||
recommended to distinguish between query text (query)
|
||||
and bottom database text (document) types, clustering
|
||||
Symmetric tasks such as , classification, etc. do not
|
||||
need to be specially specified, and the system
|
||||
default value "document" can be used
|
||||
|
||||
Raises:
|
||||
InputRequired: The url cannot be empty.
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The image synthesis
|
||||
task id in the response.
|
||||
"""
|
||||
if url is None or not url:
|
||||
raise InputRequired('url is required!')
|
||||
input = {'url': url}
|
||||
task_group, _ = _get_task_group_and_task(__name__)
|
||||
response = super().async_call(model=model,
|
||||
task_group=task_group,
|
||||
task=BatchTextEmbedding.task,
|
||||
function=BatchTextEmbedding.function,
|
||||
api_key=api_key,
|
||||
input=input,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
return BatchTextEmbeddingResponse.from_api_response(response)
|
||||
|
||||
@classmethod
|
||||
def fetch(cls,
|
||||
task: Union[str, BatchTextEmbeddingResponse],
|
||||
api_key: str = None,
|
||||
workspace: str = None) -> BatchTextEmbeddingResponse:
|
||||
"""Fetch async text embedding task status or result.
|
||||
|
||||
Args:
|
||||
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
||||
AsyncTextEmbeddingResponse return by async_call().
|
||||
api_key (str, optional): The api api_key. Defaults to None.
|
||||
workspace (str): The dashscope workspace id.
|
||||
|
||||
Returns:
|
||||
AsyncTextEmbeddingResponse: The task status or result.
|
||||
"""
|
||||
response = super().fetch(task, api_key, workspace=workspace)
|
||||
return BatchTextEmbeddingResponse.from_api_response(response)
|
||||
|
||||
@classmethod
|
||||
def wait(cls,
|
||||
task: Union[str, BatchTextEmbeddingResponse],
|
||||
api_key: str = None,
|
||||
workspace: str = None) -> BatchTextEmbeddingResponse:
|
||||
"""Wait for async text embedding task to complete, and return the result.
|
||||
|
||||
Args:
|
||||
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
||||
AsyncTextEmbeddingResponse return by async_call().
|
||||
api_key (str, optional): The api api_key. Defaults to None.
|
||||
workspace (str): The dashscope workspace id.
|
||||
|
||||
Returns:
|
||||
AsyncTextEmbeddingResponse: The task result.
|
||||
"""
|
||||
response = super().wait(task, api_key, workspace=workspace)
|
||||
return BatchTextEmbeddingResponse.from_api_response(response)
|
||||
|
||||
@classmethod
|
||||
def cancel(cls,
|
||||
task: Union[str, BatchTextEmbeddingResponse],
|
||||
api_key: str = None,
|
||||
workspace: str = None) -> DashScopeAPIResponse:
|
||||
"""Cancel async text embedding task.
|
||||
Only tasks whose status is PENDING can be canceled.
|
||||
|
||||
Args:
|
||||
task (Union[str, AsyncTextEmbeddingResponse]): The task_id or
|
||||
AsyncTextEmbeddingResponse return by async_call().
|
||||
api_key (str, optional): The api api_key. Defaults to None.
|
||||
workspace (str): The dashscope workspace id.
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The response data.
|
||||
"""
|
||||
return super().cancel(task, api_key, workspace=workspace)
|
||||
|
||||
@classmethod
|
||||
def list(cls,
|
||||
start_time: str = None,
|
||||
end_time: str = None,
|
||||
model_name: str = None,
|
||||
api_key_id: str = None,
|
||||
region: str = None,
|
||||
status: str = None,
|
||||
page_no: int = 1,
|
||||
page_size: int = 10,
|
||||
api_key: str = None,
|
||||
workspace: str = None,
|
||||
**kwargs) -> DashScopeAPIResponse:
|
||||
"""List async tasks.
|
||||
|
||||
Args:
|
||||
start_time (str, optional): The tasks start time,
|
||||
for example: 20230420000000. Defaults to None.
|
||||
end_time (str, optional): The tasks end time,
|
||||
for example: 20230420000000. Defaults to None.
|
||||
model_name (str, optional): The tasks model name. Defaults to None.
|
||||
api_key_id (str, optional): The tasks api-key-id. Defaults to None.
|
||||
region (str, optional): The service region,
|
||||
for example: cn-beijing. Defaults to None.
|
||||
status (str, optional): The status of tasks[PENDING,
|
||||
RUNNING, SUCCEEDED, FAILED, CANCELED]. Defaults to None.
|
||||
page_no (int, optional): The page number. Defaults to 1.
|
||||
page_size (int, optional): The page size. Defaults to 10.
|
||||
api_key (str, optional): The user api-key. Defaults to None.
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The response data.
|
||||
"""
|
||||
return super().list(start_time=start_time,
|
||||
end_time=end_time,
|
||||
model_name=model_name,
|
||||
api_key_id=api_key_id,
|
||||
region=region,
|
||||
status=status,
|
||||
page_no=page_no,
|
||||
page_size=page_size,
|
||||
api_key=api_key,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
@@ -0,0 +1,65 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from http import HTTPStatus
|
||||
|
||||
from attr import dataclass
|
||||
|
||||
from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
|
||||
DictMixin)
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class BatchTextEmbeddingOutput(DictMixin):
|
||||
task_id: str
|
||||
task_status: str
|
||||
url: str
|
||||
|
||||
def __init__(self,
|
||||
task_id: str,
|
||||
task_status: str,
|
||||
url: str = None,
|
||||
**kwargs):
|
||||
super().__init__(self,
|
||||
task_id=task_id,
|
||||
task_status=task_status,
|
||||
url=url,
|
||||
**kwargs)
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class BatchTextEmbeddingUsage(DictMixin):
|
||||
total_tokens: int
|
||||
|
||||
def __init__(self, total_tokens: int=None, **kwargs):
|
||||
super().__init__(total_tokens=total_tokens, **kwargs)
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class BatchTextEmbeddingResponse(DashScopeAPIResponse):
|
||||
output: BatchTextEmbeddingOutput
|
||||
usage: BatchTextEmbeddingUsage
|
||||
|
||||
@staticmethod
|
||||
def from_api_response(api_response: DashScopeAPIResponse):
|
||||
if api_response.status_code == HTTPStatus.OK:
|
||||
output = None
|
||||
usage = None
|
||||
if api_response.output is not None:
|
||||
output = BatchTextEmbeddingOutput(**api_response.output)
|
||||
if api_response.usage is not None:
|
||||
usage = BatchTextEmbeddingUsage(**api_response.usage)
|
||||
|
||||
return BatchTextEmbeddingResponse(
|
||||
status_code=api_response.status_code,
|
||||
request_id=api_response.request_id,
|
||||
code=api_response.code,
|
||||
message=api_response.message,
|
||||
output=output,
|
||||
usage=usage)
|
||||
|
||||
else:
|
||||
return BatchTextEmbeddingResponse(
|
||||
status_code=api_response.status_code,
|
||||
request_id=api_response.request_id,
|
||||
code=api_response.code,
|
||||
message=api_response.message)
|
||||
@@ -0,0 +1,182 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import List
|
||||
|
||||
from dashscope.api_entities.dashscope_response import (DashScopeAPIResponse,
|
||||
DictMixin)
|
||||
from dashscope.client.base_api import BaseApi, BaseAioApi
|
||||
from dashscope.common.error import InputRequired, ModelRequired
|
||||
from dashscope.common.utils import _get_task_group_and_task
|
||||
from dashscope.utils.oss_utils import preprocess_message_element
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class MultiModalEmbeddingItemBase(DictMixin):
|
||||
factor: float
|
||||
|
||||
def __init__(self, factor: float, **kwargs):
|
||||
super().__init__(factor=factor, **kwargs)
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class MultiModalEmbeddingItemText(MultiModalEmbeddingItemBase):
|
||||
text: str
|
||||
|
||||
def __init__(self, text: str, factor: float, **kwargs):
|
||||
super().__init__(factor, **kwargs)
|
||||
self.text = text
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class MultiModalEmbeddingItemImage(MultiModalEmbeddingItemBase):
|
||||
image: str
|
||||
|
||||
def __init__(self, image: str, factor: float, **kwargs):
|
||||
super().__init__(factor, **kwargs)
|
||||
self.image = image
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class MultiModalEmbeddingItemAudio(MultiModalEmbeddingItemBase):
|
||||
audio: str
|
||||
|
||||
def __init__(self, audio: str, factor: float, **kwargs):
|
||||
super().__init__(factor, **kwargs)
|
||||
self.audio = audio
|
||||
|
||||
|
||||
class MultiModalEmbedding(BaseApi):
|
||||
task = 'multimodal-embedding'
|
||||
|
||||
class Models:
|
||||
multimodal_embedding_one_peace_v1 = 'multimodal-embedding-one-peace-v1'
|
||||
|
||||
@classmethod
|
||||
def call(cls,
|
||||
model: str,
|
||||
input: List[MultiModalEmbeddingItemBase],
|
||||
api_key: str = None,
|
||||
workspace: str = None,
|
||||
**kwargs) -> DashScopeAPIResponse:
|
||||
"""Get embedding multimodal contents..
|
||||
|
||||
Args:
|
||||
model (str): The embedding model name.
|
||||
input (List[MultiModalEmbeddingElement]): The embedding elements,
|
||||
every element include data, modal, factor field.
|
||||
workspace (str): The dashscope workspace id.
|
||||
**kwargs:
|
||||
auto_truncation(bool, `optional`): Automatically truncate
|
||||
audio longer than 15 seconds or text longer than 70 words.
|
||||
Default to false(Too long input will result in failure).
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The embedding result.
|
||||
"""
|
||||
if input is None or not input:
|
||||
raise InputRequired('prompt is required!')
|
||||
if model is None or not model:
|
||||
raise ModelRequired('Model is required!')
|
||||
embedding_input = {}
|
||||
has_upload = cls._preprocess_message_inputs(model, input, api_key)
|
||||
if has_upload:
|
||||
headers = kwargs.pop('headers', {})
|
||||
headers['X-DashScope-OssResourceResolve'] = 'enable'
|
||||
kwargs['headers'] = headers
|
||||
embedding_input['contents'] = input
|
||||
kwargs.pop('stream', False) # not support streaming output.
|
||||
task_group, function = _get_task_group_and_task(__name__)
|
||||
return super().call(model=model,
|
||||
input=embedding_input,
|
||||
task_group=task_group,
|
||||
task=MultiModalEmbedding.task,
|
||||
function=function,
|
||||
api_key=api_key,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
|
||||
@classmethod
|
||||
def _preprocess_message_inputs(cls, model: str, input: List[dict],
|
||||
api_key: str):
|
||||
"""preprocess following inputs
|
||||
input = [{'factor': 1, 'text': 'hello'},
|
||||
{'factor': 2, 'audio': ''},
|
||||
{'factor': 3, 'image': ''}]
|
||||
"""
|
||||
has_upload = False
|
||||
for elem in input:
|
||||
if not isinstance(elem, (int, float, bool, str, bytes, bytearray)):
|
||||
is_upload = preprocess_message_element(model, elem, api_key)
|
||||
if is_upload and not has_upload:
|
||||
has_upload = True
|
||||
return has_upload
|
||||
|
||||
|
||||
class AioMultiModalEmbedding(BaseAioApi):
|
||||
task = 'multimodal-embedding'
|
||||
|
||||
class Models:
|
||||
multimodal_embedding_one_peace_v1 = 'multimodal-embedding-one-peace-v1'
|
||||
|
||||
@classmethod
|
||||
async def call(cls,
|
||||
model: str,
|
||||
input: List[MultiModalEmbeddingItemBase],
|
||||
api_key: str = None,
|
||||
workspace: str = None,
|
||||
**kwargs) -> DashScopeAPIResponse:
|
||||
"""Get embedding multimodal contents..
|
||||
|
||||
Args:
|
||||
model (str): The embedding model name.
|
||||
input (List[MultiModalEmbeddingElement]): The embedding elements,
|
||||
every element include data, modal, factor field.
|
||||
workspace (str): The dashscope workspace id.
|
||||
**kwargs:
|
||||
auto_truncation(bool, `optional`): Automatically truncate
|
||||
audio longer than 15 seconds or text longer than 70 words.
|
||||
Default to false(Too long input will result in failure).
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The embedding result.
|
||||
"""
|
||||
if input is None or not input:
|
||||
raise InputRequired('prompt is required!')
|
||||
if model is None or not model:
|
||||
raise ModelRequired('Model is required!')
|
||||
embedding_input = {}
|
||||
has_upload = cls._preprocess_message_inputs(model, input, api_key)
|
||||
if has_upload:
|
||||
headers = kwargs.pop('headers', {})
|
||||
headers['X-DashScope-OssResourceResolve'] = 'enable'
|
||||
kwargs['headers'] = headers
|
||||
embedding_input['contents'] = input
|
||||
kwargs.pop('stream', False) # not support streaming output.
|
||||
task_group, function = _get_task_group_and_task(__name__)
|
||||
response = await super().call(
|
||||
model=model,
|
||||
input=embedding_input,
|
||||
task_group=task_group,
|
||||
task=MultiModalEmbedding.task,
|
||||
function=function,
|
||||
api_key=api_key,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
return response
|
||||
|
||||
@classmethod
|
||||
def _preprocess_message_inputs(cls, model: str, input: List[dict],
|
||||
api_key: str):
|
||||
"""preprocess following inputs
|
||||
input = [{'factor': 1, 'text': 'hello'},
|
||||
{'factor': 2, 'audio': ''},
|
||||
{'factor': 3, 'image': ''}]
|
||||
"""
|
||||
has_upload = False
|
||||
for elem in input:
|
||||
if not isinstance(elem, (int, float, bool, str, bytes, bytearray)):
|
||||
is_upload = preprocess_message_element(model, elem, api_key)
|
||||
if is_upload and not has_upload:
|
||||
has_upload = True
|
||||
return has_upload
|
||||
@@ -0,0 +1,56 @@
|
||||
# Copyright (c) Alibaba, Inc. and its affiliates.
|
||||
|
||||
from typing import List, Union
|
||||
|
||||
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
|
||||
from dashscope.client.base_api import BaseApi
|
||||
from dashscope.common.constants import TEXT_EMBEDDING_INPUT_KEY
|
||||
from dashscope.common.utils import _get_task_group_and_task
|
||||
|
||||
|
||||
class TextEmbedding(BaseApi):
|
||||
task = 'text-embedding'
|
||||
|
||||
class Models:
|
||||
text_embedding_v1 = 'text-embedding-v1'
|
||||
text_embedding_v2 = 'text-embedding-v2'
|
||||
text_embedding_v3 = 'text-embedding-v3'
|
||||
text_embedding_v4 = 'text-embedding-v4'
|
||||
|
||||
@classmethod
|
||||
def call(cls,
|
||||
model: str,
|
||||
input: Union[str, List[str]],
|
||||
workspace: str = None,
|
||||
api_key: str = None,
|
||||
**kwargs) -> DashScopeAPIResponse:
|
||||
"""Get embedding of text input.
|
||||
|
||||
Args:
|
||||
model (str): The embedding model name.
|
||||
input (Union[str, List[str], io.IOBase]): The text input,
|
||||
can be a text or list of text or opened file object,
|
||||
if opened file object, will read all lines,
|
||||
one embedding per line.
|
||||
workspace (str): The dashscope workspace id.
|
||||
**kwargs:
|
||||
text_type(str, `optional`): query or document.
|
||||
|
||||
Returns:
|
||||
DashScopeAPIResponse: The embedding result.
|
||||
"""
|
||||
embedding_input = {}
|
||||
if isinstance(input, str):
|
||||
embedding_input[TEXT_EMBEDDING_INPUT_KEY] = [input]
|
||||
else:
|
||||
embedding_input[TEXT_EMBEDDING_INPUT_KEY] = input
|
||||
kwargs.pop('stream', False) # not support streaming output.
|
||||
task_group, function = _get_task_group_and_task(__name__)
|
||||
return super().call(model=model,
|
||||
input=embedding_input,
|
||||
task_group=task_group,
|
||||
task=TextEmbedding.task,
|
||||
function=function,
|
||||
api_key=api_key,
|
||||
workspace=workspace,
|
||||
**kwargs)
|
||||
Reference in New Issue
Block a user