AutoGen智能体开发:autogen_ext.memory.chromadb
autogen_ext.memory.chromadb
class ChromaDBVectorMemory(config: ChromaDBVectorMemoryConfig | None = None)[源]
基类:Memory、Component[ChromaDBVectorMemoryConfig]
使用 ChromaDB 支持的向量相似性搜索存储和检索记忆。
ChromaDBVectorMemory 提供了一种基于向量的记忆实现,它使用 ChromaDB 根据语义相似性存储和检索内容。它通过利用向量嵌入来查找相似内容,增强了智能体在对话中回忆上下文相关信息的能力。
此实现可作为使用向量嵌入的更复杂记忆系统的参考。对于需要检索内容特殊格式的高级用例,用户应扩展此类别并重写 update_context() 方法。
此实现需要安装 ChromaDB 附加组件。安装方式:
pip install "autogen-ext[chromadb]"
参数:
config (ChromaDBVectorMemoryConfig | None) – ChromaDB 记忆的配置。如果为 None,则默认为具有默认值的 PersistentChromaDBVectorMemoryConfig。支持两种配置类型: * PersistentChromaDBVectorMemoryConfig:用于本地存储 * HttpChromaDBVectorMemoryConfig:用于连接远程 ChromaDB 服务器
示例
import os
import asyncio
from pathlib import Path
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_core.memory import MemoryContent, MemoryMimeType
from autogen_ext.memory.chromadb import (
ChromaDBVectorMemory,
PersistentChromaDBVectorMemoryConfig,
SentenceTransformerEmbeddingFunctionConfig,
OpenAIEmbeddingFunctionConfig,
)
from autogen_ext.models.openai import OpenAIChatCompletionClient
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny with a high of 90°F and a low of 70°F."
def fahrenheit_to_celsius(fahrenheit: float) -> float:
return (fahrenheit - 32) * 5.0 / 9.0
async def main() -> None:
# Use default embedding function
default_memory = ChromaDBVectorMemory(
config=PersistentChromaDBVectorMemoryConfig(
collection_name="user_preferences",
persistence_path=os.path.join(str(Path.home()), ".chromadb_autogen"),
k=3, # Return top 3 results
score_threshold=0.5, # Minimum similarity score
)
)
# Using a custom SentenceTransformer model
custom_memory = ChromaDBVectorMemory(
config=PersistentChromaDBVectorMemoryConfig(
collection_name="multilingual_memory",
persistence_path=os.path.join(str(Path.home()), ".chromadb_autogen"),
embedding_function_config=SentenceTransformerEmbeddingFunctionConfig(
model_name="paraphrase-multilingual-mpnet-base-v2"
),
)
)
# Using OpenAI embeddings
openai_memory = ChromaDBVectorMemory(
config=PersistentChromaDBVectorMemoryConfig(
collection_name="openai_memory",
persistence_path=os.path.join(str(Path.home()), ".chromadb_autogen"),
embedding_function_config=OpenAIEmbeddingFunctionConfig(
api_key=os.environ["OPENAI_API_KEY"], model_name="text-embedding-3-small"
),
)
)
# Add user preferences to memory
await openai_memory.add(
MemoryContent(
content="The user prefers weather temperatures in Celsius",
mime_type=MemoryMimeType.TEXT,
metadata={"category": "preferences", "type": "units"},
)
)
# Create assistant agent with ChromaDB memory
assistant = AssistantAgent(
name="assistant",
model_client=OpenAIChatCompletionClient(
model="gpt-4.1",
),
tools=[
get_weather,
fahrenheit_to_celsius,
],
max_tool_iterations=10,
memory=[openai_memory],
)
# The memory will automatically retrieve relevant content during conversations
await Console(assistant.run_stream(task="What's the temperature in New York?"))
# Remember to close the memory when finished
await default_memory.close()
await custom_memory.close()
await openai_memory.close()
asyncio.run(main())
输出
---------- TextMessage (user) ----------
What's the temperature in New York?
---------- MemoryQueryEvent (assistant) ----------
[MemoryContent(content='The user prefers weather temperatures in Celsius', mime_type='MemoryMimeType.TEXT', metadata={'type': 'units', 'category': 'preferences', 'mime_type': 'MemoryMimeType.TEXT', 'score': 0.3133561611175537, 'id': 'fb00506c-acf4-4174-93d7-2a942593f3f7'}), MemoryContent(content='The user prefers weather temperatures in Celsius', mime_type='MemoryMimeType.TEXT', metadata={'mime_type': 'MemoryMimeType.TEXT', 'category': 'preferences', 'type': 'units', 'score': 0.3133561611175537, 'id': '34311689-b419-4e1a-8bc4-09143f356c66'})]
---------- ToolCallRequestEvent (assistant) ----------
[FunctionCall(id='call_7TjsFd430J1aKwU5T2w8bvdh', arguments='{"city":"New York"}', name='get_weather')]
---------- ToolCallExecutionEvent (assistant) ----------
[FunctionExecutionResult(content='The weather in New York is sunny with a high of 90°F and a low of 70°F.', name='get_weather', call_id='call_7TjsFd430J1aKwU5T2w8bvdh', is_error=False)]
---------- ToolCallRequestEvent (assistant) ----------
[FunctionCall(id='call_RTjMHEZwDXtjurEYTjDlvq9c', arguments='{"fahrenheit": 90}', name='fahrenheit_to_celsius'), FunctionCall(id='call_3mMuCK1aqtzZPTqIHPoHKxtP', arguments='{"fahrenheit": 70}', name='fahrenheit_to_celsius')]
---------- ToolCallExecutionEvent (assistant) ----------
[FunctionExecutionResult(content='32.22222222222222', name='fahrenheit_to_celsius', call_id='call_RTjMHEZwDXtjurEYTjDlvq9c', is_error=False), FunctionExecutionResult(content='21.11111111111111', name='fahrenheit_to_celsius', call_id='call_3mMuCK1aqtzZPTqIHPoHKxtP', is_error=False)]
---------- TextMessage (assistant) ----------
The temperature in New York today is sunny with a high of about 32°C and a low of about 21°C.
component_config_schema
ChromaDBVectorMemoryConfig 的别名
component_provider_override: ClassVar[str | None] = 'autogen_ext.memory.chromadb.ChromaDBVectorMemory'
覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。
property collection_name: str
获取 ChromaDB 集合的名称。
async update_context(model_context: ChatCompletionContext) → UpdateContextResult[源]
使用相关的记忆内容更新提供的模型上下文。
参数:
model_context – 要更新的上下文。
返回:
包含相关记忆的 UpdateContextResult
async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) → None[源]
向记忆中添加新内容。
参数:
-
content – 要添加的记忆内容
-
cancellation_token – 用于取消操作的可选令牌
async query(query: str | MemoryContent, cancellation_token: CancellationToken | None = None, **kwargs: Any) → MemoryQueryResult[源]
查询记忆存储并返回相关条目。
参数:
-
query – 查询内容项
-
cancellation_token – 用于取消操作的可选令牌
-
**kwargs – 其他特定于实现的参数
返回:
包含具有相关性分数的记忆条目的 MemoryQueryResult
清除内存中的所有条目。
清理 ChromaDB 客户端和资源。
pydantic model ChromaDBVectorMemoryConfig[源]
基类: BaseModel
ChromaDB 记忆实现的基本配置。
v0.4.1 版本中有所更改:添加了通过 embedding_function_config 支持自定义嵌入函数的功能。
显示 JSON 模式
{
"title": "ChromaDBVectorMemoryConfig",
"description": "Base configuration for ChromaDB-based memory implementation.\n\n.. versionchanged:: v0.4.1\n Added support for custom embedding functions via embedding_function_config.",
"type": "object",
"properties": {
"client_type": {
"enum": [
"persistent",
"http"
],
"title": "Client Type",
"type": "string"
},
"collection_name": {
"default": "memory_store",
"description": "Name of the ChromaDB collection",
"title": "Collection Name",
"type": "string"
},
"distance_metric": {
"default": "cosine",
"description": "Distance metric for similarity search",
"title": "Distance Metric",
"type": "string"
},
"k": {
"default": 3,
"description": "Number of results to return in queries",
"title": "K",
"type": "integer"
},
"score_threshold": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"description": "Minimum similarity score threshold",
"title": "Score Threshold"
},
"allow_reset": {
"default": false,
"description": "Whether to allow resetting the ChromaDB client",
"title": "Allow Reset",
"type": "boolean"
},
"tenant": {
"default": "default_tenant",
"description": "Tenant to use",
"title": "Tenant",
"type": "string"
},
"database": {
"default": "default_database",
"description": "Database to use",
"title": "Database",
"type": "string"
},
"embedding_function_config": {
"description": "Configuration for the embedding function",
"discriminator": {
"mapping": {
"default": "#/$defs/DefaultEmbeddingFunctionConfig",
"openai": "#/$defs/OpenAIEmbeddingFunctionConfig",
"sentence_transformer": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
"propertyName": "function_type"
},
"oneOf": [
{
"$ref": "#/$defs/DefaultEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/OpenAIEmbeddingFunctionConfig"
}
],
"title": "Embedding Function Config"
}
},
"$defs": {
"DefaultEmbeddingFunctionConfig": {
"description": "Configuration for the default ChromaDB embedding function.\n\nUses ChromaDB's default embedding function (Sentence Transformers all-MiniLM-L6-v2).\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.",
"properties": {
"function_type": {
"const": "default",
"default": "default",
"title": "Function Type",
"type": "string"
}
},
"title": "DefaultEmbeddingFunctionConfig",
"type": "object"
},
"OpenAIEmbeddingFunctionConfig": {
"description": "Configuration for OpenAI embedding functions.\n\nUses OpenAI's embedding API for generating embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n api_key (str): OpenAI API key. If empty, will attempt to use environment variable.\n model_name (str): OpenAI embedding model name. Defaults to \"text-embedding-ada-002\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import OpenAIEmbeddingFunctionConfig\n\n _ = OpenAIEmbeddingFunctionConfig(api_key=\"sk-...\", model_name=\"text-embedding-3-small\")",
"properties": {
"function_type": {
"const": "openai",
"default": "openai",
"title": "Function Type",
"type": "string"
},
"api_key": {
"default": "",
"description": "OpenAI API key",
"title": "Api Key",
"type": "string"
},
"model_name": {
"default": "text-embedding-ada-002",
"description": "OpenAI embedding model name",
"title": "Model Name",
"type": "string"
}
},
"title": "OpenAIEmbeddingFunctionConfig",
"type": "object"
},
"SentenceTransformerEmbeddingFunctionConfig": {
"description": "Configuration for SentenceTransformer embedding functions.\n\nAllows specifying a custom SentenceTransformer model for embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n model_name (str): Name of the SentenceTransformer model to use.\n Defaults to \"all-MiniLM-L6-v2\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import SentenceTransformerEmbeddingFunctionConfig\n\n _ = SentenceTransformerEmbeddingFunctionConfig(model_name=\"paraphrase-multilingual-mpnet-base-v2\")",
"properties": {
"function_type": {
"const": "sentence_transformer",
"default": "sentence_transformer",
"title": "Function Type",
"type": "string"
},
"model_name": {
"default": "all-MiniLM-L6-v2",
"description": "SentenceTransformer model name to use",
"title": "Model Name",
"type": "string"
}
},
"title": "SentenceTransformerEmbeddingFunctionConfig",
"type": "object"
}
},
"required": [
"client_type"
]
}
字段:
-
allow_reset (bool) -
client_type (Literal['persistent', 'http']) -
collection_name (str) -
database (str) -
distance_metric (str) -
embedding_function_config (autogen_ext.memory.chromadb._chroma_configs.DefaultEmbeddingFunctionConfig | autogen_ext.memory.chromadb._chroma_configs.SentenceTransformerEmbeddingFunctionConfig | autogen_ext.memory.chromadb._chroma_configs.OpenAIEmbeddingFunctionConfig | autogen_ext.memory.chromadb._chroma_configs.CustomEmbeddingFunctionConfig) -
k (int) -
score_threshold (float | None) -
tenant (str)
field client_type: Literal['persistent', 'http'] [必填]
field collection_name: str = 'memory_store'
ChromaDB 集合的名称
field distance_metric: str = 'cosine'
相似性搜索的距离度量
field k: int = 3
查询中返回的结果数量
field score_threshold: float | None = None
最小相似性分数阈值
field allow_reset: bool = False
是否允许重置 ChromaDB 客户端
field tenant: str = 'default_tenant'
要使用的租户
field database: str = 'default_database'
要使用的数据库
field embedding_function_config: Annotated[DefaultEmbeddingFunctionConfig | SentenceTransformerEmbeddingFunctionConfig | OpenAIEmbeddingFunctionConfig | CustomEmbeddingFunctionConfig, FieldInfo(annotation=NoneType, required=True, discriminator='function_type')] [可选]
嵌入函数的配置
pydantic model PersistentChromaDBVectorMemoryConfig[源]
持久化 ChromaDB 记忆的配置。
显示 JSON 模式
{
"title": "PersistentChromaDBVectorMemoryConfig",
"description": "Configuration for persistent ChromaDB memory.",
"type": "object",
"properties": {
"client_type": {
"default": "persistent",
"enum": [
"persistent",
"http"
],
"title": "Client Type",
"type": "string"
},
"collection_name": {
"default": "memory_store",
"description": "Name of the ChromaDB collection",
"title": "Collection Name",
"type": "string"
},
"distance_metric": {
"default": "cosine",
"description": "Distance metric for similarity search",
"title": "Distance Metric",
"type": "string"
},
"k": {
"default": 3,
"description": "Number of results to return in queries",
"title": "K",
"type": "integer"
},
"score_threshold": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"description": "Minimum similarity score threshold",
"title": "Score Threshold"
},
"allow_reset": {
"default": false,
"description": "Whether to allow resetting the ChromaDB client",
"title": "Allow Reset",
"type": "boolean"
},
"tenant": {
"default": "default_tenant",
"description": "Tenant to use",
"title": "Tenant",
"type": "string"
},
"database": {
"default": "default_database",
"description": "Database to use",
"title": "Database",
"type": "string"
},
"embedding_function_config": {
"description": "Configuration for the embedding function",
"discriminator": {
"mapping": {
"default": "#/$defs/DefaultEmbeddingFunctionConfig",
"openai": "#/$defs/OpenAIEmbeddingFunctionConfig",
"sentence_transformer": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
"propertyName": "function_type"
},
"oneOf": [
{
"$ref": "#/$defs/DefaultEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/OpenAIEmbeddingFunctionConfig"
}
],
"title": "Embedding Function Config"
},
"persistence_path": {
"default": "./chroma_db",
"description": "Path for persistent storage",
"title": "Persistence Path",
"type": "string"
}
},
"$defs": {
"DefaultEmbeddingFunctionConfig": {
"description": "Configuration for the default ChromaDB embedding function.\n\nUses ChromaDB's default embedding function (Sentence Transformers all-MiniLM-L6-v2).\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.",
"properties": {
"function_type": {
"const": "default",
"default": "default",
"title": "Function Type",
"type": "string"
}
},
"title": "DefaultEmbeddingFunctionConfig",
"type": "object"
},
"OpenAIEmbeddingFunctionConfig": {
"description": "Configuration for OpenAI embedding functions.\n\nUses OpenAI's embedding API for generating embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n api_key (str): OpenAI API key. If empty, will attempt to use environment variable.\n model_name (str): OpenAI embedding model name. Defaults to \"text-embedding-ada-002\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import OpenAIEmbeddingFunctionConfig\n\n _ = OpenAIEmbeddingFunctionConfig(api_key=\"sk-...\", model_name=\"text-embedding-3-small\")",
"properties": {
"function_type": {
"const": "openai",
"default": "openai",
"title": "Function Type",
"type": "string"
},
"api_key": {
"default": "",
"description": "OpenAI API key",
"title": "Api Key",
"type": "string"
},
"model_name": {
"default": "text-embedding-ada-002",
"description": "OpenAI embedding model name",
"title": "Model Name",
"type": "string"
}
},
"title": "OpenAIEmbeddingFunctionConfig",
"type": "object"
},
"SentenceTransformerEmbeddingFunctionConfig": {
"description": "Configuration for SentenceTransformer embedding functions.\n\nAllows specifying a custom SentenceTransformer model for embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n model_name (str): Name of the SentenceTransformer model to use.\n Defaults to \"all-MiniLM-L6-v2\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import SentenceTransformerEmbeddingFunctionConfig\n\n _ = SentenceTransformerEmbeddingFunctionConfig(model_name=\"paraphrase-multilingual-mpnet-base-v2\")",
"properties": {
"function_type": {
"const": "sentence_transformer",
"default": "sentence_transformer",
"title": "Function Type",
"type": "string"
},
"model_name": {
"default": "all-MiniLM-L6-v2",
"description": "SentenceTransformer model name to use",
"title": "Model Name",
"type": "string"
}
},
"title": "SentenceTransformerEmbeddingFunctionConfig",
"type": "object"
}
}
}
字段:
-
client_type (Literal['persistent', 'http']) -
persistence_path (str)
field client_type: Literal['persistent', 'http'] = 'persistent'
field persistence_path: str = './chroma_db'
持久化存储路径
pydantic model HttpChromaDBVectorMemoryConfig[源]
HTTP ChromaDB 记忆的配置。
显示 JSON 模式
{
"title": "HttpChromaDBVectorMemoryConfig",
"description": "Configuration for HTTP ChromaDB memory.",
"type": "object",
"properties": {
"client_type": {
"default": "http",
"enum": [
"persistent",
"http"
],
"title": "Client Type",
"type": "string"
},
"collection_name": {
"default": "memory_store",
"description": "Name of the ChromaDB collection",
"title": "Collection Name",
"type": "string"
},
"distance_metric": {
"default": "cosine",
"description": "Distance metric for similarity search",
"title": "Distance Metric",
"type": "string"
},
"k": {
"default": 3,
"description": "Number of results to return in queries",
"title": "K",
"type": "integer"
},
"score_threshold": {
"anyOf": [
{
"type": "number"
},
{
"type": "null"
}
],
"default": null,
"description": "Minimum similarity score threshold",
"title": "Score Threshold"
},
"allow_reset": {
"default": false,
"description": "Whether to allow resetting the ChromaDB client",
"title": "Allow Reset",
"type": "boolean"
},
"tenant": {
"default": "default_tenant",
"description": "Tenant to use",
"title": "Tenant",
"type": "string"
},
"database": {
"default": "default_database",
"description": "Database to use",
"title": "Database",
"type": "string"
},
"embedding_function_config": {
"description": "Configuration for the embedding function",
"discriminator": {
"mapping": {
"default": "#/$defs/DefaultEmbeddingFunctionConfig",
"openai": "#/$defs/OpenAIEmbeddingFunctionConfig",
"sentence_transformer": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
"propertyName": "function_type"
},
"oneOf": [
{
"$ref": "#/$defs/DefaultEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/SentenceTransformerEmbeddingFunctionConfig"
},
{
"$ref": "#/$defs/OpenAIEmbeddingFunctionConfig"
}
],
"title": "Embedding Function Config"
},
"host": {
"default": "localhost",
"description": "Host of the remote server",
"title": "Host",
"type": "string"
},
"port": {
"default": 8000,
"description": "Port of the remote server",
"title": "Port",
"type": "integer"
},
"ssl": {
"default": false,
"description": "Whether to use HTTPS",
"title": "Ssl",
"type": "boolean"
},
"headers": {
"anyOf": [
{
"additionalProperties": {
"type": "string"
},
"type": "object"
},
{
"type": "null"
}
],
"default": null,
"description": "Headers to send to the server",
"title": "Headers"
}
},
"$defs": {
"DefaultEmbeddingFunctionConfig": {
"description": "Configuration for the default ChromaDB embedding function.\n\nUses ChromaDB's default embedding function (Sentence Transformers all-MiniLM-L6-v2).\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.",
"properties": {
"function_type": {
"const": "default",
"default": "default",
"title": "Function Type",
"type": "string"
}
},
"title": "DefaultEmbeddingFunctionConfig",
"type": "object"
},
"OpenAIEmbeddingFunctionConfig": {
"description": "Configuration for OpenAI embedding functions.\n\nUses OpenAI's embedding API for generating embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n api_key (str): OpenAI API key. If empty, will attempt to use environment variable.\n model_name (str): OpenAI embedding model name. Defaults to \"text-embedding-ada-002\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import OpenAIEmbeddingFunctionConfig\n\n _ = OpenAIEmbeddingFunctionConfig(api_key=\"sk-...\", model_name=\"text-embedding-3-small\")",
"properties": {
"function_type": {
"const": "openai",
"default": "openai",
"title": "Function Type",
"type": "string"
},
"api_key": {
"default": "",
"description": "OpenAI API key",
"title": "Api Key",
"type": "string"
},
"model_name": {
"default": "text-embedding-ada-002",
"description": "OpenAI embedding model name",
"title": "Model Name",
"type": "string"
}
},
"title": "OpenAIEmbeddingFunctionConfig",
"type": "object"
},
"SentenceTransformerEmbeddingFunctionConfig": {
"description": "Configuration for SentenceTransformer embedding functions.\n\nAllows specifying a custom SentenceTransformer model for embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n model_name (str): Name of the SentenceTransformer model to use.\n Defaults to \"all-MiniLM-L6-v2\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import SentenceTransformerEmbeddingFunctionConfig\n\n _ = SentenceTransformerEmbeddingFunctionConfig(model_name=\"paraphrase-multilingual-mpnet-base-v2\")",
"properties": {
"function_type": {
"const": "sentence_transformer",
"default": "sentence_transformer",
"title": "Function Type",
"type": "string"
},
"model_name": {
"default": "all-MiniLM-L6-v2",
"description": "SentenceTransformer model name to use",
"title": "Model Name",
"type": "string"
}
},
"title": "SentenceTransformerEmbeddingFunctionConfig",
"type": "object"
}
}
}
字段:
-
client_type (Literal['persistent', 'http']) -
headers (Dict[str, str] | None) -
host (str) -
port (int) -
ssl (bool)
field client_type: Literal['persistent', 'http'] = 'http'
field host: str = 'localhost'
远程服务器的主机
field port: int = 8000
远程服务器的端口
field ssl: bool = False
是否使用 HTTPS
field headers: Dict[str, str] | None = None
发送到服务器的标头
pydantic model DefaultEmbeddingFunctionConfig[源]
基类: BaseModel
默认 ChromaDB 嵌入函数的配置。
使用 ChromaDB 的默认嵌入函数(Sentence Transformers all-MiniLM-L6-v2)。
v0.4.1 版本中新增:ChromaDB 记忆中对自定义嵌入函数的支持。
显示 JSON 模式
{
"title": "DefaultEmbeddingFunctionConfig",
"description": "Configuration for the default ChromaDB embedding function.\n\nUses ChromaDB's default embedding function (Sentence Transformers all-MiniLM-L6-v2).\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.",
"type": "object",
"properties": {
"function_type": {
"const": "default",
"default": "default",
"title": "Function Type",
"type": "string"
}
}
}
字段:
-
function_type (Literal['default'])
field function_type: Literal['default'] = 'default'
pydantic model SentenceTransformerEmbeddingFunctionConfig[源]
基类: BaseModel
SentenceTransformer 嵌入函数的配置。
允许为嵌入指定自定义 SentenceTransformer 模型。
v0.4.1 版本中新增:ChromaDB 记忆中对自定义嵌入函数的支持。
参数:
model_name (str) – 要使用的 SentenceTransformer 模型的名称。默认为“all-MiniLM-L6-v2”。
示例
from autogen_ext.memory.chromadb import SentenceTransformerEmbeddingFunctionConfig _ = SentenceTransformerEmbeddingFunctionConfig(model_name="paraphrase-multilingual-mpnet-base-v2")
显示 JSON 模式
{
"title": "SentenceTransformerEmbeddingFunctionConfig",
"description": "Configuration for SentenceTransformer embedding functions.\n\nAllows specifying a custom SentenceTransformer model for embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n model_name (str): Name of the SentenceTransformer model to use.\n Defaults to \"all-MiniLM-L6-v2\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import SentenceTransformerEmbeddingFunctionConfig\n\n _ = SentenceTransformerEmbeddingFunctionConfig(model_name=\"paraphrase-multilingual-mpnet-base-v2\")",
"type": "object",
"properties": {
"function_type": {
"const": "sentence_transformer",
"default": "sentence_transformer",
"title": "Function Type",
"type": "string"
},
"model_name": {
"default": "all-MiniLM-L6-v2",
"description": "SentenceTransformer model name to use",
"title": "Model Name",
"type": "string"
}
}
}
字段:
-
function_type (Literal['sentence_transformer']) -
model_name (str)
field function_type: Literal['sentence_transformer'] = 'sentence_transformer'
field model_name: str = 'all-MiniLM-L6-v2'
要使用的 SentenceTransformer 模型名称
pydantic model OpenAIEmbeddingFunctionConfig[源]
基类: BaseModel
OpenAI 嵌入函数的配置。
使用 OpenAI 的嵌入 API 生成嵌入。
v0.4.1 版本中新增:ChromaDB 记忆中对自定义嵌入函数的支持。
参数:
-
api_key (str) – OpenAI API 密钥。如果为空,将尝试使用环境变量。
-
model_name (str) – OpenAI 嵌入模型名称。默认为“text-embedding-ada-002”。
示例
from autogen_ext.memory.chromadb import OpenAIEmbeddingFunctionConfig _ = OpenAIEmbeddingFunctionConfig(api_key="sk-...", model_name="text-embedding-3-small")
显示 JSON 模式
{
"title": "OpenAIEmbeddingFunctionConfig",
"description": "Configuration for OpenAI embedding functions.\n\nUses OpenAI's embedding API for generating embeddings.\n\n.. versionadded:: v0.4.1\n Support for custom embedding functions in ChromaDB memory.\n\nArgs:\n api_key (str): OpenAI API key. If empty, will attempt to use environment variable.\n model_name (str): OpenAI embedding model name. Defaults to \"text-embedding-ada-002\".\n\nExample:\n .. code-block:: python\n\n from autogen_ext.memory.chromadb import OpenAIEmbeddingFunctionConfig\n\n _ = OpenAIEmbeddingFunctionConfig(api_key=\"sk-...\", model_name=\"text-embedding-3-small\")",
"type": "object",
"properties": {
"function_type": {
"const": "openai",
"default": "openai",
"title": "Function Type",
"type": "string"
},
"api_key": {
"default": "",
"description": "OpenAI API key",
"title": "Api Key",
"type": "string"
},
"model_name": {
"default": "text-embedding-ada-002",
"description": "OpenAI embedding model name",
"title": "Model Name",
"type": "string"
}
}
}
字段:
-
api_key (str) -
function_type (Literal['openai']) -
model_name (str)
field function_type: Literal['openai'] = 'openai'
field api_key: str = ''
OpenAI API 密钥
field model_name: str = 'text-embedding-ada-002'
OpenAI 嵌入模型名称
pydantic model CustomEmbeddingFunctionConfig[源]
基类: BaseModel
自定义嵌入函数的配置。
允许使用返回 ChromaDB 兼容嵌入函数的自定义函数。
v0.4.1 版本中新增:ChromaDB 记忆中对自定义嵌入函数的支持。
警告
包含自定义函数的配置不可序列化。
参数:
-
function (Callable) – 返回 ChromaDB 兼容嵌入函数的函数。
-
params (Dict[str, Any]) – 传递给函数的参数。
显示 JSON 模式
{
"title": "CustomEmbeddingFunctionConfig",
"type": "object",
"properties": {
"function_type": {
"const": "custom",
"default": "custom",
"title": "Function Type",
"type": "string"
},
"function": {
"default": null,
"title": "Function"
},
"params": {
"description": "Parameters to pass to the function",
"title": "Params",
"type": "object"
}
}
}
字段:
-
function (Callable[[...], Any]) -
function_type (Literal['custom']) -
params (Dict[str, Any])
field function_type: Literal['custom'] = 'custom'
field function: Callable[[...], Any] [必填]
返回嵌入函数的函数
field params: Dict[str, Any] [可选]
传递给函数的参数
《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能。《AI提示工程必知必会》主要内容包括各类提示词的应用,如问答式、指令式、状态类、建议式、安全类和感谢类提示词,以及如何通过实战演练掌握提示词的使用技巧;使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务,以及在数据挖掘、程序开发等领域的应用;AI在绘画创作上的应用,百度文心一言和阿里通义大模型这两大智能平台的特性与功能,以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》,读者可掌握如何有效利用AI提示工程提升工作效率,创新工作流程,并在职场中脱颖而出。

更多推荐


所有评论(0)