agentscope.embedding¶
agentscope中的嵌入模块。- class EmbeddingUsage[source]¶
基础类:
DictMixin嵌入模型API调用的用法。
- time: float¶
以秒为单位表示的使用时间。
- __init__(time, tokens=<factory>, type=<factory>)¶
- Parameters:
时间 (浮点数)
tokens (整数 | None)
type (Literal['embedding'])
- Return type:
无
- tokens: int | None¶
如果可用,已使用的令牌数量。
- type: Literal['embedding']¶
用法的类型,必须是 embedding。
- class EmbeddingResponse[source]¶
基类:
DictMixin嵌入响应类。
- embeddings: List[List[float]]¶
嵌入数据
- id: str¶
嵌入响应的标识
- created_at: str¶
嵌入响应创建的时间戳
- __init__(embeddings, id=<factory>, created_at=<factory>, type=<factory>, usage=<factory>, source=<factory>)¶
- Parameters:
embeddings (List[List[float]]) - 这是一个嵌入向量列表(浮点数列表的列表)
id (字符串)
created_at (str)
type (Literal['embedding'])
usage (EmbeddingUsage | None)
source (Literal['cache', 'api'])
- Return type:
无
- type: Literal['embedding']¶
响应的类型,必须是embedding。
- usage: EmbeddingUsage | None¶
如可用,嵌入模型API调用的使用方法。
- source: Literal['cache', 'api']¶
如果响应来自缓存或API。
- class DashScopeTextEmbedding[source]¶
-
DashScope 文本嵌入模型类
- __init__(api_key, model_name, embedding_cache=None)[source]¶
初始化DashScope文本嵌入模型类。
- Parameters:
api_key (str) – 用于访问dashscope服务的API密钥。
model_name (str) – 嵌入模型的名称。
embedding_cache (EmbeddingCacheBase) – 嵌入缓存类实例,用于缓存嵌入结果以避免重复调用API。
- Return type:
无
- class OpenAITextEmbedding[source]¶
Bases:
EmbeddingModelBaseOpenAI 文本嵌入模型类。
- class GeminiTextEmbedding[source]¶
-
Gemini文本嵌入模型。
- class OllamaTextEmbedding[source]¶
-
Ollama 嵌入模型。
- __init__(model_name, host=None, embedding_cache=None, **kwargs)[source]¶
初始化Ollama文本嵌入模型类。
- Parameters:
model_name (str) - 嵌入模型的名称。
host (str | None, defaults to None) – Ollama API 的主机 URL 地址。
embedding_cache (EmbeddingCacheBase | None, 默认为 None) – 嵌入缓存类的实例,用于缓存嵌入结果以避免重复的API调用。
kwargs (任意类型)
- Return type:
无
- class EmbeddingCacheBase[source]¶
基础:
object用于嵌入缓存的基础类,其负责存储和获取嵌入结果。
- abstract async store(embeddings, identifier, overwrite=False, **kwargs)[source]¶
以给定标识符存储嵌入内容。
- Parameters:
embeddings (List[Embedding]) – 要存储的嵌入向量。
identifier (JSONSerializableObject) – 用于区分嵌入向量的标识符。
overwrite (bool, 默认为 False) – 是否覆盖具有相同标识符的现有嵌入向量。如果为 True,则现有嵌入将被替换。
kwargs (Any):可选关键字参数
- Return type:
无
- abstract async retrieve(identifier)[source]¶
获取给定标识符对应的嵌入向量。如果未找到,则返回 None。
- Parameters:
identifier (JSONSerializableObject) – 用于检索嵌入向量的标识符.
- Return type:
列表[列表[浮点数]] | 无
- class FileEmbeddingCache[source]¶
Bases:
EmbeddingCacheBase存储每个嵌入向量到二进制文件中的嵌入缓存类。
- __init__(cache_dir='./.cache/embeddings', max_file_number=None, max_cache_size=None)[source]¶
初始化文件嵌入缓存类。
- Parameters:
cache_dir (str, 默认为 “./.cache/embeddings”) – 存储嵌入文件的目录。
max_file_number (int | None, 默认为 None) – 缓存目录中保留的最大文件数量。如果超过此数量,将删除最旧的文件。
max_cache_size (int | None, 默认为 None) – 缓存目录的最大大小(以MB为单位)。如果超过此限制,将删除最旧的文件,直到大小回到限制范围内。
- Return type:
无
- property cache_dir: str¶
嵌入文件存储的缓存目录。
- async store(embeddings, identifier, overwrite=False, **kwargs)[source]¶
使用给定标识符存储嵌入向量。
- Parameters:
embeddings (List[Embedding]) – 需要存储的嵌入向量。
identifier (JSONSerializableObject) – 用于区分嵌入的唯一标识符,该标识符将用于生成可哈希的文件名,因此它应当是JSON可序列化的(例如字符串、数字、列表、字典)。
overwrite (bool, 默认为 False) – 是否覆盖具有相同标识符的已有嵌入。如果为 True,则现有嵌入将被替换。
kwargs (Any):可选关键字参数
- Return type:
无
- async retrieve(identifier)[source]¶
根据给定的标识符检索嵌入向量。如果未找到, 则返回 None。
- Parameters:
identifier (JSONSerializableObject) – 用于检索嵌入向量的标识符,该标识符将 用于生成可哈希的文件名,因此它应该是 可 JSON 序列化的(例如字符串、数字、列表、字典)。
- Return type:
列表[列表[浮点数]] | 无