Engine 模块¶
Engine 模块涵盖 AgentScope Runtime 的核心执行、部署与服务能力,以下 API 参考与 agentscope_runtime.engine 目录结构保持一致,便于按模块查阅。
子模块¶
App¶
- class agentscope_runtime.engine.app.base_app.BaseApp(broker_url=None, backend_url=None)[source]
Bases:
CeleryMixinBaseApp integrates Celery for asynchronous background task execution, and provides FastAPI-like routing for task endpoints.
- __init__(broker_url=None, backend_url=None)[source]
- class agentscope_runtime.engine.app.agent_app.AgentApp(*, app_name='', app_description='', endpoint_path='/process', response_type='sse', stream=True, request_model=<class 'agentscope_runtime.engine.schemas.agent_schemas.AgentRequest'>, before_start=None, after_finish=None, broker_url=None, backend_url=None, runner=None, enable_embedded_worker=False, **kwargs)[source]
Bases:
BaseAppThe AgentApp class represents an application that runs as an agent.
- Parameters:
- __init__(*, app_name='', app_description='', endpoint_path='/process', response_type='sse', stream=True, request_model=<class 'agentscope_runtime.engine.schemas.agent_schemas.AgentRequest'>, before_start=None, after_finish=None, broker_url=None, backend_url=None, runner=None, enable_embedded_worker=False, **kwargs)[source]
Initialize the AgentApp.
- Parameters:
*args – Variable length argument list.
**kwargs – Arbitrary keyword arguments.
app_name (str)
app_description (str)
endpoint_path (str)
response_type (str)
stream (bool)
request_model (type[BaseModel] | None)
before_start (Callable | None)
after_finish (Callable | None)
broker_url (str | None)
backend_url (str | None)
runner (Runner | None)
enable_embedded_worker (bool)
- init(func)[source]
Register init hook (support async and sync functions).
- query(framework='agentscope')[source]
Register run hook and optionally specify agent framework. Allowed framework values: ‘agentscope’, ‘autogen’, ‘agno’, ‘langgraph’.
- Parameters:
framework (str | None)
- shutdown(func)[source]
Register shutdown hook (support async and sync functions).
- run(host='0.0.0.0', port=8080, web_ui=False, **kwargs)[source]
Launch the AgentApp HTTP API server.
This method starts a FastAPI server for the agent service. Optionally, it can also launch a browser-based Web UI for interacting with the agent.
Note: If web_ui=True and this is the first time launching the Web UI, additional time may be required to initialize. This happens because the underlying Node.js command (npx @agentscope-ai/chat agentscope-runtime-webui) might install dependencies and set up the runtime environment.
- Parameters:
host (str) – Host address to bind to. Default “0.0.0.0”.
port (int) – Port number to serve the application on. Default 8080.
web_ui (bool) – If True, launches the Agentscope Web UI in a separate process, pointing it to the API endpoint. This allows interactive use via browser. Default False.
**kwargs – Additional keyword arguments passed to FastAPIAppFactory when creating the FastAPI application.
Example
>>> app = AgentApp(app_name="MyAgent") >>> app.chat(host="127.0.0.1", port=8080, web_ui=True)
- get_fastapi_app(**kwargs)[source]
Get the FastAPI application
- async deploy(deployer, **kwargs)[source]
Deploy the agent app with custom endpoints support
- Parameters:
deployer (DeployManager)
- endpoint(path, methods=None)[source]
Decorator to register custom endpoints
- class agentscope_runtime.engine.app.celery_mixin.CeleryMixin(broker_url=None, backend_url=None)[source]
Bases:
objectCelery task processing mixin that provides core Celery functionality. Can be reused by BaseApp and FastAPIAppFactory.
- __init__(broker_url=None, backend_url=None)[source]
- register_celery_task(func, queue='celery')[source]
Register a Celery task for the given function.
- run_task_processor(loglevel='INFO', concurrency=None, queues=None)[source]
Run Celery worker in this process.
Runner¶
- class agentscope_runtime.engine.runner.Runner[source]
Bases:
object- __init__()[source]
Initializes a runner as core instance.
- Return type:
None
- async query_handler(*args, **kwargs)[source]
Handle agent query.
- async init_handler(*args, **kwargs)[source]
Init handler.
- async shutdown_handler(*args, **kwargs)[source]
Shutdown handler.
- async start()[source]
- async stop()[source]
- async __aenter__()[source]
Initializes the runner
- Return type:
Runner
- async deploy(deploy_manager=<agentscope_runtime.engine.deployers.local_deployer.LocalDeployManager object>, endpoint_path='/process', stream=True, protocol_adapters=None, requirements=None, extra_packages=None, base_image='python:3.9-slim', environment=None, runtime_config=None, **kwargs)[source]
Deploys the agent as a service.
- Parameters:
deploy_manager (DeployManager) – Deployment manager to handle service deployment
endpoint_path (str) – API endpoint path for the processing function
stream (bool) – If start a streaming service
protocol_adapters (list[ProtocolAdapter] | None) – protocol adapters
extra_packages (List[str] | None) – User code directory/file path
base_image (str) – Docker base image (for containerized deployment)
environment (Dict[str, str] | None) – Environment variables dict
runtime_config (Dict | None) – Runtime configuration dict
**kwargs – Additional arguments passed to deployment manager
- Returns:
URL of the deployed service
- Raises:
RuntimeError – If deployment fails
- async stream_query(request, **kwargs)[source]
Streams the agent.
- Parameters:
- Return type:
AsyncGenerator[Event, None]
Helpers¶
Agent Protocol Data Generator
Provides layered Builder pattern tools to generate streaming response data that conforms to types/agent definitions
- class agentscope_runtime.engine.helpers.agent_api_builder.ContentBuilder(message_builder, content_type='text', index=0)[source]
Bases:
objectContent Builder
Responsible for building and managing individual Content objects, supporting Text, Image, and Data content types
- __init__(message_builder, content_type='text', index=0)[source]
Initialize Content Builder
- add_text_delta(text)[source]
Add text delta (only applicable to text type)
- Parameters:
text (str) – Text fragment
- Returns:
Delta content object
- Return type:
TextContent
- set_text(text)[source]
Set complete text content (only applicable to text type)
- Parameters:
text (str) – Complete text content
- Returns:
Content object
- Return type:
TextContent
- set_refusal(text)[source]
Set complete refusal content (only applicable to refusal type)
- Parameters:
text (str) – Complete refusal content
- Returns:
Content object
- Return type:
RefusalContent
- set_image_url(image_url)[source]
Set image URL (only applicable to image type)
- Parameters:
image_url (str) – Image URL
- Returns:
Content object
- Return type:
ImageContent
- set_data(data)[source]
Set data content (only applicable to data type)
- update_data(key, value)[source]
Update specific fields of data content (only applicable to data type)
- add_data_delta(delta_data)[source]
Add data delta (only applicable to data type)
- add_delta(text)[source]
Add text delta (backward compatibility method)
- Parameters:
text (str) – Text fragment
- Returns:
Delta content object
- Return type:
TextContent
- complete()[source]
Complete content building
- Returns:
Dictionary representation of complete Content object
- Return type:
Message
- get_content_data()[source]
Get dictionary representation of current content
- Returns:
Content object
- Return type:
Content
- class agentscope_runtime.engine.helpers.agent_api_builder.MessageBuilder(response_builder, role='assistant')[source]
Bases:
objectMessage Builder
Responsible for building and managing individual Message objects and updating associated Response
- Parameters:
response_builder (ResponseBuilder)
role (str)
- __init__(response_builder, role='assistant')[source]
Initialize Message Builder
- Parameters:
response_builder (ResponseBuilder) – Associated ResponseBuilder object
role (str) – Message role, defaults to assistant
- create_content_builder(content_type='text')[source]
Create Content Builder
- Parameters:
content_type (str) – Content type (‘text’, ‘image’, ‘data’)
- Returns:
Newly created ContentBuilder instance
- Return type:
ContentBuilder
- add_content(content)[source]
Add content to message
- Parameters:
content (Content) – Content object
- get_message_data()[source]
Get dictionary representation of current message
- Returns:
Message object
- Return type:
Message
- complete()[source]
Complete message building
- Returns:
Dictionary representation of complete message object
- Return type:
Message
- class agentscope_runtime.engine.helpers.agent_api_builder.ResponseBuilder(session_id=None, response_id=None)[source]
Bases:
objectResponse Builder
Responsible for building and managing AgentResponse objects, coordinating MessageBuilder work
- __init__(session_id=None, response_id=None)[source]
Initialize Response Builder
- reset()[source]
Reset builder state, generate new ID and object instances
- get_response_data()[source]
Get dictionary representation of current response
- Returns:
Response object
- Return type:
AgentResponse
- created()[source]
Set response status to created
- Returns:
Response object
- Return type:
AgentResponse
- in_progress()[source]
Set response status to in_progress
- Returns:
Response object
- Return type:
AgentResponse
- completed()[source]
Set response status to completed
- Returns:
Response object
- Return type:
AgentResponse
- create_message_builder(role='assistant', message_type='message')[source]
Create Message Builder
- add_message(message)[source]
Add message to response output list
- Parameters:
message (Message) – Message object
- update_message(message)[source]
Update message in response
- Parameters:
message (Message) – Updated Message object
- generate_streaming_response(text_tokens, role='assistant')[source]
Generate complete streaming response sequence
- agentscope_runtime.engine.helpers.agent_api_builder.StreamingResponseBuilder
alias of
ResponseBuilder
常量¶
Deployers¶
- class agentscope_runtime.engine.deployers.base.DeployManager(state_manager=None)[source]
Bases:
ABC- __init__(state_manager=None)[source]
- abstract async deploy(*args, **kwargs)[source]
Deploy the service and return a dictionary with deploy_id and URL.
- abstract async stop(deploy_id, **kwargs)[source]
Stop the deployed service.
- Parameters:
deploy_id (str) – Deployment identifier
**kwargs – Platform-specific parameters (url, namespace, etc.)
- Returns:
success (bool): Whether stop succeeded
message (str): Status message
details (dict, optional): Platform-specific details
- Return type:
Dict with keys
- class agentscope_runtime.engine.deployers.local_deployer.LocalDeployManager(host='127.0.0.1', port=8090, shutdown_timeout=30, startup_timeout=30, logger=None)[source]
Bases:
DeployManagerUnified LocalDeployManager supporting multiple deployment modes.
- Parameters:
- __init__(host='127.0.0.1', port=8090, shutdown_timeout=30, startup_timeout=30, logger=None)[source]
Initialize LocalDeployManager.
- async deploy(app=None, runner=None, endpoint_path='/process', request_model=None, response_type='sse', stream=True, before_start=None, after_finish=None, mode=DeploymentMode.DAEMON_THREAD, custom_endpoints=None, protocol_adapters=None, broker_url=None, backend_url=None, enable_embedded_worker=False, project_dir=None, entrypoint=None, **kwargs)[source]
Deploy using unified FastAPI architecture.
- Parameters:
app – Agent app to be deployed
runner – Runner instance (for DAEMON_THREAD mode)
endpoint_path (str) – API endpoint path
request_model (Type | None) – Pydantic model for request validation
response_type (str) – Response type - “json”, “sse”, or “text”
stream (bool) – Enable streaming responses
before_start (Callable | None) – Callback function called before server starts
after_finish (Callable | None) – Callback function called after server finishes
mode (DeploymentMode) – Deployment mode
custom_endpoints (List[Dict] | None) – Custom endpoints from agent app
protocol_adapters (list[ProtocolAdapter] | None) – Protocol adapters
broker_url (str | None) – Celery broker URL for background task processing
backend_url (str | None) – Celery backend URL for result storage
enable_embedded_worker (bool) – Whether to run Celery worker embedded in the app
project_dir (str | None) – Project directory (for DETACHED_PROCESS mode)
entrypoint (str | None) – Entrypoint specification (for DETACHED_PROCESS mode)
**kwargs (Any) – Additional keyword arguments
- Returns:
Dict containing deploy_id and url
- Raises:
RuntimeError – If deployment fails
- Return type:
- async static create_detached_project(app=None, runner=None, entrypoint=None, endpoint_path='/process', requirements=None, extra_packages=None, protocol_adapters=None, custom_endpoints=None, broker_url=None, backend_url=None, enable_embedded_worker=False, platform='local', **kwargs)[source]
- async stop(deploy_id, **kwargs)[source]
Stop the FastAPI service.
- class agentscope_runtime.engine.deployers.kubernetes_deployer.K8sConfig(*, k8s_namespace='agentscope-runtime', kubeconfig_path=None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.kubernetes_deployer.BuildConfig(*, build_context_dir=None, dockerfile_template=None, build_timeout=600, push_timeout=300, cleanup_after_build=True)[source]
Bases:
BaseModelBuild configuration
- Parameters:
- dockerfile_template: str
- build_timeout: int
- push_timeout: int
- cleanup_after_build: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.kubernetes_deployer.KubernetesDeployManager(kube_config=None, registry_config=RegistryConfig(registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None), use_deployment=True, build_context_dir=None, state_manager=None)[source]
Bases:
DeployManagerKubernetes deployer for agent services
- Parameters:
- __init__(kube_config=None, registry_config=RegistryConfig(registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None), use_deployment=True, build_context_dir=None, state_manager=None)[source]
- static get_service_endpoint(service_external_ip, service_port, fallback_host='127.0.0.1')[source]
Auto-select appropriate service endpoint based on detected environment.
Solves the common issue where Kubernetes LoadBalancer/ExternalIP is not reachable from localhost in local clusters (e.g., Minikube/Kind).
- Parameters:
- Returns:
Full HTTP endpoint URL: http://<host>:<port>
- Return type:
Example
>>> endpoint = get_service_endpoint('192.168.5.1', 8080) >>> # In local env → 'http://127.0.0.1:8080' >>> # In cloud env → 'http://192.168.5.1:8080'
- async deploy(app=None, runner=None, entrypoint=None, endpoint_path='/process', stream=True, custom_endpoints=None, protocol_adapters=None, requirements=None, extra_packages=None, base_image='python:3.9-slim', environment=None, runtime_config=None, port=8090, replicas=1, mount_dir=None, image_name='agent_llm', image_tag='latest', push_to_registry=False, use_cache=True, **kwargs)[source]
Deploy runner to Kubernetes.
All temporary files are created in cwd/.agentscope_runtime/ by default.
- Parameters:
app – Agent app to be deployed
runner – Complete Runner object with agent, environment_manager, context_manager
entrypoint (str | None) – Entrypoint spec (e.g., “app.py” or “app.py:handler”)
endpoint_path (str) – API endpoint path
stream (bool) – Enable streaming responses
custom_endpoints (List[Dict] | None) – Custom endpoints from agent app
protocol_adapters (list[ProtocolAdapter] | None) – protocol adapters
requirements (str | None | List[str]) – PyPI dependencies (following _agent_engines.py pattern)
extra_packages (List[str] | None) – User code directory/file path
base_image (str) – Docker base image
port (int) – Container port
replicas (int) – Number of replicas
environment (Dict | None) – Environment variables dict
mount_dir (str | None) – Mount directory
runtime_config (Dict | None) – K8s runtime configuration
use_cache (bool) – Enable build cache (default: True)
compatibility (# Backward)
image_name (str) – Image name
image_tag (str) – Image tag
push_to_registry (bool) – Push to registry
**kwargs – Additional arguments
- Returns:
Dict containing deploy_id, url, resource_name, replicas
- Raises:
RuntimeError – If deployment fails
- Return type:
- async stop(deploy_id, **kwargs)[source]
Stop Kubernetes deployment.
- class agentscope_runtime.engine.deployers.modelstudio_deployer.OSSConfig(*, region='cn-hangzhou', access_key_id=None, access_key_secret=None, bucket_prefix='tmpbucket-agentscope-runtime')[source]
Bases:
BaseModel- Parameters:
- region: str
- bucket_prefix: str
- classmethod from_env()[source]
- Return type:
OSSConfig
- ensure_valid()[source]
- Return type:
None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.modelstudio_deployer.ModelstudioConfig(*, endpoint='bailian.cn-beijing.aliyuncs.com', workspace_id=None, access_key_id=None, access_key_secret=None, dashscope_api_key=None)[source]
Bases:
BaseModel- Parameters:
- endpoint: str
- classmethod from_env()[source]
- Return type:
ModelstudioConfig
- ensure_valid()[source]
- Return type:
None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.modelstudio_deployer.ModelstudioDeployManager(oss_config=None, modelstudio_config=None, build_root=None, state_manager=None)[source]
Bases:
DeployManagerDeployer for Alibaba Modelstudio Function Compute based agent deployment.
This deployer packages the user project into a wheel, uploads it to OSS, and triggers a Modelstudio Full-Code deploy.
- Parameters:
- __init__(oss_config=None, modelstudio_config=None, build_root=None, state_manager=None)[source]
- async deploy(app=None, runner=None, endpoint_path='/process', protocol_adapters=None, requirements=None, extra_packages=None, environment=None, project_dir=None, cmd=None, deploy_name=None, skip_upload=False, telemetry_enabled=True, external_whl_path=None, agent_id=None, agent_desc=None, custom_endpoints=None, **kwargs)[source]
Package the project, upload to OSS and trigger ModelStudio deploy.
Returns a dict containing deploy_id, wheel_path, url (if uploaded), resource_name (deploy_name), and workspace_id.
- async stop(deploy_id, **kwargs)[source]
Stop ModelStudio deployment.
Note: ModelStudio stop API not yet available.
- class agentscope_runtime.engine.deployers.agentrun_deployer.EndpointConfig(agent_runtime_endpoint_name=None, description=None, tags=None, target_version='LATEST')[source]
Bases:
objectConfiguration for agent runtime endpoint.
- Parameters:
- class agentscope_runtime.engine.deployers.agentrun_deployer.CodeConfig(command=None, oss_bucket_name=None, oss_object_name=None)[source]
Bases:
objectConfiguration for code-based runtimes.
- class agentscope_runtime.engine.deployers.agentrun_deployer.LogConfig(logstore=None, project=None)[source]
Bases:
objectConfiguration for logging.
- class agentscope_runtime.engine.deployers.agentrun_deployer.NetworkConfig(network_mode='PUBLIC', security_group_id=None, vpc_id=None, vswitch_ids=None)[source]
Bases:
objectNetwork configuration for the runtime.
- Parameters:
- network_mode: str = 'PUBLIC'
- class agentscope_runtime.engine.deployers.agentrun_deployer.AgentRunConfig(*, access_key_id=None, access_key_secret=None, region_id='cn-hangzhou', endpoint=None, log_config=None, network_config=NetworkConfig(network_mode='PUBLIC', security_group_id=None, vpc_id=None, vswitch_ids=None), cpu=2.0, memory=2048, execution_role_arn=None, session_concurrency_limit=200, session_idle_timeout_seconds=3600)[source]
Bases:
BaseModel- Parameters:
access_key_id (str | None)
access_key_secret (str | None)
region_id (str)
endpoint (str | None)
log_config (LogConfig | None)
network_config (NetworkConfig | None)
cpu (float)
memory (int)
execution_role_arn (str | None)
session_concurrency_limit (int | None)
session_idle_timeout_seconds (int | None)
- region_id: str
- log_config: LogConfig | None
- network_config: NetworkConfig | None
- cpu: float
- memory: int
- classmethod from_env()[source]
Create AgentRunConfig from environment variables.
- Returns:
Configuration loaded from environment variables.
- Return type:
AgentRunConfig
- ensure_valid()[source]
Validate that all required configuration fields are present.
- Raises:
ValueError – If required environment variables are missing.
- Return type:
None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.agentrun_deployer.OSSConfig(*, region='cn-hangzhou', access_key_id=None, access_key_secret=None, bucket_name)[source]
Bases:
BaseModel- Parameters:
- region: str
- bucket_name: str
- classmethod from_env()[source]
Create OSSConfig from environment variables.
- Returns:
Configuration loaded from environment variables.
- Return type:
OSSConfig
- ensure_valid()[source]
Validate that all required OSS configuration fields are present.
- Raises:
RuntimeError – If required AccessKey credentials are missing.
- Return type:
None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.agentrun_deployer.AgentRunDeployManager(oss_config=None, agentrun_config=None, build_root=None, state_manager=None)[source]
Bases:
DeployManagerManager for deploying agents to Alibaba Cloud AgentRun service.
This class handles the complete deployment workflow including: - Building and packaging agent projects - Uploading artifacts to OSS - Creating and managing agent runtimes - Creating and managing runtime endpoints
- Parameters:
- GET_AGENT_RUNTIME_STATUS_MAX_ATTEMPTS = 60
- GET_AGENT_RUNTIME_STATUS_INTERVAL = 1
- LATEST_VERSION = 'LATEST'
- DEFAULT_ENDPOINT_NAME = 'default-endpoint'
- __init__(oss_config=None, agentrun_config=None, build_root=None, state_manager=None)[source]
Initialize AgentRun deployment manager.
- Parameters:
oss_config (OSSConfig | None) – OSS configuration for artifact storage. If None, loads from environment.
agentrun_config (AgentRunConfig | None) – AgentRun service configuration. If None, loads from environment.
build_root (str | Path | None) – Root directory for build artifacts. If None, uses parent directory of current working directory.
state_manager – Deployment state manager. If None, creates a new instance.
- async deploy(runner=None, endpoint_path='/process', protocol_adapters=None, requirements=None, extra_packages=None, environment=None, project_dir=None, cmd=None, deploy_name=None, skip_upload=False, external_whl_path=None, agentrun_id=None, custom_endpoints=None, app=None, **kwargs)[source]
Deploy agent to AgentRun service.
- Parameters:
app – AgentApp instance to deploy.
runner – Runner instance containing the agent to deploy.
endpoint_path (str) – HTTP endpoint path for the agent service.
protocol_adapters (list[ProtocolAdapter] | None) – List of protocol adapters for the agent.
requirements (str | None | List[str]) – Python requirements for the agent (file path or list).
extra_packages (List[str] | None) – Additional Python packages to install.
environment (Dict[str, str] | None) – Environment variables for the runtime.
project_dir (str | Path | None) – Project directory to deploy.
cmd (str | None) – Command to start the agent application.
deploy_name (str | None) – Name for this deployment.
skip_upload (bool) – If True, skip uploading to AgentRun service.
external_whl_path (str | None) – Path to pre-built wheel file.
agentrun_id (str | None) – ID of existing agent to update.
custom_endpoints (List[Dict] | None) – Custom endpoints for the agent.
**kwargs – Additional deployment parameters.
- Returns:
success: Whether deployment succeeded
message: Status message
agentrun_id: ID of the agent runtime
agentrun_endpoint_url: Public endpoint URL
build_dir: Build directory path
wheel_path: Path to wheel file
artifact_url: OSS artifact URL
url: Console URL for the deployment
deploy_id: Deployment ID
resource_name: Resource name
- Return type:
Dictionary containing deployment results with keys
- Raises:
ValueError – If required parameters are missing.
FileNotFoundError – If specified files/directories don’t exist.
- async deploy_to_agentrun(agent_runtime_name, oss_bucket_name, oss_object_name, agentrun_id=None, environment=None)[source]
Deploy agent runtime and endpoint to AgentRun service.
- Parameters:
agent_runtime_name (str) – Name for the agent runtime.
oss_bucket_name (str) – OSS bucket containing the code artifact.
oss_object_name (str) – Object key of the code artifact in OSS.
agentrun_id (str | None) – AgentRun ID to update deploy to.
environment (Dict[str, str] | None) – Environment variables for the runtime.
- Returns:
success: Whether deployment succeeded
agent_runtime_id: ID of the created runtime
agent_runtime_endpoint_id: ID of the created endpoint
agent_runtime_endpoint_name: Name of the endpoint
agent_runtime_public_endpoint_url: Public URL of the endpoint
status: Status of the deployment
request_id: Request ID for tracking
- Return type:
Dictionary containing deployment results
- async delete(agent_runtime_id)[source]
Delete an agent runtime on AgentRun.
- Parameters:
agent_runtime_id (str) – The ID of the agent runtime to delete.
- Returns:
- A dictionary containing the delete result with:
success (bool): Whether the operation was successful
message (str): Status message
agent_runtime_id (str): The ID of the deleted agent runtime
status (str): The status of the agent runtime
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async get_agent_runtime(agent_runtime_id, agent_runtime_version=None)[source]
Get agent runtime details.
- Parameters:
- Returns:
- A dictionary containing the agent runtime details with:
success (bool): Whether the operation was successful
data (dict): The agent runtime data
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async create_agent_runtime(agent_runtime_name, artifact_type, cpu, memory, port, code_configuration=None, description=None, environment_variables=None, execution_role_arn=None, log_configuration=None, network_configuration=None, session_concurrency_limit_per_instance=None, session_idle_timeout_seconds=None)[source]
Create an agent runtime on AgentRun.
- Parameters:
agent_runtime_name (str) – The name of the agent runtime.
artifact_type (str) – The type of the artifact.
cpu (float) – The CPU allocated to the runtime.
memory (int) – The memory allocated to the runtime.
port (int) – The port on which the runtime will listen.
code_configuration (Optional[CodeConfig]) – Configuration for code-based runtimes.
description (Optional[str]) – Description of the agent runtime.
environment_variables (Optional[Dict[str, str]]) – Environment variables for the runtime.
execution_role_arn (Optional[str]) – The execution role ARN for accessing cloud services.
log_configuration (Optional[LogConfig]) – Configuration for logging.
network_configuration (Optional[NetworkConfig]) – Network configuration for the runtime, including: - network_mode: The network mode for the runtime - security_group_id: The security group ID for the runtime - vpc_id: The VPC ID for the runtime - vswitch_ids: List of vswitch IDs for the runtime
session_concurrency_limit_per_instance (Optional[int]) – Maximum concurrent sessions per instance.
session_idle_timeout_seconds (Optional[int]) – Maximum idle timeout for sessions.
- Returns:
- A dictionary containing the creation result with:
success (bool): Whether the operation was successful
agent_runtime_id (str): The ID of the created agent runtime
status (str): The status of the agent runtime
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async update_agent_runtime(agent_runtime_id, agent_runtime_name=None, artifact_type=None, cpu=None, memory=None, port=None, code_configuration=None, description=None, environment_variables=None, execution_role_arn=None, log_configuration=None, network_configuration=None, session_concurrency_limit_per_instance=None, session_idle_timeout_seconds=None)[source]
Update an agent runtime on AgentRun.
- Parameters:
agent_runtime_id (str) – The ID of the agent runtime to update.
agent_runtime_name (Optional[str]) – The name of the agent runtime.
artifact_type (Optional[str]) – The type of the artifact.
cpu (Optional[float]) – The CPU allocated to the runtime.
memory (Optional[int]) – The memory allocated to the runtime.
port (Optional[int]) – The port on which the runtime will listen.
code_configuration (Optional[CodeConfig]) – Configuration for code-based runtimes.
description (Optional[str]) – Description of the agent runtime.
environment_variables (Optional[Dict[str, str]]) – Environment variables for the runtime.
execution_role_arn (Optional[str]) – The execution role ARN for accessing cloud services.
log_configuration (Optional[LogConfig]) – Configuration for logging.
network_configuration (Optional[NetworkConfig]) – Network configuration for the runtime, including: - network_mode: The network mode for the runtime - security_group_id: The security group ID for the runtime - vpc_id: The VPC ID for the runtime - vswitch_ids: List of vswitch IDs for the runtime
session_concurrency_limit_per_instance (Optional[int]) – Maximum concurrent sessions per instance.
session_idle_timeout_seconds (Optional[int]) – Maximum idle timeout for sessions.
- Returns:
- A dictionary containing the update result with:
success (bool): Whether the operation was successful
agent_runtime_id (str): The ID of the updated agent runtime
status (str): The status of the agent runtime
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async create_agent_runtime_endpoint(agent_runtime_id, endpoint_config=None)[source]
Create an agent runtime endpoint.
- Parameters:
agent_runtime_id (str) – The ID of the agent runtime.
endpoint_config (Optional[EndpointConfig]) – Configuration for the endpoint, including: - agent_runtime_endpoint_name: The name of the endpoint - description: Description of the endpoint - target_version: Target version for the endpoint
- Returns:
- A dictionary containing the creation result with:
success (bool): Whether the operation was successful
agent_runtime_endpoint_id (str): The ID of the created endpoint
agent_runtime_endpoint_name (str): The name of the created endpoint
agent_runtime_public_endpoint_url (str): The public URL of the endpoint
status (str): The status of the endpoint
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async update_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id, endpoint_config=None)[source]
Update an agent runtime endpoint.
- Parameters:
agent_runtime_id (str) – The ID of the agent runtime.
agent_runtime_endpoint_id (str) – The ID of the agent runtime endpoint.
endpoint_config (Optional[EndpointConfig]) – Configuration for the endpoint, including: - agent_runtime_endpoint_name: The name of the endpoint - description: Description of the endpoint - target_version: Target version for the endpoint
- Returns:
- A dictionary containing the update result with:
success (bool): Whether the operation was successful
agent_runtime_endpoint_id (str): The ID of the updated endpoint
status (str): The status of the endpoint
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async get_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id)[source]
Get an agent runtime endpoint.
- Parameters:
- Returns:
- A dictionary containing the endpoint details with:
success (bool): Whether the operation was successful
agent_runtime_endpoint_id (str): The ID of the endpoint
agent_runtime_endpoint_name (str): The name of the endpoint
agent_runtime_id (str): The ID of the agent runtime
agent_runtime_public_endpoint_url (str): The public URL of the endpoint
status (str): The status of the endpoint
status_reason (str): The reason for the status
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async delete_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id)[source]
Delete an agent runtime endpoint.
- Parameters:
- Returns:
- A dictionary containing the delete result with:
success (bool): Whether the operation was successful
message (str): Status message
agent_runtime_endpoint_id (str): The ID of the deleted endpoint
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- async publish_agent_runtime_version(agent_runtime_id, description=None)[source]
Publish an agent runtime version.
- Parameters:
- Returns:
- A dictionary containing the publish result with:
success (bool): Whether the operation was successful
agent_runtime_id (str): The ID of the agent runtime
agent_runtime_version (str): The published version
description (str): Description of the version
request_id (str): The request ID for tracking
- Return type:
Dict[str, Any]
- agentscope_runtime.engine.deployers.cli_fc_deploy.main()[source]
- Return type:
None
Deployers · Adapter¶
- class agentscope_runtime.engine.deployers.adapter.protocol_adapter.ProtocolAdapter(**kwargs)[source]
Bases:
ABC- __init__(**kwargs)[source]
- abstract add_endpoint(app, func, **kwargs)[source]
Add an endpoint to the protocol adapter.
This method should be implemented by subclasses to provide protocol-specific endpoint addition functionality.
- Parameters:
*args – Variable length argument list for endpoint configuration
**kwargs – Arbitrary keyword arguments for endpoint configuration
func (Callable)
- Returns:
The result of adding the endpoint, implementation-dependent
- Return type:
Any
- class agentscope_runtime.engine.deployers.adapter.a2a.a2a_agent_adapter.A2AExecutor(func, **kwargs)[source]
Bases:
AgentExecutor- Parameters:
func (Callable)
- async execute(context, event_queue)[source]
Execute the agent’s logic for a given request context.
The agent should read necessary information from the context and publish Task or Message events, or TaskStatusUpdateEvent / TaskArtifactUpdateEvent to the event_queue. This method should return once the agent’s execution for this request is complete or yields control (e.g., enters an input-required state).
- Parameters:
context (RequestContext) – The request context containing the message, task ID, etc.
event_queue (EventQueue) – The queue to publish events to.
- Return type:
None
- async cancel(context, event_queue)[source]
Request the agent to cancel an ongoing task.
The agent should attempt to stop the task identified by the task_id in the context and publish a TaskStatusUpdateEvent with state TaskState.canceled to the event_queue.
- Parameters:
context (RequestContext) – The request context containing the task ID to cancel.
event_queue (EventQueue) – The queue to publish the cancellation status update to.
- Return type:
None
- class agentscope_runtime.engine.deployers.adapter.a2a.a2a_protocol_adapter.A2AFastAPIDefaultAdapter(agent_name, agent_description, **kwargs)[source]
Bases:
ProtocolAdapter- __init__(agent_name, agent_description, **kwargs)[source]
- add_endpoint(app, func, **kwargs)[source]
Add an endpoint to the protocol adapter.
This method should be implemented by subclasses to provide protocol-specific endpoint addition functionality.
- Parameters:
*args – Variable length argument list for endpoint configuration
**kwargs – Arbitrary keyword arguments for endpoint configuration
func (Callable)
- Returns:
The result of adding the endpoint, implementation-dependent
- Return type:
Any
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_message_to_agent_message(msg)[source]
Convert A2A Message object to AgentAPI Message object
- Parameters:
msg (A2AMessage) – A2A protocol message object
- Returns:
Converted internal Agent API message object
- Return type:
AgentMessage
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_part_to_agent_content(part)[source]
Convert A2A protocol Part object to internal Content object
- Parameters:
part (Part) – A2A protocol part object
- Returns:
Converted internal content object
- Return type:
Content
- Raises:
ValueError – If the part type is unknown
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_sendparams_to_agent_request(params, stream, context_id=None)[source]
Convert a2a MessageSendParams to agent-api AgentRequest
- Parameters:
- Returns:
Converted agent request object
- Return type:
AgentRequest
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_taskqueryparams_to_agent_request(params, session_id=None)[source]
Convert TaskQueryParams to AgentRequest, only set session_id Other fields are controlled by AgentRequest default values
- Parameters:
params (TaskQueryParams) – Task query parameters from a2a protocol
session_id (str, optional) – Session ID for the request
- Returns:
Converted agent request object with only session_id set
- Return type:
AgentRequest
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_content_to_a2a_part(content)[source]
Convert internal Content object to A2A protocol Part object
- Parameters:
content (Content) – Internal content object
- Returns:
Converted A2A protocol part object
- Return type:
Part
- Raises:
ValueError – If the content type is unknown
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_message_to_a2a_artifact(msg)[source]
Convert AgentAPI Message to a2a Artifact
- Parameters:
msg (AgentMessage) – Agent API message object
- Returns:
Converted A2A protocol artifact object
- Return type:
Artifact
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.runstatus_to_a2a_taskstate(status)[source]
Map Internal RunStatus to a2a TaskState
- Parameters:
status (str) – Internal run status string
- Returns:
Mapped A2A task state
- Return type:
TaskState
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_response_to_a2a_task(resp)[source]
Convert AgentResponse object to a2a Task object.
- Parameters:
resp (AgentResponse) – Internal agent response object
- Returns:
Converted A2A protocol task object
- Return type:
Task
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.response_to_task_status_update_event(response)[source]
Convert AgentResponse (internal response) to a2a TaskStatusUpdateEvent.
- Parameters:
response (AgentResponse) – Internal agent response object
- Returns:
Converted A2A protocol task status update event
- Return type:
TaskStatusUpdateEvent
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.content_to_task_artifact_update_event(content, context_id='', task_id=None, append=False, last_chunk=False)[source]
Convert single Content (TextContent/ImageContent/DataContent) to TaskArtifactUpdateEvent. If delta=false, should not return task_artifact_update_event
- Parameters:
content (Content) – SSE returned content, including delta and non-delta types
context_id (str) – Corresponds to agent api sessionId, needs external input
task_id (str | None) – Currently equivalent to msg_id, or not passed
append (bool) – Used to determine if current artifact is new or first
last_chunk (bool) – Used to determine if current content is the last one
- Returns:
Converted A2A protocol task artifact update event
- Return type:
TaskArtifactUpdateEvent
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_role_to_a2a_role(role)[source]
- Parameters:
role (str)
- agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_message_to_a2a_message(msg)[source]
Convert AgentAPI Message object to a2a protocol Message object
- Parameters:
msg (AgentAPIMessage) – Agent API message object
- Returns:
Converted A2A protocol message object
- Return type:
A2AMessage
- class agentscope_runtime.engine.deployers.adapter.responses.response_api_agent_adapter.ResponseAPIExecutor(func, **kwargs)[source]
Bases:
object- Parameters:
func (Callable)
- class agentscope_runtime.engine.deployers.adapter.responses.response_api_protocol_adapter.ResponseAPIDefaultAdapter(**kwargs)[source]
Bases:
ProtocolAdapter- __init__(**kwargs)[source]
- Return type:
None
Responses Adapter
Bidirectional protocol converter: Responses API ↔ Agent API
Conversion functions: 1. Responses API request → Agent API request 2. Agent API event → Responses API event 3. Support for streaming and non-streaming conversion
- class agentscope_runtime.engine.deployers.adapter.responses.response_api_adapter_utils.ResponsesAdapter[source]
Bases:
objectBidirectional protocol converter: Responses API ↔ Agent API
Main functions: 1. Convert Responses API request → Agent API request 2. Convert Agent API event → Responses API event 3. Convert Responses API event stream → Agent API event stream 4. Handle various message types (text, tool calls, reasoning, etc.)
- __init__()[source]
- Return type:
None
- convert_agent_response_to_responses(agent_response)[source]
- Parameters:
agent_response (BaseResponse)
- convert_status_to_responses(agent_status)[source]
- convert_responses_request_to_agent_request(responses_request)[source]
Convert Responses API request to Agent API request
Implement automatic assignment of fields with the same name, then explicitly handle different field names
- convert_agent_event_to_responses_event(agent_event)[source]
Convert Agent API event to Responses API stream event
- Parameters:
agent_event (Event) – Agent API Event
- Returns:
ResponseStreamEvent or None
- Return type:
List[Annotated[ResponseAudioDeltaEvent | ResponseAudioDoneEvent | ResponseAudioTranscriptDeltaEvent | ResponseAudioTranscriptDoneEvent | ResponseCodeInterpreterCallCodeDeltaEvent | ResponseCodeInterpreterCallCodeDoneEvent | ResponseCodeInterpreterCallCompletedEvent | ResponseCodeInterpreterCallInProgressEvent | ResponseCodeInterpreterCallInterpretingEvent | ResponseCompletedEvent | ResponseContentPartAddedEvent | ResponseContentPartDoneEvent | ResponseCreatedEvent | ResponseErrorEvent | ResponseFileSearchCallCompletedEvent | ResponseFileSearchCallInProgressEvent | ResponseFileSearchCallSearchingEvent | ResponseFunctionCallArgumentsDeltaEvent | ResponseFunctionCallArgumentsDoneEvent | ResponseInProgressEvent | ResponseFailedEvent | ResponseIncompleteEvent | ResponseOutputItemAddedEvent | ResponseOutputItemDoneEvent | ResponseReasoningSummaryPartAddedEvent | ResponseReasoningSummaryPartDoneEvent | ResponseReasoningSummaryTextDeltaEvent | ResponseReasoningSummaryTextDoneEvent | ResponseReasoningTextDeltaEvent | ResponseReasoningTextDoneEvent | ResponseRefusalDeltaEvent | ResponseRefusalDoneEvent | ResponseTextDeltaEvent | ResponseTextDoneEvent | ResponseWebSearchCallCompletedEvent | ResponseWebSearchCallInProgressEvent | ResponseWebSearchCallSearchingEvent | ResponseImageGenCallCompletedEvent | ResponseImageGenCallGeneratingEvent | ResponseImageGenCallInProgressEvent | ResponseImageGenCallPartialImageEvent | ResponseMcpCallArgumentsDeltaEvent | ResponseMcpCallArgumentsDoneEvent | ResponseMcpCallCompletedEvent | ResponseMcpCallFailedEvent | ResponseMcpCallInProgressEvent | ResponseMcpListToolsCompletedEvent | ResponseMcpListToolsFailedEvent | ResponseMcpListToolsInProgressEvent | ResponseOutputTextAnnotationAddedEvent | ResponseQueuedEvent | ResponseCustomToolCallInputDeltaEvent | ResponseCustomToolCallInputDoneEvent, PropertyInfo(alias=’None’, format=None, format_template=’None’, discriminator=’type’)]] | None
Deployers · Utils¶
Helpers for working with AgentApp objects inside deployers.
- agentscope_runtime.engine.deployers.utils.app_runner_utils.ensure_runner_from_app(app)[source]
Return a Runner extracted from an AgentApp instance.
Builds the runner lazily if the app hasn’t initialized it yet.
- Parameters:
app (Any)
- Return type:
Optional[‘Runner’]
Deployment modes and configuration for unified FastAPI architecture.
- class agentscope_runtime.engine.deployers.utils.deployment_modes.DeploymentMode(value)[source]
-
FastAPI application deployment modes.
- DAEMON_THREAD = 'daemon_thread'
- DETACHED_PROCESS = 'detached_process'
- STANDALONE = 'standalone'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
Shared helpers for building detached deployment bundles.
- agentscope_runtime.engine.deployers.utils.detached_app.build_detached_app(*, app=None, runner=None, entrypoint=None, requirements=None, extra_packages=None, output_dir=None, dockerfile_path=None, use_local_runtime=None, platform='unknown', **kwargs)[source]
Create a detached bundle directory ready for execution.
All temporary files are created in cwd/.agentscope_runtime/ by default.
- Parameters:
app – AgentApp instance to deploy
runner – Runner instance to deploy
entrypoint (str | None) – Entrypoint specification (e.g., “app.py” or “app.py:handler”)
requirements (str | None | List[str]) – Additional pip requirements (string or list)
extra_packages (List[str] | None) – Additional Python packages to include
output_dir (str | None) – Output directory (creates temp dir if None)
dockerfile_path (str | None) – Optional custom Dockerfile path to include
use_local_runtime (bool | None) – If True, build and include local runtime wheel. If None (default), auto-detect based on version. Useful for development when runtime is not released.
platform (str)
- Returns:
Tuple of (project_root_path, project_info)
- Return type:
- agentscope_runtime.engine.deployers.utils.detached_app.append_project_requirements(extraction_dir, additional_requirements, use_local_runtime=False)[source]
Append requirements to requirements.txt.
For dev versions or when use_local_runtime=True, builds a wheel from local source and places it in wheels/ subdirectory.
- Parameters:
- Return type:
None
- agentscope_runtime.engine.deployers.utils.detached_app.get_bundle_entry_script(bundle_dir)[source]
Project-based packaging utilities for AgentApp and Runner deployment.
This module provides packaging utilities that support: - Function-based AgentApp deployment with decorators - Runner-based deployment with entrypoint files - Entire project directory packaging - Smart dependency caching - CLI-style and object-style deployment patterns
- agentscope_runtime.engine.deployers.utils.package.generate_build_directory(platform='unknown', workspace=None)[source]
Generate a platform-aware build directory with timestamp and random suffix.
- Parameters:
- Returns:
Generated build directory path
- Return type:
Path
Example
>>> build_dir = generate_build_directory("modelstudio") >>> # Returns: .agentscope_runtime/builds/modelstudio_20251207_xxx
- class agentscope_runtime.engine.deployers.utils.package.RuntimeParameter(*, name, type, default, help=None, cli_name=None)[source]
Bases:
BaseModelConfiguration for a runtime parameter.
- Parameters:
- name: str
- type: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.package.EntrypointInfo(*, module_name, object_name, object_type, host='0.0.0.0', port=8090, extra_parameters=[])[source]
Bases:
BaseModelInformation about the generated entrypoint.
- Parameters:
- module_name: str
- object_name: str
- object_type: str
- host: str
- port: int
- extra_parameters: List[RuntimeParameter]
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.package.ProjectInfo(*, project_dir, entrypoint_file, entrypoint_handler, handler_type=None, is_directory_entrypoint=False)[source]
Bases:
BaseModelInformation about a project to be packaged.
- Parameters:
- project_dir: str
- entrypoint_file: str
- entrypoint_handler: str
- is_directory_entrypoint: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- agentscope_runtime.engine.deployers.utils.package.project_dir_extractor(app=None, runner=None)[source]
Extract project directory information from app or runner object.
This function inspects the call stack to find where the app or runner was defined and extracts the project root directory and object name.
- Parameters:
app – AgentApp instance (optional)
runner – Runner instance (optional)
- Returns:
ProjectInfo with project directory, entrypoint file, and handler name
- Raises:
ValueError – If neither app nor runner is provided or project dir
cannot be determined –
- Return type:
ProjectInfo
- agentscope_runtime.engine.deployers.utils.package.parse_entrypoint(spec)[source]
Parse entrypoint specification into ProjectInfo.
Supported formats: - “app.py” - File with default handler name “app” - “app.py:my_handler” - File with specific handler name - “project_dir/” - Directory (will auto-detect entrypoint)
- Parameters:
spec (str) – Entrypoint specification string
- Returns:
ProjectInfo with parsed information
- Raises:
ValueError – If specification format is invalid or file/dir doesn’t
exist –
- Return type:
ProjectInfo
- agentscope_runtime.engine.deployers.utils.package.generate_main_template(entrypoint_info)[source]
Generate main.py template based on object type using Jinja2 templates.
- Parameters:
entrypoint_info (EntrypointInfo) – Information about the entrypoint
- Returns:
String content for main.py
- Raises:
ValueError – If object_type is not supported
RuntimeError – If template rendering fails
- Return type:
- agentscope_runtime.engine.deployers.utils.package.package_code(source_dir, output_zip, ignore_patterns=None)[source]
Package project source code into a zip file.
- agentscope_runtime.engine.deployers.utils.package.package(app=None, runner=None, entrypoint=None, output_dir=None, host='0.0.0.0', port=8090, extra_parameters=None, requirements=None, platform='unknown', **kwargs)[source]
Package an AgentApp or Runner for deployment.
This function supports two deployment patterns: 1. Object-style: package(app=my_app) or package(runner=my_runner) 2. Entrypoint-style: package(entrypoint=”app.py”) or package( entrypoint=”project_dir/”)
For object-style deployment, this function will: 1. Extract the project directory containing the app/runner 2. Generate a new main.py that imports and runs the app/runner 3. Package the project with the generated main.py as entrypoint
Build directory naming: - When output_dir=None (default), creates workspace directory with
platform-aware naming
- Directory format: cwd/.agentscope_runtime/builds/<platform>_
<timestamp>_<code>/
Explicit output_dir uses the provided path
- Parameters:
app – AgentApp instance (for object-style deployment)
runner – Runner instance (for object-style deployment)
entrypoint (str | None) – Entrypoint specification (for CLI-style deployment)
output_dir (str | None) – Output directory (creates temp dir if None)
host (str) – Default host for the service (default: “0.0.0.0”)
port (int) – Default port for the service (default: 8090)
extra_parameters (List[RuntimeParameter] | None) – Additional runtime parameters to expose via CLI
requirements (List[str] | None) – Additional pip requirements
platform (str) – Deployment platform (k8s, modelstudio, agentrun, local)
**kwargs – Additional keyword arguments (ignored)
- Returns:
Tuple of (package_path, project_info) - package_path: Path to the deployment package directory - project_info: ProjectInfo with project metadata
- Raises:
ValueError – If neither app/runner nor entrypoint is provided
RuntimeError – If packaging fails
- Return type:
Example
>>> # Package with extra parameters >>> extra_params = [ ... RuntimeParameter( ... name="log_level", ... type="str", ... default="info", ... help="Logging level" ... ), ... RuntimeParameter( ... name="workers", ... type="int", ... default=4, ... help="Number of worker threads" ... ), ... ] >>> package(app=my_app, extra_parameters=extra_params, platform="k8s")
Utilities for packaging a local Python project into a distributable wheel that can be uploaded and deployed by various deployers.
This module extracts and generalizes logic from the legacy test script tests/integrated/test_bailian_fc_deploy/deploy_builder.py so that production deployers can reuse the behaviour in a structured way.
- agentscope_runtime.engine.deployers.utils.wheel_packager.get_user_bundle_appdir(build_root, user_project_dir)[source]
- agentscope_runtime.engine.deployers.utils.wheel_packager.generate_wrapper_project(build_root, user_project_dir, start_cmd, deploy_name, telemetry_enabled=True, requirements=None)[source]
Create a wrapper project under build_root, embedding user project under user_bundle/<project_basename>. Returns: (wrapper_project_dir, dist_dir)
- agentscope_runtime.engine.deployers.utils.wheel_packager.build_wheel(project_dir)[source]
Build a wheel inside an isolated virtual environment to avoid PEP 668 issues. Returns the path to the built wheel.
- agentscope_runtime.engine.deployers.utils.wheel_packager.default_deploy_name()[source]
- Return type:
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.RegistryConfig(*, registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None)[source]
Bases:
BaseModelContainer registry configuration
- Parameters:
- registry_url: str
- username: str
- password: str
- image_pull_secret: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.BuildConfig(*, no_cache=False, quiet=False, build_args={}, platform=None, target=None, source_updated=False)[source]
Bases:
BaseModelConfiguration for Docker image building
- Parameters:
- no_cache: bool
- quiet: bool
- source_updated: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.DockerImageBuilder[source]
Bases:
objectResponsible solely for building and managing Docker images. Separated from project packaging for better separation of concerns.
- __init__()[source]
Initialize Docker image builder.
- static get_full_name(image_name, image_tag='latest')[source]
- build_image(build_context, image_name, image_tag='latest', dockerfile_path=None, config=None, source_updated=False)[source]
Build Docker image from build context.
- Parameters:
build_context (str) – Path to build context directory
image_name (str) – Name for the Docker image
image_tag (str) – Tag for the Docker image
dockerfile_path (str | None) – Optional path to Dockerfile (defaults to Dockerfile in context)
config (BuildConfig | None) – Build configuration
source_updated (bool) – Optional flag to determine if source image should be updated.
- Returns:
Full image name with tag
- Return type:
- Raises:
subprocess.CalledProcessError – If docker build fails
ValueError – If build context doesn’t exist
- tag_image(image_name, registry_config=None)[source]
Tag image with registry info.
- Parameters:
image_name (str) – Full image name to push
registry_config (RegistryConfig | None) – Optional registry config (uses instance config if None)
- Returns:
Full image name with url
- Return type:
registry_image_name
- push_image(image_name, registry_config=None, quiet=False)[source]
Push image to registry.
- Parameters:
- Returns:
Full image name that was pushed
- Return type:
- Raises:
subprocess.CalledProcessError – If push fails
ValueError – If no registry configuration is available
- build_and_push(build_context, image_name, image_tag='latest', dockerfile_path=None, build_config=None, registry_config=None, source_updated=False)[source]
Build and push image in one operation.
- Parameters:
build_context (str) – Path to build context directory
image_name (str) – Name for the Docker image
image_tag (str) – Tag for the Docker image
dockerfile_path (str | None) – Optional path to Dockerfile
build_config (BuildConfig | None) – Build configuration
registry_config (RegistryConfig | None) – Registry configuration
source_updated (bool) – Whether the source image was updated or not
- Returns:
Full registry image name
- Return type:
- remove_image(image_name, force=False, quiet=True)[source]
Remove Docker image.
- get_image_info(image_name)[source]
Get information about a Docker image.
- Parameters:
image_name (str) – Name of the Docker image
- Returns:
Image information from docker inspect
- Return type:
Dict
- Raises:
ValueError – If image not found or info invalid
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.dockerfile_generator.DockerfileConfig(*, base_image='python:3.10-slim-bookworm', port=8000, working_dir='/app', user='appuser', additional_packages=[], env_vars={}, startup_command=None, health_check_endpoint='/health', custom_template=None, platform=None)[source]
Bases:
BaseModelConfiguration for Dockerfile generation
- Parameters:
- base_image: str
- port: int
- working_dir: str
- user: str
- health_check_endpoint: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.dockerfile_generator.DockerfileGenerator[source]
Bases:
objectResponsible for generating Dockerfiles from templates. Separated from image building for better modularity.
- DEFAULT_TEMPLATE = '# Use official Python runtime as base image\nFROM --platform={platform} {base_image}\n\n# Set working directory in container\nWORKDIR /app\n\n# Set environment variables\nENV PYTHONDONTWRITEBYTECODE=1\nENV PYTHONUNBUFFERED=1\n\n# Configure package sources for better performance\nRUN rm -f /etc/apt/sources.list.d/*.list\n\n# add aliyun mirrors\nRUN echo "deb https://mirrors.aliyun.com/debian/ bookworm main contrib " "non-free non-free-firmware" > /etc/apt/sources.list && echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main " "contrib non-free non-free-firmware" >> /etc/apt/sources.list && echo "deb https://mirrors.aliyun.com/debian-security/ " "bookworm-security main contrib non-free " "non-free-firmware" >> /etc/apt/sources.list\n\n# replace debian to aliyun\nRUN mkdir -p /etc/apt/sources.list.d && cat > /etc/apt/sources.list.d/debian.sources <<\'EOF\'\nEOF\n\n# Clean up package lists\nRUN rm -rf /var/lib/apt/lists/*\n\n# Install system dependencies\nRUN apt-get update && apt-get install -y \\\n gcc \\\n curl \\\n{additional_packages_section} && rm -rf /var/lib/apt/lists/*\n\n# Copy project files\nCOPY . {working_dir}/\n\n# Install Python dependencies\nRUN pip install --no-cache-dir --upgrade pip\nRUN if [ -f requirements.txt ]; then \\\n pip install --no-cache-dir -r requirements.txt \\\n -i https://pypi.tuna.tsinghua.edu.cn/simple; fi\n\n# Create non-root user for security\nRUN adduser --disabled-password --gecos \'\' {user} && \\\n chown -R {user} {working_dir}\nUSER {user}\n\n{env_vars_section}\n# Expose port\nEXPOSE {port}\n\n# Health check\nHEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \\\n CMD curl -f http://localhost:{port}{health_check_endpoint} || exit 1\n\n# Command to run the application\n{startup_command_section}'
- __init__()[source]
- Return type:
None
- generate_dockerfile_content(config)[source]
Generate Dockerfile content from configuration.
- Parameters:
config (DockerfileConfig) – Dockerfile configuration
- Returns:
Generated Dockerfile content
- Return type:
- create_dockerfile(config, output_dir=None)[source]
Create Dockerfile in specified directory.
- validate_config(config)[source]
Validate Dockerfile configuration.
- Parameters:
config (DockerfileConfig) – Configuration to validate
- Returns:
True if valid
- Return type:
- Raises:
ValueError – If configuration is invalid
- cleanup()[source]
Clean up temporary files
- __enter__()[source]
Context manager entry
- __exit__(exc_type, exc_val, exc_tb)[source]
Context manager exit with cleanup
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.image_factory.ImageConfig(*, requirements=None, extra_packages=None, build_context_dir=None, endpoint_path='/process', protocol_adapters=None, custom_endpoints=None, base_image='python:3.10-slim-bookworm', port=8000, env_vars=<factory>, startup_command=None, host='0.0.0.0', embed_task_processor=False, extra_startup_args=<factory>, no_cache=False, quiet=False, build_args={}, platform=None, image_name=('agent', ), image_tag=None, registry_config=None, push_to_registry=False)[source]
Bases:
BaseModelComplete configuration for building a Runner image
- Parameters:
build_context_dir (str | None)
endpoint_path (str)
protocol_adapters (List | None)
base_image (str)
port (int)
startup_command (str | None)
host (str)
embed_task_processor (bool)
no_cache (bool)
quiet (bool)
platform (str | None)
image_name (str | None)
image_tag (str | None)
registry_config (RegistryConfig | None)
push_to_registry (bool)
- endpoint_path: str
- base_image: str
- port: int
- host: str
- embed_task_processor: bool
- no_cache: bool
- quiet: bool
- registry_config: RegistryConfig | None
- push_to_registry: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.deployers.utils.docker_image_utils.image_factory.ImageFactory[source]
Bases:
objectFactory class for building Runner Docker images. Coordinates ProjectPackager, DockerfileGenerator, and DockerImageBuilder.
- __init__()[source]
Initialize the Runner image factory.
- build_image(app=None, runner=None, entrypoint=None, requirements=None, extra_packages=None, base_image='python:3.10-slim-bookworm', image_name='agent', image_tag=None, registry_config=None, push_to_registry=False, protocol_adapters=None, custom_endpoints=None, host='0.0.0.0', embed_task_processor=True, extra_startup_args=None, use_cache=True, **kwargs)[source]
Simplified interface for building Runner images.
All temporary files are created in cwd/.agentscope_runtime/ by default.
- Parameters:
app – agent app object
runner (Runner | None) – Runner object
entrypoint (str | None) – Entrypoint specification (e.g., “app.py” or “app.py:handler”)
extra_packages (List[str] | None) – Additional files to include
base_image (str) – Docker base image
image_name (str) – Docker image name
image_tag (str | None) – Optional image tag
registry_config (RegistryConfig | None) – Optional registry config
push_to_registry (bool) – Whether to push to registry
protocol_adapters (List | None) – Protocol adapters
custom_endpoints (List[Dict] | None) – Custom endpoints from agent app
host (str) – Host to bind to (default: 0.0.0.0 for containers)
embed_task_processor (bool) – Whether to embed task processor
extra_startup_args (Dict[str, str | int | bool] | None) – Additional startup arguments
use_cache (bool) – Enable build cache (default: True)
**kwargs – Additional configuration options
- Returns:
Built image name
- Return type:
- cleanup()[source]
Clean up all temporary resources
- __enter__()[source]
Context manager entry
- __exit__(exc_type, exc_val, exc_tb)[source]
Context manager exit with cleanup
- async agentscope_runtime.engine.deployers.utils.service_utils.fastapi_factory.error_stream(e)[source]
- class agentscope_runtime.engine.deployers.utils.service_utils.fastapi_factory.FastAPIAppFactory[source]
Bases:
objectFactory for creating FastAPI applications with unified architecture.
- static create_app(func=None, runner=None, endpoint_path='/process', request_model=None, response_type='sse', stream=True, before_start=None, after_finish=None, mode=DeploymentMode.DAEMON_THREAD, protocol_adapters=None, custom_endpoints=None, broker_url=None, backend_url=None, enable_embedded_worker=False, app_kwargs=None, **kwargs)[source]
Create a FastAPI application with unified architecture.
- Parameters:
func (Callable | None) – Custom processing function
runner (Any | None) – Runner instance (for DAEMON_THREAD mode)
endpoint_path (str) – API endpoint path for the processing function
request_model (Type | None) – Pydantic model for request validation
response_type (str) – Response type - “json”, “sse”, or “text”
stream (bool) – Enable streaming responses
before_start (Callable | None) – Callback function called before server starts
after_finish (Callable | None) – Callback function called after server finishes
mode (DeploymentMode) – Deployment mode
protocol_adapters (list[ProtocolAdapter] | None) – Protocol adapters
custom_endpoints (List[Dict] | None) – List of custom endpoint configurations
broker_url (str | None) – Celery broker URL
backend_url (str | None) – Celery backend URL
enable_embedded_worker (bool) – Whether to run embedded Celery worker
app_kwargs (Dict | None) – Additional keyword arguments for the FastAPI app
**kwargs (Any) – Additional keyword arguments
- Returns:
FastAPI application instance
- Return type:
FastAPI
FastAPI templates management and rendering.
- class agentscope_runtime.engine.deployers.utils.service_utils.fastapi_templates.FastAPITemplateManager[source]
Bases:
objectManager for FastAPI deployment templates.
- __init__()[source]
Initialize template manager.
- render_standalone_template(agent_name, endpoint_path='/process', deployment_mode=DeploymentMode.STANDALONE, protocol_adapters=None, **kwargs)[source]
Render the standalone deployment template.
- Parameters:
- Returns:
Rendered template content
- Return type:
- render_detached_script_template(endpoint_path='/process', host='127.0.0.1', port=8000, stream_enabled=True, response_type='sse', runner_code='', func_code='', protocol_adapters=None, **kwargs)[source]
Render the detached process script template.
- Parameters:
endpoint_path (str) – API endpoint path
host (str) – Host to bind to
port (int) – Port to bind to
stream_enabled (bool) – Enable streaming responses
response_type (str) – Response type
runner_code (str) – Code to setup runner
func_code (str) – Code to setup custom function
protocol_adapters (str | None) – Protocol adapters code string
**kwargs – Additional template variables
- Returns:
Rendered template content
- Return type:
- render_template_from_string(template_string, **variables)[source]
Render a template from string.
- get_template_list()[source]
Get list of available templates.
- Returns:
List of template filenames
- Return type:
- class agentscope_runtime.engine.deployers.utils.service_utils.process_manager.ProcessManager(shutdown_timeout=30)[source]
Bases:
objectManager for detached process lifecycle.
- Parameters:
shutdown_timeout (int)
- __init__(shutdown_timeout=30)[source]
Initialize process manager.
- Parameters:
shutdown_timeout (int) – Timeout in seconds for graceful shutdown
- async start_detached_process(script_path, host='127.0.0.1', port=8000, env=None)[source]
Start a detached process running the given script.
- async stop_process_gracefully(pid, timeout=None)[source]
Stop a process gracefully.
- Parameters:
- Returns:
True if process was stopped successfully
- Raises:
RuntimeError – If process termination fails
- Return type:
- is_process_running(pid)[source]
Check if a process is running.
- create_pid_file(pid, file_path)[source]
Create a PID file.
- read_pid_file(file_path)[source]
Read PID from file.
- async find_process_by_port(port)[source]
Find process listening on a specific port.
- get_process_info(pid)[source]
Get information about a process.
- async wait_for_port(host, port, timeout=30)[source]
Wait for a service to become available on a port.
- get_process_logs(max_lines=50)[source]
Get the last N lines of process logs.
Services · 核心¶
- class agentscope_runtime.engine.services.base.Service[source]
Bases:
ABCAbstract base class for services.
This class defines the interface that all services must implement.
- abstract async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- abstract async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
- class agentscope_runtime.engine.services.base.ServiceLifecycleManagerMixin[source]
Bases:
objectMixin class that provides async lifecycle manager functionality for services.
This mixin can be used with any class that implements the Service interface.
- async __aenter__()[source]
Async context manager entry.
- async __aexit__(exc_type, exc_val, exc_tb)[source]
Async context manager exit.
- class agentscope_runtime.engine.services.base.ServiceWithLifecycleManager[source]
Bases:
Service,ServiceLifecycleManagerMixinBase class for services that want async lifecycle manager functionality.
This class combines the Service interface with the context manager mixin, providing a convenient base class for most service implementations.
Note: This is an abstract base class. Subclasses must implement the abstract methods from the Service class.
- abstract async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- abstract async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
Services · Agent State¶
- class agentscope_runtime.engine.services.agent_state.state_service.StateService[source]
Bases:
ServiceWithLifecycleManagerAbstract base class for agent state management services.
Stores and manages agent states organized by user_id, session_id, and round_id. Supports saving, retrieving, listing, and deleting states.
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
- abstract async save_state(user_id, state, session_id=None, round_id=None)[source]
Save serialized state data for a specific user/session.
If round_id is provided, store the state in that round. If round_id is None, append as a new round with automatically assigned round_id.
- Parameters:
- Returns:
The round_id in which the state was saved.
- Return type:
- abstract async export_state(user_id, session_id=None, round_id=None)[source]
Retrieve serialized state data for a user/session.
If round_id is provided, return that round’s state. If round_id is None, return the latest round’s state.
- class agentscope_runtime.engine.services.agent_state.state_service.InMemoryStateService[source]
Bases:
StateServiceIn-memory implementation of StateService using dictionaries for sparse round storage.
Multiple users, sessions, and non-contiguous round IDs are supported.
If round_id is None when saving, a new round is appended automatically.
If round_id is None when exporting, the latest round is returned.
- __init__()[source]
- Return type:
None
- async start()[source]
Initialize the in-memory store.
- Return type:
None
- async stop()[source]
Clear all in-memory state data.
- Return type:
None
- async save_state(user_id, state, session_id=None, round_id=None)[source]
Save serialized state in sparse dict storage.
If round_id is None, a new round_id will be assigned as (max existing round_id + 1) or 1 if none exist. Otherwise, the given round_id will be overwritten.
- async export_state(user_id, session_id=None, round_id=None)[source]
Retrieve state data for given user/session/round.
If round_id is None: return the latest round. If round_id is provided: return that round’s state.
- class agentscope_runtime.engine.services.agent_state.redis_state_service.RedisStateService(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Bases:
StateServiceRedis-based implementation of StateService.
Stores agent states in Redis using a hash per (user_id, session_id), with round_id as the hash field and serialized state as the value.
- Parameters:
redis_url (str)
redis_client (Redis | None)
- __init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
- Parameters:
redis_url (str)
redis_client (Redis | None)
- async start()[source]
Initialize the Redis connection.
- Return type:
None
- async stop()[source]
Close the Redis connection.
- Return type:
None
- async save_state(user_id, state, session_id=None, round_id=None)[source]
Save serialized state data for a specific user/session.
If round_id is provided, store the state in that round. If round_id is None, append as a new round with automatically assigned round_id.
- Parameters:
- Returns:
The round_id in which the state was saved.
- Return type:
- async export_state(user_id, session_id=None, round_id=None)[source]
Retrieve serialized state data for a user/session.
If round_id is provided, return that round’s state. If round_id is None, return the latest round’s state.
Services · Memory¶
- class agentscope_runtime.engine.services.memory.memory_service.MemoryService[source]
Bases:
ServiceWithLifecycleManagerUsed to store and retrieve long memory from the database or in-memory. The memory is organized by the user id at top level, under which there are two different memory manage strategies, - one is the message grouped by the session id, the session id is under the user id, - the other is the message grouped by the user id only
- abstract async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the memory service.
- async stop()[source]
Stops the service, releasing any acquired resources.
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- abstract async search_memory(user_id, messages, filters=FieldInfo(annotation=NoneType, required=False, default=None, description='Associated filters for the messages, such as top_k, score etc.'))[source]
Searches messages from the memory service.
- Parameters:
- Return type:
- abstract async list_memory(user_id, filters=FieldInfo(annotation=NoneType, required=False, default=None, description='Associated filters for the messages, such as top_k, score etc.'))[source]
Lists the memory items for a given user with filters, such as page_num, page_size, etc.
- class agentscope_runtime.engine.services.memory.memory_service.InMemoryMemoryService[source]
Bases:
MemoryServiceAn in-memory implementation of the memory service.
- __init__()[source]
Initializes the InMemorySessionHistoryService.
- Return type:
None
- async start()[source]
Initialize the in-memory store.
- Return type:
None
- async stop()[source]
Stops the service.
- Return type:
None
- async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the in-memory store.
- async search_memory(user_id, messages, filters=None)[source]
- Searches messages from the in-memory store for a specific user
based on keywords.
- Parameters:
- Returns:
A list of matching messages from the store.
- Return type:
- async get_query_text(message)[source]
Gets the query text from the messages.
- Parameters:
message (Message) – A list of messages.
- Returns:
The query text.
- Return type:
- async list_memory(user_id, filters=None)[source]
Lists messages from the in-memory store with pagination support.
- async delete_memory(user_id, session_id=None)[source]
Deletes messages from the in-memory store.
- class agentscope_runtime.engine.services.memory.mem0_memory_service.Mem0MemoryService(**kwargs)[source]
Bases:
MemoryServiceMemory service that uses mem0 to store and retrieve memories. To get the api key, please refer to the following link: https://docs.mem0.ai/platform/quickstart
- __init__(**kwargs)[source]
- async static get_query_text(message)[source]
Get the query text from the message.
- Parameters:
message (Message)
- Return type:
- async transform_messages(messages)[source]
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- async stop()[source]
Stops the service, releasing any acquired resources.
- async health()[source]
Checks the health of the service.
- Returns:
True if the service is healthy, False otherwise.
- async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the memory service.
- async search_memory(user_id, messages, filters=None)[source]
Searches messages from the memory service.
- Parameters:
- Return type:
- async list_memory(user_id, filters=None)[source]
Lists the memory items for a given user with filters, such as page_num, page_size, etc.
- class agentscope_runtime.engine.services.memory.redis_memory_service.RedisMemoryService(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Bases:
MemoryServiceA Redis-based implementation of the memory service.
- Parameters:
redis_url (str)
redis_client (Redis | None)
- __init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
- Parameters:
redis_url (str)
redis_client (Redis | None)
- async start()[source]
Starts the Redis connection.
- Return type:
None
- async stop()[source]
Closes the Redis connection.
- Return type:
None
- async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the memory service.
- async search_memory(user_id, messages, filters=None)[source]
Searches messages from the memory service.
- Parameters:
- Return type:
- async list_memory(user_id, filters=None)[source]
Lists the memory items for a given user with filters, such as page_num, page_size, etc.
- async delete_memory(user_id, session_id=None)[source]
Deletes the memory items for a given user with certain session id, or all the memory items for a given user.
- async clear_all_memory()[source]
Clears all memory data from Redis. This method removes all user memory keys from the Redis database.
- Return type:
None
- class agentscope_runtime.engine.services.memory.tablestore_memory_service.SearchStrategy(value)[source]
Bases:
EnumAn enumeration.
- FULL_TEXT = 'full_text'
- VECTOR = 'vector'
- class agentscope_runtime.engine.services.memory.tablestore_memory_service.TablestoreMemoryService(tablestore_client, search_strategy=SearchStrategy.FULL_TEXT, embedding_model=None, vector_dimension=1536, table_name='agentscope_runtime_memory', search_index_schema=({'field_name': 'user_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}, {'field_name': 'session_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}), text_field='text', embedding_field='embedding', vector_metric_type=VectorMetricType.VM_COSINE, **kwargs)[source]
Bases:
MemoryServiceA Tablestore-based implementation of the memory service. based on tablestore_for_agent_memory (https://github.com/aliyun/ alibabacloud-tablestore-for-agent-memory/blob/main/python/docs/knowledge_store_tutorial.ipynb).
- Parameters:
tablestore_client (AsyncOTSClient)
search_strategy (SearchStrategy)
embedding_model (Embeddings | None)
vector_dimension (int)
table_name (str | None)
search_index_schema (List[FieldSchema] | None)
text_field (str | None)
embedding_field (str | None)
vector_metric_type (VectorMetricType)
kwargs (Any)
- __init__(tablestore_client, search_strategy=SearchStrategy.FULL_TEXT, embedding_model=None, vector_dimension=1536, table_name='agentscope_runtime_memory', search_index_schema=({'field_name': 'user_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}, {'field_name': 'session_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}), text_field='text', embedding_field='embedding', vector_metric_type=VectorMetricType.VM_COSINE, **kwargs)[source]
- Parameters:
tablestore_client (AsyncOTSClient)
search_strategy (SearchStrategy)
embedding_model (Embeddings | None)
vector_dimension (int)
table_name (str | None)
search_index_schema (List[FieldSchema] | None)
text_field (str | None)
embedding_field (str | None)
vector_metric_type (VectorMetricType)
kwargs (Any)
- async start()[source]
Start the tablestore service
- Return type:
None
- async stop()[source]
Close the tablestore service
- Return type:
None
- async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the memory service.
- async search_memory(user_id, messages, filters=None)[source]
Searches messages from the memory service.
- Parameters:
- Return type:
- async list_memory(user_id, filters=None)[source]
Lists the memory items for a given user with filters, such as page_num, page_size, etc.
- class agentscope_runtime.engine.services.memory.reme_personal_memory_service.ReMePersonalMemoryService(**kwargs)[source]
Bases:
MemoryServiceReMe requires the following env variables to be set: FLOW_EMBEDDING_API_KEY=sk-xxxx FLOW_EMBEDDING_BASE_URL=https://xxxx/v1 FLOW_LLM_API_KEY=sk-xxxx FLOW_LLM_BASE_URL=https://xxxx/v1
- __init__(**kwargs)[source]
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
- async health()[source]
Checks the health of the service.
- Returns:
True if the service is healthy, False otherwise.
- Return type:
- async add_memory(user_id, messages, session_id=None)[source]
Adds messages to the memory service.
- async search_memory(user_id, messages, filters=None)[source]
Searches messages from the memory service.
- Parameters:
- Return type:
- async list_memory(user_id, filters=None)[source]
Lists the memory items for a given user with filters, such as page_num, page_size, etc.
Services · Session History¶
- class agentscope_runtime.engine.services.session_history.session_history_service.SessionHistoryService[source]
Bases:
ServiceWithLifecycleManagerAbstract base class for session history management services.
This class defines the standard interface for creating, retrieving, updating, and deleting conversation sessions. Concrete implementations (like InMemorySessionHistoryService) will handle the actual storage logic.
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
- abstract async create_session(user_id, session_id=None)[source]
Creates a new session for a given user.
- abstract async get_session(user_id, session_id)[source]
Retrieves a specific session.
- abstract async delete_session(user_id, session_id)[source]
Deletes a specific session.
- abstract async list_sessions(user_id)[source]
Lists all sessions for a given user.
- async append_message(session, message)[source]
Appends a message to the history of a specific session.
- class agentscope_runtime.engine.services.session_history.session_history_service.InMemorySessionHistoryService[source]
Bases:
SessionHistoryServiceAn in-memory implementation of the SessionHistoryService.
This service stores all session data in a dictionary, making it suitable for development, testing, and scenarios where persistence is not required.
- _store
A dictionary holding all session objects, keyed by user ID and then by session ID.
- __init__()[source]
Initializes the InMemorySessionHistoryService.
- Return type:
None
- async start()[source]
Initialize the in-memory store.
- Return type:
None
- async stop()[source]
Clear all in-memory data.
- Return type:
None
- async create_session(user_id, session_id=None)[source]
Creates a new session for a given user and stores it.
- async get_session(user_id, session_id)[source]
Retrieves a specific session from memory.
- async delete_session(user_id, session_id)[source]
Deletes a specific session from memory.
If the session does not exist, the method does nothing.
- async list_sessions(user_id)[source]
Lists all sessions for a given user.
To improve performance and reduce data transfer, the returned session objects do not contain the detailed response history.
- async append_message(session, message)[source]
Appends message to a session’s history in memory.
This method finds the authoritative session object in the in-memory storage and appends the message to its history. It supports both dictionary format messages and Message objects.
- class agentscope_runtime.engine.services.session_history.redis_session_history_service.RedisSessionHistoryService(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Bases:
SessionHistoryService- Parameters:
redis_url (str)
redis_client (Redis | None)
- __init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
- Parameters:
redis_url (str)
redis_client (Redis | None)
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- async stop()[source]
Stops the service, releasing any acquired resources.
- async health()[source]
Checks the health of the service.
- Returns:
True if the service is healthy, False otherwise.
- Return type:
- async create_session(user_id, session_id=None)[source]
Creates a new session for a given user.
- async get_session(user_id, session_id)[source]
Retrieves a specific session.
- async delete_session(user_id, session_id)[source]
Deletes a specific session.
- async list_sessions(user_id)[source]
Lists all sessions for a given user.
- async append_message(session, message)[source]
Appends a message to the history of a specific session.
- class agentscope_runtime.engine.services.session_history.tablestore_session_history_service.TablestoreSessionHistoryService(tablestore_client, session_table_name='agentscope_runtime_session', message_table_name='agentscope_runtime_message', session_secondary_index_meta=None, session_search_index_schema=None, message_search_index_schema=None, **kwargs)[source]
Bases:
SessionHistoryServiceAn aliyun tablestore implementation of the SessionHistoryService based on tablestore_for_agent_memory (https://github.com/aliyun/ alibabacloud-tablestore-for-agent-memory/blob/main/python/docs/memory_store_tutorial.ipynb).
- Parameters:
- __init__(tablestore_client, session_table_name='agentscope_runtime_session', message_table_name='agentscope_runtime_message', session_secondary_index_meta=None, session_search_index_schema=None, message_search_index_schema=None, **kwargs)[source]
Initializes the TablestoreSessionHistoryService.
- Parameters:
- Return type:
None
- async start()[source]
Start the tablestore service
- Return type:
None
- async stop()[source]
Close the tablestore service
- Return type:
None
- async create_session(user_id, session_id=None)[source]
Creates a new session for a given user and stores it.
- async get_session(user_id, session_id)[source]
Retrieves a specific session from memory.
- async delete_session(user_id, session_id)[source]
Deletes a specific session from memory.
If the session does not exist, the method does nothing.
- async list_sessions(user_id)[source]
Lists all sessions for a given user.
To improve performance and reduce data transfer, the returned session objects do not contain the detailed response history.
- async append_message(session, message)[source]
Appends message to a session’s history in memory.
This method finds the authoritative session object in the in-memory storage and appends the message to its history. It supports both dictionary format messages and Message objects.
Services · Sandbox¶
- class agentscope_runtime.engine.services.sandbox.sandbox_service.SandboxService(base_url=None, bearer_token=None)[source]
Bases:
ServiceWithLifecycleManager- __init__(base_url=None, bearer_token=None)[source]
- async start()[source]
Starts the service, initializing any necessary resources or connections.
- Return type:
None
- async stop()[source]
Stops the service, releasing any acquired resources.
- Return type:
None
- async health()[source]
Checks the health of the service.
- Returns:
True if the service is healthy, False otherwise.
- Return type:
- connect(session_id, user_id=None, sandbox_types=None)[source]
- release(session_id, user_id=None)[source]
Services · Utils¶
- agentscope_runtime.engine.services.utils.tablestore_service_utils.create_tablestore_client(end_point, access_key_id, access_key_secret, instance_name, sts_token=None, region=None, credentials_provider=None, retry_policy=<tablestore.retry.WriteRetryPolicy object>, **kwargs)[source]
- agentscope_runtime.engine.services.utils.tablestore_service_utils.exclude_None_fields_in_place(obj)[source]
Remove fields with None values from dictionary in-place
- Parameters:
obj (Dict)
- agentscope_runtime.engine.services.utils.tablestore_service_utils.stringify_values(d)[source]
- agentscope_runtime.engine.services.utils.tablestore_service_utils.restore_json_strings(d)[source]
- agentscope_runtime.engine.services.utils.tablestore_service_utils.tablestore_log(msg)[source]
- Parameters:
msg (str)
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_session_to_session(tablestore_session, tablestore_messages=None)[source]
Convert TablestoreSession to Session
- Parameters:
tablestore_session (Session)
tablestore_messages (List[Message] | None)
- Return type:
Session
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_session_to_tablestore_session(session)[source]
Convert Session to TablestoreSession and list of TablestoreMessage
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_message_to_message(tablestore_message)[source]
Convert TablestoreMessage to Message
- Parameters:
tablestore_message (Message)
- Return type:
Message
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_message_to_tablestore_message(message, session)[source]
Convert Message to TablestoreMessage
- Parameters:
message (Message)
session (Session)
- Return type:
Message
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_messages_to_tablestore_documents(messages, user_id, session_id, embedding_model=None)[source]
Convert list of messages to TablestoreDocuments with optional batch embedding
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_message_to_tablestore_document(message, user_id, session_id, embedding=None)[source]
Convert Message to TablestoreDocument
- agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_document_to_message(tablestore_document)[source]
Convert TablestoreDocument to Message
- Parameters:
tablestore_document (Document)
- Return type:
Message
- agentscope_runtime.engine.services.utils.tablestore_service_utils.get_message_metadata_names()[source]
Get list of message metadata field names
Schemas¶
- class agentscope_runtime.engine.schemas.agent_schemas.MessageType[source]
Bases:
object- MESSAGE = 'message'
- FUNCTION_CALL = 'function_call'
- FUNCTION_CALL_OUTPUT = 'function_call_output'
- PLUGIN_CALL = 'plugin_call'
- PLUGIN_CALL_OUTPUT = 'plugin_call_output'
- COMPONENT_CALL = 'component_call'
- COMPONENT_CALL_OUTPUT = 'component_call_output'
- MCP_LIST_TOOLS = 'mcp_list_tools'
- MCP_APPROVAL_REQUEST = 'mcp_approval_request'
- MCP_TOOL_CALL = 'mcp_call'
- MCP_APPROVAL_RESPONSE = 'mcp_approval_response'
- MCP_TOOL_CALL_OUTPUT = 'mcp_call_output'
- REASONING = 'reasoning'
- HEARTBEAT = 'heartbeat'
- ERROR = 'error'
- classmethod all_values()[source]
return all constants values in MessageType
- class agentscope_runtime.engine.schemas.agent_schemas.ContentType[source]
Bases:
object- TEXT = 'text'
- DATA = 'data'
- IMAGE = 'image'
- AUDIO = 'audio'
- FILE = 'file'
- REFUSAL = 'refusal'
- class agentscope_runtime.engine.schemas.agent_schemas.Role[source]
Bases:
object- ASSISTANT = 'assistant'
- USER = 'user'
- SYSTEM = 'system'
- TOOL = 'tool'
- class agentscope_runtime.engine.schemas.agent_schemas.RunStatus[source]
Bases:
objectEnum class for agent event message.
- Created = 'created'
- InProgress = 'in_progress'
- Completed = 'completed'
- Canceled = 'canceled'
- Failed = 'failed'
- Rejected = 'rejected'
- Unknown = 'unknown'
- Queued = 'queued'
- Incomplete = 'incomplete'
- class agentscope_runtime.engine.schemas.agent_schemas.FunctionParameters(*, type, properties, required)[source]
Bases:
BaseModel- type: str
The type of the parameters object. Must be object.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.FunctionTool(*, name, description, parameters)[source]
Bases:
BaseModelModel class for message tool.
- name: str
The name of the function to be called.
- description: str
A description of what the function does, used by the model to choose when and how to call the function.
- parameters: FunctionParameters | Dict[str, Any]
The parameters the functions accepts, described as a JSON Schema object.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.Tool(*, type='function', function=None)[source]
Bases:
BaseModelModel class for assistant message tool call.
- Parameters:
type (str | None)
function (FunctionTool | None)
- function: FunctionTool | None
The function that the model called.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.FunctionCall(*, call_id=None, name=None, arguments=None)[source]
Bases:
BaseModelModel class for assistant prompt message tool call function.
- arguments: str | None
The arguments to call the function with, as generated by the model in JSON format.
Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.FunctionCallOutput(*, call_id, name=None, output)[source]
Bases:
BaseModelModel class for assistant prompt message tool call function.
- call_id: str
The ID of the tool call.
- output: str
The result of the function.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.McpCall(*, call_id=None, arguments=None, name=None, server_label=None)[source]
Bases:
BaseModelMCP TOOL CALL MESSAGE BODY
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.McpCallOutput(*, call_id, name=None, output)[source]
Bases:
BaseModelMCP TOOL CALL OUTPUT MESSAGE BODY
- call_id: str
The unique ID of the tool call.
- output: str
The output from the tool call.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.McpListToolsTool(*, input_schema, name, annotations=None, description=None)[source]
Bases:
BaseModel- input_schema: object
The JSON schema describing the tool’s input.
- name: str
The name of the tool.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.McpListTools(*, id, server_label, tools, error=None)[source]
Bases:
BaseModel- id: str
The unique ID of the list.
- server_label: str
The label of the MCP server.
- tools: List[McpListToolsTool]
The tools available on the server.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.McpApprovalRequest(*, id, arguments, name, server_label)[source]
Bases:
BaseModelmcp approval request
- id: str
The unique ID of the approval request.
- arguments: str
A json string of arguments for the tool.
- name: str
The name of the tool to run.
- server_label: str
The label of the mcp server making the request.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.Error(*, code, message)[source]
Bases:
BaseModel- code: str
The error code of the message.
- message: str
The error message of the message.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.Event(*, sequence_number=None, object, status=None, error=None)[source]
Bases:
BaseModel- object: str
The identity of the content part.
- error: Error | None
response error for output
- created()[source]
Set the message status to ‘created’.
- Return type:
Self
- in_progress()[source]
Set the message status to ‘in_progress’.
- Return type:
Self
- completed()[source]
Set the message status to ‘completed’.
- Return type:
Self
- failed(error)[source]
Set the message status to ‘failed’.
- Parameters:
error (Error)
- Return type:
Self
- rejected()[source]
Set the message status to ‘rejected’.
- Return type:
Self
- canceled()[source]
Set the message status to ‘canceled’.
- Return type:
Self
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.agent_schemas.Content(*, sequence_number=None, object='content', status=None, error=None, type, index=None, delta=False, msg_id=None)[source]
Bases:
Event- Parameters:
- type: str
The type of the content part.
- object: str
The identity of the content part.
- static from_chat_completion_chunk(chunk, index=None)[source]
- Parameters:
chunk (ChatCompletionChunk)
index (int | None)
- Return type:
TextContent | DataContent | ImageContent | None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.ImageContent(*, sequence_number=None, object='content', status=None, error=None, type='image', index=None, delta=False, msg_id=None, image_url=None)[source]
Bases:
Content- Parameters:
- type: Literal['image']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.TextContent(*, sequence_number=None, object='content', status=None, error=None, type='text', index=None, delta=False, msg_id=None, text=None)[source]
Bases:
Content- Parameters:
- type: Literal['text']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.DataContent(*, sequence_number=None, object='content', status=None, error=None, type='data', index=None, delta=False, msg_id=None, data=None)[source]
Bases:
Content- Parameters:
- type: Literal['data']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.AudioContent(*, sequence_number=None, object='content', status=None, error=None, type='audio', index=None, delta=False, msg_id=None, data=None, format=None)[source]
Bases:
Content- Parameters:
- type: Literal['audio']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.FileContent(*, sequence_number=None, object='content', status=None, error=None, type='file', index=None, delta=False, msg_id=None, file_url=None, file_id=None, filename=None, file_data=None)[source]
Bases:
Content- Parameters:
- type: Literal['file']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.RefusalContent(*, sequence_number=None, object='content', status=None, error=None, type='refusal', index=None, delta=False, msg_id=None, refusal=None)[source]
Bases:
Content- Parameters:
- type: Literal['refusal']
The type of the content part.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
The identity of the content part.
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.ToolCall(*, arguments, call_id, name)[source]
Bases:
BaseModel- arguments: str
A JSON string of the arguments to pass to the function.
- call_id: str
The unique ID of the function tool call generated by the model.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- name: str
The name of the function to run.
- class agentscope_runtime.engine.schemas.agent_schemas.ToolCallOutput(*, call_id, output)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- call_id: str
The unique ID of the function tool call generated by the model.
- output: str
A JSON string of the output of the function tool call.
- class agentscope_runtime.engine.schemas.agent_schemas.Message(*, sequence_number=None, object='message', status='created', error=None, id=<factory>, type='message', role=None, content=None, code=None, message=None, usage=None, metadata=None)[source]
Bases:
Event- Parameters:
sequence_number (int | None)
object (str)
status (str)
error (Error | None)
id (str)
type (str)
role (Literal['assistant', 'system', 'user', 'tool'] | None)
content (List[Annotated[TextContent | ImageContent | DataContent | AudioContent | FileContent | RefusalContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | None)
code (str | None)
message (str | None)
usage (Dict | None)
metadata (Dict | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- error: Error | None
response error for output
- id: str
message unique id
- object: str
message identity
- type: str
The type of the message.
- status: str
The status of the message. in_progress, completed, or incomplete
- role: Literal['assistant', 'system', 'user', 'tool'] | None
The role of the messages author, should be in user,`system`, ‘assistant’.
- content: List[Annotated[TextContent | ImageContent | DataContent | AudioContent | FileContent | RefusalContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | None
The contents of the message.
- static from_openai_message(message)[source]
Create a message object from an openai message.
- Parameters:
message (BaseModel | dict)
- Return type:
Message
- get_text_content()[source]
Extract the first text content from the message.
- Returns:
First text string found in the content, or None if no text content
- Return type:
str | None
- get_image_content()[source]
Extract all image content (URLs or base64 data) from the message.
- get_audio_content()[source]
Extract all audio content (URLs or base64 data) from the message.
- add_delta_content(new_content)[source]
- Parameters:
new_content (TextContent | ImageContent | DataContent)
- add_content(new_content)[source]
- Parameters:
new_content (TextContent | ImageContent | DataContent)
- class agentscope_runtime.engine.schemas.agent_schemas.BaseRequest(*, input, stream=True, id=None)[source]
Bases:
BaseModelagent request
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- input: List[Message]
input messages
- stream: bool
If set, partial message deltas will be sent, like in ChatGPT.
- class agentscope_runtime.engine.schemas.agent_schemas.AgentRequest(*, input, stream=True, id=None, model=None, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, n=1, seed=None, tools=None, session_id=None, user_id=None, **extra_data)[source]
Bases:
BaseRequestagent request
- Parameters:
- input: List[Message]
input messages
- stream: bool
If set, partial message deltas will be sent, like in ChatGPT.
- model_config: ClassVar[ConfigDict] = {'extra': 'allow', 'json_schema_extra': {'example': {'input': [{'content': [{'text': 'hello', 'type': 'text'}], 'role': 'user', 'type': 'message'}, {'content': [{'object': 'content', 'text': 'Hello! How can I assist you today?', 'type': 'text'}], 'role': 'assistant', 'type': 'message'}, {'content': [{'text': 'What is the capital of France?', 'type': 'text'}], 'role': 'user', 'type': 'message'}], 'session_id': '1764056632961'}}}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- top_p: float | None
Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.
So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
- temperature: float | None
What sampling temperature to use, between 0 and 2.
Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p but not both.
- frequency_penalty: float | None
Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
- presence_penalty: float | None
Number between -2.0 and 2.0.
Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- max_tokens: int | None
The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model’s context length.
- n: int | None
How many chat completion choices to generate for each input message.
Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.
- class agentscope_runtime.engine.schemas.agent_schemas.BaseResponse(*, sequence_number=None, object='response', status='created', error=None, id=<factory>, created_at=1765859462, completed_at=None, output=None, usage=None)[source]
Bases:
Event- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- error: Error | None
response error for output
- classmethod validate_id(v)[source]
- object: str
response identity
- status: str
response run status
- created_at: int
request start time
- add_new_message(message)[source]
- Parameters:
message (Message)
- class agentscope_runtime.engine.schemas.agent_schemas.AgentResponse(*, sequence_number=None, object='response', status='created', error=None, id=<factory>, created_at=1765859462, completed_at=None, output=None, usage=None, session_id=None)[source]
Bases:
BaseResponseagent response
- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- object: str
response identity
- status: str
response run status
- created_at: int
request start time
- output: List[Message] | None
response data for output
- usage: Dict | None
response usage for output
- error: Error | None
response error for output
- class agentscope_runtime.engine.schemas.agent_schemas.SequenceNumberGenerator(start=0)[source]
Bases:
objectA simple sequence number generator for streaming events.
This class encapsulates the logic for generating sequential numbers, making the code more maintainable and less error-prone.
- Parameters:
start (int)
- __init__(start=0)[source]
Initialize the generator with a starting number.
- Parameters:
start (int) – The starting sequence number (default: 0)
- next()[source]
Get the next sequence number and increment the counter.
- Returns:
The current sequence number before incrementing
- Return type:
- yield_with_sequence(event)[source]
Set the sequence number on an event and increment the counter.
- Parameters:
event (Event) – The event to set the sequence number on
- Returns:
The same event with sequence number set
- Return type:
Event
- agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_tool_call(function)[source]
- Parameters:
function (FunctionCall)
- agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_messages(messages)[source]
Convert a generic message protocol to a model-specific protocol. :param messages: Original list of messages
- agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_tools(tools)[source]
- class agentscope_runtime.engine.schemas.session.Session(*, id, user_id, messages=[])[source]
Bases:
BaseModelRepresents a single conversation session.
A session contains the history of a conversation, including all messages, and is uniquely identified by its ID.
- id
The unique identifier for the session.
- Type:
- user_id
The identifier of the user who owns the session.
- Type:
- messages
A list of messages formatted for Agent response
- Type:
List[agentscope_runtime.engine.schemas.agent_schemas.Message | Dict[str, Any]]
- id: str
- user_id: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.embedding.Usage(*, prompt_tokens=None, total_tokens=None, input_tokens=None, text_count=None, image_count=None, video_count=None, duration=None)[source]
Bases:
BaseModel- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.embedding.EmbeddingResponse(*, data, model, object, usage)[source]
Bases:
BaseModel- data: List[Embedding]
The list of embeddings generated by the model.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model: str
The name of the model used to generate the embedding.
- object: Literal['list']
The object type, which is always “list”.
- usage: Usage
The usage information for the request.
- class agentscope_runtime.engine.schemas.realtime.AsrVendor(value)[source]
-
An enumeration.
- MODELSTUDIO = 'modelstudio'
- AZURE = 'azure'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- class agentscope_runtime.engine.schemas.realtime.TtsVendor(value)[source]
-
An enumeration.
- MODELSTUDIO = 'modelstudio'
- AZURE = 'azure'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- class agentscope_runtime.engine.schemas.realtime.ModelstudioConnection(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None)[source]
Bases:
BaseModel- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.TtsConfig(*, model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None)[source]
Bases:
BaseModel- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioTtsConfig(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None, model='cosyvoice-v2', voice='longcheng_v2', sample_rate=16000, format='pcm', bits_per_sample=None, nb_channels=None, chat_id=None)[source]
Bases:
TtsConfig,ModelstudioConnection- Parameters:
- model: str
- voice: str
- sample_rate: int
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.AsrConfig(*, model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None)[source]
Bases:
BaseModel- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioAsrConfig(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None, model='gummy-realtime-v1', language=None, sample_rate=16000, format='pcm', bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=700, fast_vad_min_duration=200, fast_vad_max_duration=1100)[source]
Bases:
AsrConfig,ModelstudioConnection- Parameters:
base_url (str | None)
api_key (str | None)
workspace_id (str | None)
user_agent (str | None)
data_inspection (str | None)
model (str | None)
language (str | None)
sample_rate (int | None)
format (str | None)
bits_per_sample (int | None)
nb_channels (int | None)
initial_silence_timeout (int | None)
max_end_silence (int | None)
fast_vad_min_duration (int | None)
fast_vad_max_duration (int | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioKnowledgeBaseConfig(*, index_ids, workspace_id, api_key)[source]
Bases:
BaseModel- workspace_id: str
- api_key: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatUpstream(*, dialog_mode='duplex', enable_server_vad=True, modalities=<factory>, asr_vendor=AsrVendor.MODELSTUDIO, asr_options=AsrConfig(model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None))[source]
Bases:
BaseModel- Parameters:
- asr_vendor: AsrVendor | None
- asr_options: AsrConfig | None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatDownstream(*, modalities=<factory>, tts_vendor=TtsVendor.MODELSTUDIO, tts_options=TtsConfig(model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None))[source]
Bases:
BaseModel- Parameters:
- tts_vendor: TtsVendor | None
- tts_options: TtsConfig | None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatParameters(*, modelstudio_kb=None, enable_tool_call=False)[source]
Bases:
BaseModel- Parameters:
modelstudio_kb (ModelstudioKnowledgeBaseConfig | None)
enable_tool_call (bool | None)
- modelstudio_kb: ModelstudioKnowledgeBaseConfig | None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatInput(*, dialog_id=None, app_id=None, text=None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatDirective(value)[source]
-
An enumeration.
- SESSION_START = 'SessionStart'
- SESSION_STOP = 'SessionStop'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatEvent(value)[source]
-
An enumeration.
- SESSION_STARTED = 'SessionStarted'
- SESSION_STOPPED = 'SessionStopped'
- AUDIO_TRANSCRIPT = 'AudioTranscript'
- RESPONSE_TEXT = 'ResponseText'
- RESPONSE_AUDIO_STARTED = 'ResponseAudioStarted'
- RESPONSE_AUDIO_ENDED = 'ResponseAudioEnded'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatInPayload[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStartPayload(*, session_id=<factory>, upstream=ModelstudioVoiceChatUpstream(dialog_mode='duplex', enable_server_vad=True, modalities=['audio'], asr_vendor=<AsrVendor.MODELSTUDIO: 'modelstudio'>, asr_options=AsrConfig(model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None)), downstream=ModelstudioVoiceChatDownstream(modalities=['audio', 'text'], tts_vendor=<TtsVendor.MODELSTUDIO: 'modelstudio'>, tts_options=TtsConfig(model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None)), parameters=ModelstudioVoiceChatParameters(modelstudio_kb=None, enable_tool_call=False))[source]
Bases:
ModelstudioVoiceChatInPayload- Parameters:
session_id (str | None)
upstream (ModelstudioVoiceChatUpstream | None)
downstream (ModelstudioVoiceChatDownstream | None)
parameters (ModelstudioVoiceChatParameters | None)
- upstream: ModelstudioVoiceChatUpstream | None
- downstream: ModelstudioVoiceChatDownstream | None
- parameters: ModelstudioVoiceChatParameters | None
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStopPayload[source]
Bases:
ModelstudioVoiceChatInPayload- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatRequest(*, directive, payload)[source]
Bases:
BaseModel- Parameters:
directive (ModelstudioVoiceChatDirective)
payload (ModelstudioVoiceChatSessionStartPayload | ModelstudioVoiceChatSessionStopPayload)
- directive: ModelstudioVoiceChatDirective
- payload: ModelstudioVoiceChatSessionStartPayload | ModelstudioVoiceChatSessionStopPayload
- parse_payload_based_on_directive(values, handler)[source]
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatOutPayload(*, session_id=None)[source]
Bases:
BaseModel- Parameters:
session_id (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStartedPayload(*, session_id=None)[source]
Bases:
ModelstudioVoiceChatOutPayload- Parameters:
session_id (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStoppedPayload(*, session_id=None)[source]
Bases:
ModelstudioVoiceChatOutPayload- Parameters:
session_id (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatAudioTranscriptPayload(*, session_id=None, text='', finished)[source]
Bases:
ModelstudioVoiceChatOutPayload- finished: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseTextPayload(*, session_id=None, text='', tool_calls=<factory>, finished)[source]
Bases:
ModelstudioVoiceChatOutPayload- Parameters:
- finished: bool
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseAudioStartedPayload(*, session_id=None)[source]
Bases:
ModelstudioVoiceChatOutPayload- Parameters:
session_id (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseAudioStoppedPayload(*, session_id=None)[source]
Bases:
ModelstudioVoiceChatOutPayload- Parameters:
session_id (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponse(*, event, payload)[source]
Bases:
BaseModel- Parameters:
event (ModelstudioVoiceChatEvent | None)
payload (ModelstudioVoiceChatSessionStartedPayload | ModelstudioVoiceChatSessionStoppedPayload | ModelstudioVoiceChatAudioTranscriptPayload | ModelstudioVoiceChatResponseTextPayload | ModelstudioVoiceChatResponseAudioStartedPayload | ModelstudioVoiceChatResponseAudioStoppedPayload)
- event: ModelstudioVoiceChatEvent | None
- payload: ModelstudioVoiceChatSessionStartedPayload | ModelstudioVoiceChatSessionStoppedPayload | ModelstudioVoiceChatAudioTranscriptPayload | ModelstudioVoiceChatResponseTextPayload | ModelstudioVoiceChatResponseAudioStartedPayload | ModelstudioVoiceChatResponseAudioStoppedPayload
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.AzureConnection(*, key=None, region=None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.AzureAsrConfig(*, key=None, region=None, model=None, language='en-US', sample_rate=16000, format='pcm', bits_per_sample=16, nb_channels=1, initial_silence_timeout=5000, max_end_silence=800, fast_vad_min_duration=None, fast_vad_max_duration=None)[source]
Bases:
AsrConfig,AzureConnection- Parameters:
key (str | None)
region (str | None)
model (str | None)
language (str | None)
sample_rate (int | None)
format (str | None)
bits_per_sample (int | None)
nb_channels (int | None)
initial_silence_timeout (int | None)
max_end_silence (int | None)
fast_vad_min_duration (int | None)
fast_vad_max_duration (int | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.realtime.AzureTtsConfig(*, key=None, region=None, model=None, voice='en-US-AvaMultilingualNeural', sample_rate=16000, format='pcm', bits_per_sample=16, nb_channels=1, chat_id=None)[source]
Bases:
TtsConfig,AzureConnection- Parameters:
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.modelstudio_llm.KnowledgeHolder(*, source, content)[source]
Bases:
BaseModel- source: str
The source identifier or URL where the knowledge was retrieved from.
- content: str
The actual content or knowledge text retrieved from the source.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.modelstudio_llm.IntentionOptions(*, white_list=<factory>, black_list=<factory>, search_model='search_v6', intensity=None, scene_id=None)[source]
Bases:
BaseModel- Parameters:
- search_model: str
The search model version to use for intentions recognition.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.modelstudio_llm.SearchOptions(*, enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None))[source]
Bases:
BaseModelSearch Options on Modelstudio platform for knowledge retrieval and web search.
- Parameters:
enable_source (bool)
enable_citation (bool)
enable_readpage (bool)
enable_online_read (bool)
citation_format (str)
search_strategy (Literal['standard', 'pro_ultra', 'pro', 'lite', 'pro_max', 'image', 'turbo', 'max'])
forced_search (bool)
prepend_search_result (bool)
enable_search_extension (bool)
item_cnt (int)
top_n (int)
intention_options (IntentionOptions | None)
- enable_source: bool
Whether to include source information in search results.
- enable_citation: bool
Whether to include citation information for retrieved content.
- enable_readpage: bool
Whether to enable full page reading for web content.
- enable_online_read: bool
Whether to enable online reading capabilities for real-time content.
- citation_format: str
The format string for citations in the response.
- search_strategy: Literal['standard', 'pro_ultra', 'pro', 'lite', 'pro_max', 'image', 'turbo', 'max']
The search strategy to use (‘standard’, ‘pro_ultra’, ‘pro’, ‘lite’,’pro_max’, ‘image’,’turbo’,’max’).
- forced_search: bool
Whether to force search even when cached results are available.
- prepend_search_result: bool
Whether to prepend search results to the response.
- enable_search_extension: bool
Whether to enable extended search capabilities.
- item_cnt: int
The maximum number of items to retrieve in search results.
- top_n: int
The number of top results to return (0 means return all).
- intention_options: IntentionOptions | None
Options for intentions recognition and processing during search.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.modelstudio_llm.RagOptions(*, workspace_id='', replaced_word='${documents}', index_names=<factory>, pipeline_ids=<factory>, file_id_list=<factory>, prompt_strategy_name='topK', maximum_allowed_chunk_num=5, maximum_allowed_length=2000, prompt_enable_citation=False, fallback_options=None, enable_web_search=False, session_file_ids=<factory>, dense_similarity_top_k=100, sparse_similarity_top_k=100, enable_rewrite=None, rewrite=None, enable_reranking=None, rerank_min_score=None, rerank_top_n=None, rerank=None, enable_reject_filter=None, reject_filter_type=None, reject_filter_model_name=None, reject_filter_prompt=None, enable_agg_search=None, enable_hybrid_gen=None)[source]
Bases:
BaseModel- Parameters:
workspace_id (str | None)
replaced_word (str)
prompt_strategy_name (str | None)
maximum_allowed_chunk_num (int | None)
maximum_allowed_length (int | None)
prompt_enable_citation (bool)
fallback_options (FallbackOptions | None)
enable_web_search (bool)
dense_similarity_top_k (int | None)
sparse_similarity_top_k (int | None)
enable_rewrite (bool | None)
rewrite (List[RewriteOptions] | None)
enable_reranking (bool | None)
rerank_min_score (float | None)
rerank_top_n (int | None)
rerank (List[RerankOptions] | None)
enable_reject_filter (bool | None)
reject_filter_type (str | None)
reject_filter_model_name (str | None)
reject_filter_prompt (str | None)
enable_agg_search (bool | None)
enable_hybrid_gen (bool | None)
- model_config: ClassVar[ConfigDict] = {'populate_by_name': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class FallbackOptions(*, default_response_type='llm', default_response='')[source]
Bases:
BaseModel- default_response_type: str | None
The type of default response when RAG fails (‘llm’, ‘template’, ‘none’).
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class RewriteOptions(*, model_name=None, class_name=None)[source]
Bases:
BaseModel- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class RerankOptions(*, model_name=None)[source]
Bases:
BaseModel- Parameters:
model_name (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- replaced_word: str
The placeholder word in prompts that will be replaced with retrieved documents.
- maximum_allowed_chunk_num: int | None
The maximum number of document chunks to include in the context.
- enable_citation: bool
Whether to include citation information for retrieved documents.
- fallback_options: FallbackOptions | None
Options for handling cases when RAG retrieval fails.
- enable_web_search: bool
Whether to enable web search as part of the RAG pipeline.
- class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioParameters(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None), repetition_penalty=None, length_penalty=None, top_k=None, min_tokens=None, result_format='message', incremental_output=False, enable_search=False, search_options=SearchOptions(enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None)), enable_rag=False, rag_options=None, selected_model='qwen-max', intention_options=None, mcp_config_file=None)[source]
Bases:
ParametersParameters for Modelstudio platform, extending the base Parameters with Modelstudio-specific options.
- Parameters:
top_p (float | None)
temperature (float | None)
frequency_penalty (float | None)
presence_penalty (float | None)
max_tokens (int | None)
stream (bool)
stream_options (ChatCompletionStreamOptionsParam | None)
tool_choice (str | ToolChoice | None)
parallel_tool_calls (bool)
top_logprobs (int | None)
logprobs (bool | None)
seed (int | None)
response_format (ResponseFormat | str | None)
repetition_penalty (float | None)
length_penalty (float | None)
min_tokens (int | None)
result_format (Literal['text', 'message'])
incremental_output (bool)
enable_search (bool)
search_options (SearchOptions | None)
enable_rag (bool)
rag_options (RagOptions | None)
selected_model (str | None)
intention_options (IntentionOptions | None)
mcp_config_file (str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- top_p: float | None
Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.
So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
- temperature: float | None
What sampling temperature to use, between 0 and 2.
Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p but not both.
- frequency_penalty: float | None
Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
- presence_penalty: float | None
Number between -2.0 and 2.0.
Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- max_tokens: int | None
The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model’s context length.
- stop: str | None | List[str] | None
Up to 4 sequences where the API will stop generating further tokens.
- stream: bool
If set, partial message deltas will be sent, like in ChatGPT.
- stream_options: ChatCompletionStreamOptionsParam | None
Options for streaming response. Only set this when you set stream: true.
- tools: List[Tool | Dict] | None
A list of tools the model may call.
Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.
- parallel_tool_calls: bool
Whether to enable parallel function calling during tool use.
- logit_bias: Dict[str, int] | None
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
- top_logprobs: int | None
An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
logprobs must be set to true if this parameter is used.
- logprobs: bool | None
Whether to return log probabilities of the output tokens or not.
If true, returns the log probabilities of each output token returned in the content of message.
- n: int | None
How many chat completion choices to generate for each input message.
Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.
- seed: int | None
If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
- response_format: ResponseFormat | str | None
An object specifying the format that the model must output.
Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.
- length_penalty: float | None
Penalty applied to longer sequences. Affects the length of generated text.
- top_k: Annotated[int, Strict(strict=True)] | None
The number of highest probability vocabulary tokens to keep for top-k filtering.
- result_format: Literal['text', 'message']
The format of the response (‘text’ for plain text, ‘message’ for structured message)
- incremental_output: bool
Whether to return incremental output during generations.
- enable_search: bool
Whether to enable search capabilities for knowledge retrieval.
- search_options: SearchOptions | None
Configuration options for search functionality.
- enable_rag: bool
Whether to enable Retrieval-Augmented Generation (RAG) for the Modelstudio assistant service.
- rag_options: RagOptions | None
Configuration options for RAG functionality.
- intention_options: IntentionOptions | None
Options for intentions recognition and processing.
- class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatRequest(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None), repetition_penalty=None, length_penalty=None, top_k=None, min_tokens=None, result_format='message', incremental_output=False, enable_search=False, search_options=SearchOptions(enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None)), enable_rag=False, rag_options=None, selected_model='qwen-max', intention_options=None, mcp_config_file=None, messages, model)[source]
Bases:
ModelstudioParameters- Parameters:
top_p (float | None)
temperature (float | None)
frequency_penalty (float | None)
presence_penalty (float | None)
max_tokens (int | None)
stream (bool)
stream_options (ChatCompletionStreamOptionsParam | None)
tool_choice (str | ToolChoice | None)
parallel_tool_calls (bool)
top_logprobs (int | None)
logprobs (bool | None)
seed (int | None)
response_format (ResponseFormat | str | None)
repetition_penalty (float | None)
length_penalty (float | None)
min_tokens (int | None)
result_format (Literal['text', 'message'])
incremental_output (bool)
enable_search (bool)
search_options (SearchOptions | None)
enable_rag (bool)
rag_options (RagOptions | None)
selected_model (str | None)
intention_options (IntentionOptions | None)
mcp_config_file (str | None)
messages (List[OpenAIMessage])
model (str)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- length_penalty: float | None
Penalty applied to longer sequences. Affects the length of generated text.
- top_k: StrictInt | None
The number of highest probability vocabulary tokens to keep for top-k filtering.
- result_format: Literal['text', 'message']
The format of the response (‘text’ for plain text, ‘message’ for structured message)
- incremental_output: bool
Whether to return incremental output during generations.
- enable_search: bool
Whether to enable search capabilities for knowledge retrieval.
- search_options: SearchOptions | None
Configuration options for search functionality.
- enable_rag: bool
Whether to enable Retrieval-Augmented Generation (RAG) for the Modelstudio assistant service.
- rag_options: RagOptions | None
Configuration options for RAG functionality.
- intention_options: IntentionOptions | None
Options for intentions recognition and processing.
- top_p: float | None
Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.
So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
- temperature: float | None
What sampling temperature to use, between 0 and 2.
Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p but not both.
- frequency_penalty: float | None
Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
- presence_penalty: float | None
Number between -2.0 and 2.0.
Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- max_tokens: int | None
The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model’s context length.
- stop: str | None | List[str] | None
Up to 4 sequences where the API will stop generating further tokens.
- stream: bool
If set, partial message deltas will be sent, like in ChatGPT.
- stream_options: ChatCompletionStreamOptionsParam | None
Options for streaming response. Only set this when you set stream: true.
- tools: List[Tool | Dict] | None
A list of tools the model may call.
Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.
- parallel_tool_calls: bool
Whether to enable parallel function calling during tool use.
- logit_bias: Dict[str, int] | None
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
- top_logprobs: int | None
An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
logprobs must be set to true if this parameter is used.
- logprobs: bool | None
Whether to return log probabilities of the output tokens or not.
If true, returns the log probabilities of each output token returned in the content of message.
- n: int | None
How many chat completion choices to generate for each input message.
Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.
- seed: int | None
If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.
- response_format: ResponseFormat | str | None
An object specifying the format that the model must output.
Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.
- messages: List[OpenAIMessage]
A list of messages comprising the conversation so far.
- model: str
ID of the model to use for the chat completion.
- class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatResponse(*, id, choices, created, model, object, service_tier=None, system_fingerprint=None, usage=None, **extra_data)[source]
Bases:
ChatCompletion- Parameters:
- model_config: ClassVar[ConfigDict] = {'defer_build': True, 'extra': 'allow'}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- id: str
A unique identifier for the chat completion.
- choices: List[Choice]
A list of chat completion choices.
Can be more than one if n is greater than 1.
- created: int
The Unix timestamp (in seconds) of when the chat completion was created.
- model: str
The model used for the chat completion.
- object: Literal['chat.completion']
The object type, which is always chat.completion.
- service_tier: Literal['auto', 'default', 'flex', 'scale', 'priority'] | None
Specifies the processing type used for serving the request.
If set to ‘auto’, then the request will be processed with the service tier configured in the Project settings. Unless otherwise configured, the Project will use ‘default’.
If set to ‘default’, then the request will be processed with the standard pricing and performance for the selected model.
If set to ‘[flex](https://platform.openai.com/docs/guides/flex-processing)’ or ‘[priority](https://openai.com/api-priority-processing/)’, then the request will be processed with the corresponding service tier.
When not set, the default behavior is ‘auto’.
When the service_tier parameter is set, the response body will include the service_tier value based on the processing mode actually used to serve the request. This response value may be different from the value set in the parameter.
- system_fingerprint: str | None
This fingerprint represents the backend configuration that the model runs with.
Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.
- usage: CompletionUsage | None
Usage statistics for the completion request.
- class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatCompletionChunk(*, id, choices, created, model, object, service_tier=None, system_fingerprint=None, usage=None, **extra_data)[source]
Bases:
ChatCompletionChunk- Parameters:
- model_config: ClassVar[ConfigDict] = {'defer_build': True, 'extra': 'allow'}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- id: str
A unique identifier for the chat completion. Each chunk has the same ID.
- choices: List[Choice]
A list of chat completion choices.
Can contain more than one elements if n is greater than 1. Can also be empty for the last chunk if you set stream_options: {“include_usage”: true}.
- created: int
The Unix timestamp (in seconds) of when the chat completion was created.
Each chunk has the same timestamp.
- model: str
The model to generate the completion.
- object: Literal['chat.completion.chunk']
The object type, which is always chat.completion.chunk.
- service_tier: Literal['auto', 'default', 'flex', 'scale', 'priority'] | None
Specifies the processing type used for serving the request.
If set to ‘auto’, then the request will be processed with the service tier configured in the Project settings. Unless otherwise configured, the Project will use ‘default’.
If set to ‘default’, then the request will be processed with the standard pricing and performance for the selected model.
If set to ‘[flex](https://platform.openai.com/docs/guides/flex-processing)’ or ‘[priority](https://openai.com/api-priority-processing/)’, then the request will be processed with the corresponding service tier.
When not set, the default behavior is ‘auto’.
When the service_tier parameter is set, the response body will include the service_tier value based on the processing mode actually used to serve the request. This response value may be different from the value set in the parameter.
- system_fingerprint: str | None
This fingerprint represents the backend configuration that the model runs with. Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.
- usage: CompletionUsage | None
An optional field that will only be present when you set stream_options: {“include_usage”: true} in your request. When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.
NOTE: If the stream is interrupted or cancelled, you may not receive the final usage chunk which contains the total token usage for the request.
- agentscope_runtime.engine.schemas.oai_llm.generate_tool_call_id(prefix='call_')[source]
- class agentscope_runtime.engine.schemas.oai_llm.ImageMessageContent(*, type='image_url', image_url)[source]
Bases:
BaseModel- Parameters:
type (Literal['image_url'])
image_url (ImageUrl)
- class ImageUrl(*, url, detail='low')[source]
Bases:
BaseModelModel class for image prompt message content.
- url: str
Either a URL of the image or the base64 encoded image data.
- detail: Literal['auto', 'low', 'high']
Specifies the detail level of the image.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- type: Literal['image_url']
The type of the content part.
- image_url: ImageUrl
The image URL details.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.oai_llm.TextMessageContent(*, type='text', text)[source]
Bases:
BaseModel- type: Literal['text']
The type of the content part.
- text: str
The text content.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.oai_llm.AudioMessageContent(*, type='input_audio', input_audio)[source]
Bases:
BaseModel- Parameters:
type (Literal['input_audio'])
input_audio (InputAudioDetail)
- class InputAudioDetail(*, base64_data='', format='mp3')[source]
Bases:
BaseModelModel class for audio prompt message content.
- base64_data: str
The base64 encoded audio data.
- format: str
The format of the encoded audio data. Supports ‘wav’ and ‘mp3’.
- property data: str
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- type: Literal['input_audio']
The type of the content part.
- input_audio: InputAudioDetail
The input audio details.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.oai_llm.ToolCall(*, index=0, id, type=None, function)[source]
Bases:
BaseModelModel class for assistant prompt message tool call.
- index: int
The index of the tool call in the tool calls array.
- id: str
The ID of the tool call.
- function: FunctionCall
The function that the model called.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.oai_llm.OpenAIMessage(*, role, content=None, name=None, tool_calls=None)[source]
Bases:
BaseModelModel class for prompt message.
- Parameters:
- role: str
The role of the messages author, should be in user,`system`, ‘assistant’, ‘tool’.
- content: List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None
The contents of the message.
Can be a string, a list of content parts for multimodal messages.
- name: str | None
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
- get_text_content()[source]
Extract the first text content from the message.
- Returns:
First text string found in the content, or None if no text content.
- Return type:
str | None
- get_image_content()[source]
Extract all image content (URLs or base64 data) from the message.
- get_audio_content()[source]
Extract all audio content (URLs or base64 data) from the message.
- has_multimodal_content()[source]
Check if the message contains multimodal content (images, audio, or video).
- Returns:
True if the message contains non-text content, False otherwise
- Return type:
- get_content_summary()[source]
Get a summary of different content types in the message.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.schemas.oai_llm.UserMessage(*, role='user', content=None, name=None, tool_calls=None)[source]
Bases:
OpenAIMessageModel class for user prompt message.
- Parameters:
- role: str
The role of the messages author, in this case user.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- content: List[ChatCompletionMessage] | str | None
The contents of the message.
Can be a string, a list of content parts for multimodal messages.
- name: str | None
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
- tool_calls: List[ToolCall] | None
The tool calls generated by the model, such as function calls.
- class agentscope_runtime.engine.schemas.oai_llm.AssistantMessage(*, role='assistant', content=None, name=None, tool_calls=None)[source]
Bases:
OpenAIMessageModel class for assistant prompt message.
- Parameters:
- role: str
The role of the messages author, in this case assistant.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- content: List[ChatCompletionMessage] | str | None
The contents of the message.
Can be a string, a list of content parts for multimodal messages.
- name: str | None
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
- tool_calls: List[ToolCall] | None
The tool calls generated by the model, such as function calls.
- class agentscope_runtime.engine.schemas.oai_llm.SystemMessage(*, role='system', content=None, name=None, tool_calls=None)[source]
Bases:
OpenAIMessageModel class for system prompt message.
- Parameters:
- role: str
The role of the messages author, in this case system.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- content: List[ChatCompletionMessage] | str | None
The contents of the message.
Can be a string, a list of content parts for multimodal messages.
- name: str | None
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
- tool_calls: List[ToolCall] | None
The tool calls generated by the model, such as function calls.
- class agentscope_runtime.engine.schemas.oai_llm.ToolMessage(*, role='tool', content=None, name=None, tool_calls=None, tool_call_id)[source]
Bases:
OpenAIMessageModel class for tool prompt message.
- Parameters:
- role: str
The role of the messages author, in this case tool.
- tool_call_id: str
Tool call that this message is responding to.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- content: List[ChatCompletionMessage] | str | None
The contents of the message.
Can be a string, a list of content parts for multimodal messages.
- name: str | None
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
- tool_calls: List[ToolCall] | None
The tool calls generated by the model, such as function calls.
- class agentscope_runtime.engine.schemas.oai_llm.ResponseFormat(*, type='text', json_schema=None)[source]
Bases:
BaseModel- Parameters:
type (Literal['text', 'json_object', 'json_schema'])
json_schema (JsonSchema | None)
- class JsonSchema(*, name, description=None, schema=None, strict=False)[source]
Bases:
BaseModel- name: str
The name of the response format.
- description: str | None
A description of what the response format is for, used by the model to determine how to respond in the format.
- schema_param: dict
The schema for the response format, described as a JSON Schema object.
- strict: bool | None
Whether to enable strict schema adherence when generating the output
If set to true, the model will follow the exact schema defined in the schema field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the [function calling guide](docs/guides/function-calling).
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- type: Literal['text', 'json_object', 'json_schema']
The type of response format being defined.
- text: The default response format, which can be either text or any
value needed.
- json_object: Enables JSON mode, which guarantees the message the model
generates is valid JSON.
- json_schema: Enables Structured Outputs which guarantees the model will
match your supplied JSON schema.
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- json_schema: JsonSchema | None
The JSON schema for the response format.
- validate_schema()[source]
- Return type:
ResponseFormat
- class agentscope_runtime.engine.schemas.oai_llm.ToolChoiceInputFunction(*, name)[source]
Bases:
BaseModel- Parameters:
name (str)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- name: str
The name of the function to call.
- class agentscope_runtime.engine.schemas.oai_llm.ToolChoice(*, type, function)[source]
Bases:
BaseModel- Parameters:
type (str)
function (ToolChoiceInputFunction)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- type: str
The type of the tool. Currently, only function is supported.
- function: ToolChoiceInputFunction
The function that the model called.
- class agentscope_runtime.engine.schemas.oai_llm.Parameters(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None))[source]
Bases:
BaseModelGeneral Parameters for LLM
- Parameters:
top_p (float | None)
temperature (float | None)
frequency_penalty (float | None)
presence_penalty (float | None)
max_tokens (int | None)
stream (bool)
stream_options (ChatCompletionStreamOptionsParam | None)
tool_choice (str | ToolChoice | None)
parallel_tool_calls (bool)
top_logprobs (int | None)
logprobs (bool | None)
seed (int | None)
response_format (ResponseFormat | str | None)
- model_config: ClassVar[ConfigDict] = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- top_p: float | None
Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.
So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
- temperature: float | None
What sampling temperature to use, between 0 and 2.
Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p but not both.
- frequency_penalty: float | None
Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
- presence_penalty: float | None
Number between -2.0 and 2.0.
Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
- max_tokens: int | None
The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.
The total length of input tokens and generated tokens is limited by the model’s context length.
- stream: bool
If set, partial message deltas will be sent, like in ChatGPT.
- stream_options: ChatCompletionStreamOptionsParam | None
Options for streaming response. Only set this when you set stream: true.
- tools: List[Tool | Dict] | None
A list of tools the model may call.
Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.
- parallel_tool_calls: bool
Whether to enable parallel function calling during tool use.
- logit_bias: Dict[str, int] | None
Modify the likelihood of specified tokens appearing in the completion.
Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
- top_logprobs: int | None
An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.
logprobs must be set to true if this parameter is used.
- logprobs: bool | None
Whether to return log probabilities of the output tokens or not.
If true, returns the log probabilities of each output token returned in the content of message.
- n: int | None
How many chat completion choices to generate for each input message.
Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.
- agentscope_runtime.engine.schemas.oai_llm.create_chat_completion(message, model_name, id='', finish_reason=None)[source]
- agentscope_runtime.engine.schemas.oai_llm.create_chat_completion_chunk(message, model_name, id='', finish_reason=None)[source]
Tracing¶
- class agentscope_runtime.engine.tracing.base.TracerHandler[source]
Bases:
ABCAbstract base class for tracer handlers.
- abstract on_start(event_name, payload, **kwargs)[source]
Handle the start of a trace event.
- abstract on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]
Handle the end of a trace event.
- Parameters:
- Return type:
None
- abstract on_log(message, **kwargs)[source]
Handle a log message during tracing.
- Parameters:
message (str) – The log message.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- abstract on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]
Handle an error during tracing.
- Parameters:
event_name (str) – The type of event being traced.
start_payload (Dict[str, Any]) – The payload data from event start.
error (Exception) – The exception that occurred.
start_time (float) – The timestamp when the event started.
traceback_info (str) – The traceback information.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- class agentscope_runtime.engine.tracing.base.BaseLogHandler[source]
Bases:
TracerHandlerBasic log handler implementation using Python’s logging module.
- logging = <module 'logging' from '/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/logging/__init__.py'>
- logger = <Logger agentscope_runtime.engine.tracing.base (INFO)>
- on_start(event_name, payload, **kwargs)[source]
Log the start of a trace event.
- on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]
Log the end of a trace event.
- Parameters:
- Return type:
None
- on_log(message, **kwargs)[source]
Log a message during tracing.
- Parameters:
message (str) – The log message.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]
Log an error during tracing.
- Parameters:
event_name (str) – The name of event being traced.
start_payload (Dict[str, Any]) – The payload data from event start.
error (Exception) – The exception that occurred.
start_time (float) – The timestamp when the event started.
traceback_info (str) – The traceback information.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- class agentscope_runtime.engine.tracing.base.Tracer(handlers)[source]
Bases:
objectTracer class for logging events.
Usage example:
with tracer.event(TraceType.LLM, payload) as event: event.log("message") # ...logic here... end_payload = {"xxx": "value"} # optional on_end call for additional payload and kwargs event.on_end(end_payload, if_success=True)
- Parameters:
handlers (List[TracerHandler])
- __init__(handlers)[source]
Initialize the tracer with a list of handlers.
- Parameters:
handlers (List[TracerHandler]) – List of handlers to process trace
events.
- event(span, event_name, payload, **kwargs)[source]
Create a context manager for tracing an event.
- class agentscope_runtime.engine.tracing.base.EventContext(span, handlers, event_name, start_time, start_payload)[source]
Bases:
objectContext manager for individual trace events.
- Parameters:
- __init__(span, handlers, event_name, start_time, start_payload)[source]
Initialize the event context.
- on_end(payload, **kwargs)[source]
Set the end payload and additional kwargs for the event.
- Parameters:
payload (Dict[str, Any]) – The payload data for event end.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- on_log(message, **kwargs)[source]
Log a message within this event context.
- Parameters:
message (str) – The log message.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- finalize(start_payload=None)[source]
Public method to finalize the event.
- Parameters:
start_payload (Dict[str, Any], optional) – The payload data from
start. (event)
- Return type:
None
- set_attribute(key, value)[source]
Set attribute for the current span.
- get_trace_context()[source]
- Return type:
Context
- class agentscope_runtime.engine.tracing.tracing_util.TracingUtil[source]
Bases:
object- static get_request_id(default_value='')[source]
Get request id
- static set_trace_header(trace_headers)[source]
Set trace headers
- Parameters:
trace_headers (dict) – trace headers to set
- Return type:
None
- static get_trace_header()[source]
Get trace headers
- Returns:
trace headers in dict
- Return type:
- static set_common_attributes(attributes)[source]
Set common attributes by merging with existing ones
- Parameters:
attributes (dict) – common attributes to merge
- Return type:
None
- static get_common_attributes()[source]
Get common attributes
- Returns:
common attributes in dict
- Return type:
- static clear_common_attributes()[source]
Clear common attributes
- Return type:
None
- agentscope_runtime.engine.tracing.tracing_util.get_global_attributes()[source]
Set global common attributes for tracing.
- Return type:
- class agentscope_runtime.engine.tracing.tracing_metric.TraceType(value)[source]
Bases:
strCallback manager event types.
- Parameters:
value (str)
- LLM
Logs for the template and response of LLM calls.
- TOOL:Logs for the tool name and its arguments and
the output of tool calls
- AGENT_STEP
Logs for the agent loop start and end?
- LLM = 'LLM'
- TOOL = 'TOOL'
- AGENT_STEP = 'AGENT_STEP'
- MEMORY = 'MEMORY'
- SEARCH = 'SEARCH'
- AIGC = 'AIGC'
- RAG = 'RAG'
- INTENTION = 'INTENTION'
- PLUGIN = 'PLUGIN'
- FUNCTION_CALL = 'FUNCTION_CALL'
- AGENT = 'AGENT'
- PLANNING = 'PLANNING'
- CHAIN = 'CHAIN'
- RETRIEVER = 'RETRIEVER'
- RERANKER = 'RERANKER'
- EMBEDDING = 'EMBEDDING'
- TASK = 'TASK'
- GUARDRAIL = 'GUARDRAIL'
- REWRITER = 'REWRITER'
- OTHER = 'OTHER'
- __init__(value)[source]
Initialize a TraceType with a string value.
- Parameters:
value (str) – The string value for the trace type.
- Raises:
ValueError – If value is not a string.
- __str__()[source]
Return the string representation of the trace type.
- Returns:
The trace type value.
- Return type:
- __repr__()[source]
Return the detailed string representation of the trace type.
- Returns:
The trace type representation in the format TraceType(value).
- Return type:
- classmethod add_type(name, value)[source]
Add a new trace type to the class.
- Parameters:
- Raises:
ValueError – If name or value are not strings, or if the name
already exists. –
- Return type:
None
- class agentscope_runtime.engine.tracing.wrapper.MineType(value)[source]
-
MIME type enumeration for content types.
- TEXT = 'text/plain'
- JSON = 'application/json'
- __format__(format_spec)
Returns format using actual value type unless __str__ has been overridden.
- agentscope_runtime.engine.tracing.wrapper.trace(trace_type=None, *, trace_name=None, is_root_span=None, get_finish_reason_func=<function get_finish_reason>, merge_output_func=<function merge_incremental_chunk>)[source]
Decorator for tracing function execution.
- Parameters:
trace_type (Union[TraceType, str]) – The type of trace event.
trace_name (Optional[str]) – The name of trace event.
is_root_span (Optional[bool]) – Specify current span as root span
get_finish_reason_func (Optional[Callable]) – The function to judge if stopped
merge_output_func (Optional[Callable]) – The function to merge outputs
- Returns:
The decorated function with tracing capabilities.
- Return type:
Any
- class agentscope_runtime.engine.tracing.local_logging_handler.LogContext(*, time='', step='', model='', user_id='', task_id='', code='', message='', request_id='', context={}, interval={}, service_id='', service_name='', ds_service_id='', ds_service_name='')[source]
Bases:
BaseModelPydantic model for log context data.
- Parameters:
- time: str
- step: str
- model: str
- user_id: str
- task_id: str
- code: str
- message: str
- request_id: str
- context: Dict
- interval: Dict
- service_id: str
- service_name: str
- ds_service_id: str
- ds_service_name: str
- model_config: ClassVar[ConfigDict] = {'extra': 'ignore'}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class agentscope_runtime.engine.tracing.local_logging_handler.JsonFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]
Bases:
FormatterCustom formatter to output logs in llm chat format.
- format(record)[source]
Format a log record as JSON.
- Parameters:
record (logging.LogRecord) – The log record to format.
- Returns:
The formatted log record as a JSON string.
- Return type:
- class agentscope_runtime.engine.tracing.local_logging_handler.LocalLogHandler(log_level=20, log_file_name=None, log_dir='/home/runner/work/agentscope-runtime/agentscope-runtime/cookbook/logs', max_bytes=1073741824, backup_count=7, enable_console=False, **kwargs)[source]
Bases:
TracerHandlerllm chat log handler for structured JSON logging.
- Parameters:
- __init__(log_level=20, log_file_name=None, log_dir='/home/runner/work/agentscope-runtime/agentscope-runtime/cookbook/logs', max_bytes=1073741824, backup_count=7, enable_console=False, **kwargs)[source]
Initialize the llm chat log handler.
- Parameters:
log_level (int) – The logging level. Defaults to logging.INFO.
log_file_name (Optional[str]) – Prefix for log file names. Defaults to None.
log_dir (str) – Directory to save log files. Defaults to “./logs”.
max_bytes (int) – Maximum size in bytes for a single log file. Defaults to 1GB.
backup_count (int) – Number of log files to keep. Defaults to 7.
enable_console (bool) – Whether to enable console logging. Defaults to False.
**kwargs (Any) – Additional keyword arguments (unused but kept for compatibility).
- Return type:
None
- on_start(event_name, payload, **kwargs)[source]
Handle the start of a trace event.
- on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]
Handle the end of a trace event.
- Parameters:
- Return type:
None
- on_log(message, **kwargs)[source]
Handle a log message during tracing.
- Parameters:
message (str) – The log message.
**kwargs (Any) – Additional keyword arguments including: - step_suffix: Optional suffix for step identification - event_type: The type of event being traced - payload: The payload data - start_time: The timestamp when the event started - start_payload: The payload data from event start
- Return type:
None
- on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]
Handle an error during tracing.
- Parameters:
event_name (str) – The name of event being traced.
start_payload (Dict[str, Any]) – The payload data from event start.
error (Exception) – The exception that occurred.
start_time (float) – The timestamp when the event started.
traceback_info (str) – The traceback information.
**kwargs (Any) – Additional keyword arguments.
- Return type:
None
- agentscope_runtime.engine.tracing.message_util.merge_incremental_chunk(responses)[source]
Merge an incremental chunk list to a ChatCompletionChunk.
- Parameters:
responses (List[ChatCompletionChunk]) – List of incremental chat completion chunks to merge into a single response.
- Returns:
The merged chat completion chunk, or None if the input list is empty.
- Return type:
Optional[ChatCompletionChunk]
- agentscope_runtime.engine.tracing.message_util.get_finish_reason(response)[source]
- Parameters:
response (ChatCompletionChunk)
- Return type:
str | None
- agentscope_runtime.engine.tracing.message_util.merge_agent_response(responses)[source]
Merge a list of incremental response objects into a single AgentResponse.
- Parameters:
responses (List[Union[AgentResponse, Message, TextContent]]) – List of incremental responses to merge into a single response.
- Returns:
The merged agent response.
- Return type:
AgentResponse
- agentscope_runtime.engine.tracing.message_util.get_agent_response_finish_reason(response)[source]
Get the finish reason from a response object.
- Parameters:
response (Union[AgentResponse, Message, TextContent]) – The response object
- Returns:
The finish reason, or None if not finished
- Return type:
Optional[str]
- agentscope_runtime.engine.tracing.message_util.merge_agent_message(messages)[source]
Merge a list of incremental message objects into a single Message.
- Parameters:
messages (List[Union[Message, TextContent]]) – List of incremental messages to merge into a single message.
- Returns:
The merged message.
- Return type:
Message
- agentscope_runtime.engine.tracing.message_util.get_agent_message_finish_reason(message)[source]
- Parameters:
message (Message | TextContent | None)
- Return type:
str | None
- async agentscope_runtime.engine.tracing.asyncio_util.aenumerate(asequence, start=0)[source]
Asynchronously enumerate an async iterator from a given start value.
- Parameters:
asequence (AsyncIterable[T_co]) – The async iterable to enumerate.
start (int) – The starting value for enumeration. Defaults to 0.
- Yields:
Tuple[int, T_co] – A tuple containing the index and the item from the async iterable.
- Return type:
AsyncIterator[Tuple[int, T_co]]