beamlit 0.0.24rc19__py3-none-any.whl → 0.0.24rc21__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- beamlit/agents/chat.py +37 -11
- beamlit/agents/decorator.py +97 -65
- beamlit/api/agents/create_agent.py +9 -14
- beamlit/api/agents/delete_agent.py +22 -1
- beamlit/api/agents/delete_agent_history.py +2 -6
- beamlit/api/agents/get_agent.py +22 -1
- beamlit/api/agents/get_agent_environment_logs.py +11 -11
- beamlit/api/agents/get_agent_history.py +2 -6
- beamlit/api/agents/get_agent_metrics.py +22 -1
- beamlit/api/agents/list_agent_history.py +11 -11
- beamlit/api/agents/list_agents.py +36 -4
- beamlit/api/agents/put_agent_history.py +2 -6
- beamlit/api/agents/update_agent.py +9 -14
- beamlit/api/functions/create_function.py +9 -14
- beamlit/api/functions/delete_function.py +22 -1
- beamlit/api/functions/get_function.py +22 -1
- beamlit/api/functions/get_function_environment_logs.py +11 -11
- beamlit/api/functions/get_function_metrics.py +22 -1
- beamlit/api/functions/list_functions.py +36 -4
- beamlit/api/functions/update_function.py +9 -14
- beamlit/api/history/get_agents_history.py +12 -12
- beamlit/api/history/list_agents_history.py +12 -12
- beamlit/api/integrations/get_integration_connection.py +16 -12
- beamlit/api/integrations/get_integration_connection_model.py +8 -1
- beamlit/api/integrations/get_integration_model.py +8 -1
- beamlit/api/locations/list_locations.py +12 -12
- beamlit/api/models/create_model.py +13 -14
- beamlit/api/models/delete_model.py +22 -1
- beamlit/api/models/get_model.py +22 -1
- beamlit/api/models/get_model_environment_logs.py +11 -11
- beamlit/api/models/get_model_metrics.py +38 -13
- beamlit/api/models/list_models.py +36 -4
- beamlit/api/models/update_model.py +13 -14
- beamlit/api/privateclusters/__init__.py +0 -0
- beamlit/api/{agents/get_agent_deployment_metrics.py → privateclusters/create_private_cluster.py} +28 -55
- beamlit/api/{functions/list_function_deployments.py → privateclusters/delete_private_cluster.py} +38 -37
- beamlit/api/{agents/get_agent_deployment.py → privateclusters/get_private_cluster.py} +40 -44
- beamlit/api/privateclusters/get_private_cluster_health.py +97 -0
- beamlit/api/{agents/list_agent_deployments.py → privateclusters/list_private_clusters.py} +30 -45
- beamlit/api/{models/list_model_deployments.py → privateclusters/update_private_cluster.py} +38 -45
- beamlit/api/privateclusters/update_private_cluster_health.py +97 -0
- beamlit/authentication/device_mode.py +3 -9
- beamlit/common/generate.py +47 -34
- beamlit/common/settings.py +29 -27
- beamlit/deploy/deploy.py +101 -342
- beamlit/deploy/format.py +70 -0
- beamlit/deploy/parser.py +175 -0
- beamlit/functions/decorator.py +17 -14
- beamlit/models/__init__.py +64 -78
- beamlit/models/acl.py +4 -22
- beamlit/models/agent.py +34 -95
- beamlit/models/agent_history.py +7 -5
- beamlit/models/agent_history_event.py +11 -9
- beamlit/models/agent_metadata.py +5 -3
- beamlit/models/agent_spec.py +54 -45
- beamlit/models/api_key.py +14 -14
- beamlit/models/configuration.py +11 -0
- beamlit/models/core_spec.py +45 -32
- beamlit/models/{model_deployment_metrics_query_per_second_per_region_per_code.py → core_spec_configurations.py} +22 -22
- beamlit/models/environment.py +33 -105
- beamlit/models/environment_metadata.py +146 -0
- beamlit/models/environment_spec.py +8 -6
- beamlit/models/function.py +34 -95
- beamlit/models/function_metadata.py +5 -3
- beamlit/models/function_spec.py +53 -40
- beamlit/models/integration_connection.py +31 -137
- beamlit/models/integration_connection_spec.py +6 -4
- beamlit/models/metadata.py +5 -3
- beamlit/models/metadata_labels.py +4 -2
- beamlit/models/model.py +33 -94
- beamlit/models/model_metadata.py +5 -3
- beamlit/models/{function_provider_ref.py → model_private_cluster.py} +23 -14
- beamlit/models/model_provider.py +27 -43
- beamlit/models/model_spec.py +45 -32
- beamlit/models/owner_fields.py +4 -2
- beamlit/models/pending_invitation.py +14 -14
- beamlit/models/pod_template_spec.py +4 -2
- beamlit/models/policy.py +33 -159
- beamlit/models/policy_spec.py +24 -28
- beamlit/models/private_cluster.py +183 -0
- beamlit/models/{model_provider_ref.py → private_location.py} +7 -16
- beamlit/models/resource_deployment_metrics.py +0 -108
- beamlit/models/resource_environment_metrics.py +143 -71
- beamlit/models/{resource_deployment_metrics_inference_per_region.py → resource_environment_metrics_inference_per_region.py} +5 -5
- beamlit/models/resource_environment_metrics_inference_per_second_per_region.py +9 -11
- beamlit/models/{resource_deployment_metrics_query_per_region_per_code.py → resource_environment_metrics_query_per_region_per_code.py} +5 -5
- beamlit/models/resource_environment_metrics_query_per_second_per_region_per_code.py +5 -3
- beamlit/models/resource_log.py +4 -2
- beamlit/models/runtime.py +20 -2
- beamlit/models/serverless_config.py +28 -21
- beamlit/models/spec_configuration.py +7 -5
- beamlit/models/store_agent.py +14 -14
- beamlit/models/store_function.py +14 -14
- beamlit/models/time_fields.py +4 -2
- beamlit/models/websocket_channel.py +4 -2
- beamlit/models/workspace.py +23 -14
- beamlit/run.py +0 -1
- beamlit/serve/app.py +1 -0
- beamlit/serve/middlewares/accesslog.py +3 -1
- {beamlit-0.0.24rc19.dist-info → beamlit-0.0.24rc21.dist-info}/METADATA +1 -1
- beamlit-0.0.24rc21.dist-info/RECORD +243 -0
- beamlit/api/agents/delete_agent_deployment.py +0 -163
- beamlit/api/agents/delete_agent_deployment_history.py +0 -172
- beamlit/api/agents/get_agent_deployment_history.py +0 -172
- beamlit/api/agents/get_agent_deployment_logs.py +0 -164
- beamlit/api/agents/list_agent_deployment_history.py +0 -164
- beamlit/api/agents/put_agent_deployment.py +0 -185
- beamlit/api/agents/put_agent_deployment_history.py +0 -198
- beamlit/api/functions/delete_function_deployment.py +0 -163
- beamlit/api/functions/get_function_deployment.py +0 -163
- beamlit/api/functions/get_function_deployment_logs.py +0 -164
- beamlit/api/functions/get_function_deployment_metrics.py +0 -159
- beamlit/api/functions/put_function_deployment.py +0 -185
- beamlit/api/models/delete_model_deployment.py +0 -171
- beamlit/api/models/get_model_deployment.py +0 -171
- beamlit/api/models/get_model_deployment_logs.py +0 -168
- beamlit/api/models/get_model_deployment_metrics.py +0 -163
- beamlit/api/models/put_model_deployment.py +0 -193
- beamlit/models/agent_configuration.py +0 -70
- beamlit/models/agent_deployment.py +0 -340
- beamlit/models/agent_deployment_configuration.py +0 -45
- beamlit/models/agent_deployment_configuration_type_0.py +0 -43
- beamlit/models/agent_deployment_history.py +0 -185
- beamlit/models/agent_deployment_history_event.py +0 -133
- beamlit/models/agent_deployment_pod_template.py +0 -45
- beamlit/models/agent_deployment_pod_template_type_0.py +0 -43
- beamlit/models/agent_with_deployments.py +0 -176
- beamlit/models/authentication_provider_model.py +0 -144
- beamlit/models/authentication_provider_organization.py +0 -88
- beamlit/models/deployment_configuration.py +0 -70
- beamlit/models/deployment_configurations.py +0 -58
- beamlit/models/deployment_serverless_config.py +0 -131
- beamlit/models/deployment_serverless_config_type_0.py +0 -220
- beamlit/models/function_configuration.py +0 -70
- beamlit/models/function_deployment.py +0 -340
- beamlit/models/function_deployment_configuration.py +0 -45
- beamlit/models/function_deployment_configuration_type_0.py +0 -43
- beamlit/models/function_deployment_pod_template.py +0 -45
- beamlit/models/function_deployment_pod_template_type_0.py +0 -43
- beamlit/models/function_with_deployments.py +0 -176
- beamlit/models/integration.py +0 -198
- beamlit/models/integration_config.py +0 -45
- beamlit/models/integration_secret.py +0 -61
- beamlit/models/labels_type_0.py +0 -45
- beamlit/models/location.py +0 -122
- beamlit/models/model_deployment.py +0 -296
- beamlit/models/model_deployment_log.py +0 -70
- beamlit/models/model_deployment_metrics.py +0 -172
- beamlit/models/model_deployment_metrics_inference_per_second_per_region.py +0 -77
- beamlit/models/model_deployment_pod_template.py +0 -45
- beamlit/models/model_deployment_pod_template_type_0.py +0 -43
- beamlit/models/model_metrics.py +0 -96
- beamlit/models/model_with_deployments.py +0 -176
- beamlit/models/resource_deployment_log.py +0 -70
- beamlit/models/resource_deployment_metrics_inference_per_region_type_0.py +0 -79
- beamlit/models/resource_deployment_metrics_inference_per_second_per_region_type_0.py +0 -79
- beamlit/models/resource_deployment_metrics_query_per_region_per_code_type_0.py +0 -73
- beamlit/models/resource_deployment_metrics_query_per_second_per_region_per_code_type_0.py +0 -73
- beamlit/models/runtime_readiness_probe_type_0.py +0 -43
- beamlit/models/runtime_type_0.py +0 -111
- beamlit/models/runtime_type_0_readiness_probe.py +0 -43
- beamlit/models/runtime_type_0_readiness_probe_type_0.py +0 -43
- beamlit/models/runtime_type_0_resources.py +0 -59
- beamlit/models/standard_fields_dynamo_db.py +0 -88
- beamlit/models/store_agent_configuration.py +0 -97
- beamlit/models/store_agent_labels_type_0.py +0 -43
- beamlit/models/store_function_configuration.py +0 -97
- beamlit/models/store_function_labels_type_0.py +0 -43
- beamlit-0.0.24rc19.dist-info/RECORD +0 -303
- {beamlit-0.0.24rc19.dist-info → beamlit-0.0.24rc21.dist-info}/WHEEL +0 -0
beamlit/deploy/deploy.py
CHANGED
@@ -1,222 +1,45 @@
|
|
1
1
|
import ast
|
2
|
-
import importlib
|
3
2
|
import json
|
4
3
|
import os
|
5
4
|
import sys
|
6
|
-
from dataclasses import dataclass
|
7
5
|
from logging import getLogger
|
8
|
-
from typing import
|
6
|
+
from typing import Literal
|
9
7
|
|
10
8
|
from beamlit.common.settings import Settings, get_settings, init
|
11
|
-
from beamlit.models import (
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
from beamlit.models import (Agent, EnvironmentMetadata, AgentSpec, AgentChain, Flavor, Function, FunctionSpec,
|
10
|
+
Runtime)
|
11
|
+
from .format import arg_to_dict, format_parameters, format_agent_chain
|
12
|
+
from .parser import get_resources, Resource, get_parameters, get_description
|
15
13
|
sys.path.insert(0, os.getcwd())
|
16
14
|
sys.path.insert(0, os.path.join(os.getcwd(), "src"))
|
17
15
|
|
18
16
|
|
19
|
-
|
20
|
-
class Resource:
|
21
|
-
type: Literal["agent", "function"]
|
22
|
-
module: Callable
|
23
|
-
name: str
|
24
|
-
decorator: ast.Call
|
25
|
-
func: Callable
|
26
|
-
|
27
|
-
def get_resources(from_decorator, dir="src") -> list[Resource]:
|
28
|
-
"""
|
29
|
-
Scans through Python files in a directory to find functions decorated with a specific decorator.
|
30
|
-
|
31
|
-
Args:
|
32
|
-
from_decorator (str): The name of the decorator to search for
|
33
|
-
dir (str): The directory to scan, defaults to "src"
|
34
|
-
|
35
|
-
Returns:
|
36
|
-
list[Resource]: List of Resource objects containing information about decorated functions
|
37
|
-
"""
|
38
|
-
resources = []
|
39
|
-
logger = getLogger(__name__)
|
40
|
-
|
41
|
-
# Walk through all Python files in resources directory and subdirectories
|
42
|
-
for root, _, files in os.walk(dir):
|
43
|
-
for file in files:
|
44
|
-
if file.endswith(".py"):
|
45
|
-
file_path = os.path.join(root, file)
|
46
|
-
# Read and compile the file content
|
47
|
-
with open(file_path) as f:
|
48
|
-
try:
|
49
|
-
file_content = f.read()
|
50
|
-
# Parse the file content to find decorated resources
|
51
|
-
tree = ast.parse(file_content)
|
52
|
-
|
53
|
-
# Look for function definitions with decorators
|
54
|
-
for node in ast.walk(tree):
|
55
|
-
if (
|
56
|
-
not isinstance(node, ast.FunctionDef) and not isinstance(node, ast.AsyncFunctionDef)
|
57
|
-
) or len(node.decorator_list) == 0:
|
58
|
-
continue
|
59
|
-
decorator = node.decorator_list[0]
|
60
|
-
|
61
|
-
decorator_name = ""
|
62
|
-
if isinstance(decorator, ast.Call):
|
63
|
-
decorator_name = decorator.func.id
|
64
|
-
if isinstance(decorator, ast.Name):
|
65
|
-
decorator_name = decorator.id
|
66
|
-
if decorator_name == from_decorator:
|
67
|
-
# Get the function name and decorator name
|
68
|
-
func_name = node.name
|
69
|
-
|
70
|
-
# Import the module to get the actual function
|
71
|
-
spec = importlib.util.spec_from_file_location(func_name, file_path)
|
72
|
-
module = importlib.util.module_from_spec(spec)
|
73
|
-
spec.loader.exec_module(module)
|
74
|
-
# Check if kit=True in the decorator arguments
|
75
|
-
|
76
|
-
# Get the decorated function
|
77
|
-
if hasattr(module, func_name) and isinstance(decorator, ast.Call):
|
78
|
-
|
79
|
-
resources.append(
|
80
|
-
Resource(
|
81
|
-
type=decorator_name,
|
82
|
-
module=module,
|
83
|
-
name=func_name,
|
84
|
-
func=getattr(module, func_name),
|
85
|
-
decorator=decorator,
|
86
|
-
)
|
87
|
-
)
|
88
|
-
except Exception as e:
|
89
|
-
logger.warning(f"Error processing {file_path}: {e!s}")
|
90
|
-
return resources
|
91
|
-
|
92
|
-
|
93
|
-
def get_parameters(resource: Resource) -> list[StoreFunctionParameter]:
|
94
|
-
"""
|
95
|
-
Extracts parameter information from a function's signature and docstring.
|
96
|
-
|
97
|
-
Args:
|
98
|
-
resource (Resource): The resource object containing the function to analyze
|
99
|
-
|
100
|
-
Returns:
|
101
|
-
list[StoreFunctionParameter]: List of parameter objects with name, type, required status, and description
|
102
|
-
"""
|
103
|
-
parameters = []
|
104
|
-
# Get function signature
|
105
|
-
import inspect
|
106
|
-
sig = inspect.signature(resource.func)
|
107
|
-
# Get docstring for parameter descriptions
|
108
|
-
docstring = inspect.getdoc(resource.func)
|
109
|
-
param_descriptions = {}
|
110
|
-
if docstring:
|
111
|
-
# Parse docstring for parameter descriptions
|
112
|
-
lines = docstring.split('\n')
|
113
|
-
for line in lines:
|
114
|
-
line = line.strip().lower()
|
115
|
-
if line.startswith(':param '):
|
116
|
-
# Extract parameter name and description
|
117
|
-
param_line = line[7:].split(':', 1)
|
118
|
-
if len(param_line) == 2:
|
119
|
-
param_name = param_line[0].strip()
|
120
|
-
param_desc = param_line[1].strip()
|
121
|
-
param_descriptions[param_name] = param_desc
|
122
|
-
for name, param in sig.parameters.items():
|
123
|
-
# Skip *args and **kwargs parameters
|
124
|
-
if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD):
|
125
|
-
|
126
|
-
continue
|
127
|
-
|
128
|
-
param_type = "string" # Default type
|
129
|
-
type_mapping = {
|
130
|
-
'str': 'string',
|
131
|
-
'int': 'integer',
|
132
|
-
'float': 'number',
|
133
|
-
'bool': 'boolean',
|
134
|
-
'list': 'array',
|
135
|
-
'dict': 'object',
|
136
|
-
'none': 'null'
|
137
|
-
}
|
138
|
-
if param.annotation != inspect.Parameter.empty:
|
139
|
-
# Map Python types to OpenAPI types
|
140
|
-
if hasattr(param.annotation, "__name__"):
|
141
|
-
param_type = param.annotation.__name__.lower()
|
142
|
-
else:
|
143
|
-
# Handle special types like Union, Optional etc
|
144
|
-
param_type = str(param.annotation).lower()
|
145
|
-
parameter = StoreFunctionParameter(
|
146
|
-
name=name,
|
147
|
-
type_=type_mapping.get(param_type, "string"),
|
148
|
-
required=param.default == inspect.Parameter.empty,
|
149
|
-
description=param_descriptions.get(name, f"Parameter {name}")
|
150
|
-
)
|
151
|
-
parameters.append(parameter)
|
152
|
-
|
153
|
-
return parameters
|
154
|
-
|
155
|
-
|
156
|
-
def get_description(description: str | None, resource: Resource) -> str:
|
157
|
-
"""
|
158
|
-
Gets the description of a function from either a provided description or the function's docstring.
|
159
|
-
|
160
|
-
Args:
|
161
|
-
description (str | None): Optional explicit description
|
162
|
-
resource (Resource): The resource object containing the function
|
163
|
-
|
164
|
-
Returns:
|
165
|
-
str: The function description
|
166
|
-
"""
|
167
|
-
if description:
|
168
|
-
return description
|
169
|
-
doc = resource.func.__doc__
|
170
|
-
if doc:
|
171
|
-
# Split docstring into sections and get only the description part
|
172
|
-
doc_lines = doc.split('\n')
|
173
|
-
description_lines = []
|
174
|
-
for line in doc_lines:
|
175
|
-
line = line.strip()
|
176
|
-
# Stop when we hit param/return sections
|
177
|
-
if line.startswith(':param') or line.startswith(':return'):
|
178
|
-
break
|
179
|
-
if line:
|
180
|
-
description_lines.append(line)
|
181
|
-
return ' '.join(description_lines).strip()
|
182
|
-
return ""
|
183
|
-
|
184
|
-
def get_kwargs(arg: ast.Call) -> dict:
|
185
|
-
"""
|
186
|
-
Extracts keyword arguments from an AST Call node.
|
187
|
-
|
188
|
-
Args:
|
189
|
-
arg (ast.Call): The AST Call node to process
|
190
|
-
|
191
|
-
Returns:
|
192
|
-
dict: Dictionary of keyword arguments and their values
|
193
|
-
"""
|
194
|
-
kwargs = {}
|
195
|
-
for keyword in arg.keywords:
|
196
|
-
if isinstance(keyword.value, ast.Constant):
|
197
|
-
kwargs[keyword.arg] = keyword.value.value
|
198
|
-
elif isinstance(keyword.value, (ast.List, ast.Tuple)):
|
199
|
-
kwargs[keyword.arg] = [
|
200
|
-
AgentChain(**get_kwargs(elem)) if isinstance(elem, ast.Call) and isinstance(elem.func, ast.Name) and elem.func.id == "AgentChain"
|
201
|
-
else elem.value if isinstance(elem, ast.Constant) else elem
|
202
|
-
for elem in keyword.value.elts
|
203
|
-
]
|
204
|
-
elif isinstance(keyword.value, ast.Dict):
|
205
|
-
kwargs[keyword.arg] = {}
|
206
|
-
for k, v in zip(keyword.value.keys, keyword.value.values):
|
207
|
-
if isinstance(k, ast.Constant) and isinstance(v, ast.Constant):
|
208
|
-
kwargs[keyword.arg][k.value] = v.value
|
209
|
-
if isinstance(k, ast.Constant) and isinstance(v, ast.Call):
|
210
|
-
kwargs[keyword.arg][k.value] = get_kwargs(v)
|
211
|
-
return kwargs
|
212
|
-
|
213
|
-
def get_runtime(type: str, name: str) -> Runtime:
|
17
|
+
def get_runtime_image(type: str, name: str) -> str:
|
214
18
|
settings = get_settings()
|
215
19
|
registry_url = settings.registry_url.replace("https://", "").replace("http://", "")
|
216
20
|
image = f"{registry_url}/{settings.workspace}/{type}s/{name}"
|
217
|
-
return
|
21
|
+
return image
|
22
|
+
|
218
23
|
|
219
|
-
def
|
24
|
+
def set_default_values(resource: Resource, deployment: Agent | Function):
|
25
|
+
settings = get_settings()
|
26
|
+
deployment.metadata.workspace = settings.workspace
|
27
|
+
deployment.metadata.environment = settings.environment
|
28
|
+
if not deployment.metadata.name:
|
29
|
+
deployment.metadata.name = resource.name
|
30
|
+
if not deployment.metadata.display_name:
|
31
|
+
deployment.metadata.display_name = deployment.metadata.name
|
32
|
+
if not deployment.spec.description:
|
33
|
+
deployment.spec.description = get_description(None, resource)
|
34
|
+
if not deployment.spec.runtime:
|
35
|
+
deployment.spec.runtime = Runtime()
|
36
|
+
if not deployment.spec.runtime.image:
|
37
|
+
deployment.spec.runtime.image = get_runtime_image(resource.type, deployment.metadata.name)
|
38
|
+
return deployment
|
39
|
+
|
40
|
+
def get_beamlit_deployment_from_resource(
|
41
|
+
resource: Resource,
|
42
|
+
) -> Agent | Function:
|
220
43
|
"""
|
221
44
|
Creates a deployment configuration from a resource.
|
222
45
|
|
@@ -224,59 +47,32 @@ def get_beamlit_deployment_from_resource(resource: Resource) -> AgentDeployment
|
|
224
47
|
resource (Resource): The resource to create a deployment for
|
225
48
|
|
226
49
|
Returns:
|
227
|
-
|
50
|
+
Agent | Function: The deployment configuration
|
228
51
|
"""
|
229
|
-
for arg in resource.decorator.args:
|
230
|
-
if isinstance(arg, ast.Call):
|
231
|
-
if isinstance(arg.func, ast.Name) and arg.func.id == "AgentDeployment":
|
232
|
-
kwargs = get_kwargs(arg)
|
233
|
-
description = kwargs.pop("description", None)
|
234
|
-
return AgentDeployment(
|
235
|
-
**kwargs,
|
236
|
-
description=get_description(description, resource),
|
237
|
-
runtime=get_runtime("agent", kwargs.get("agent", resource.name))
|
238
|
-
)
|
239
|
-
if isinstance(arg.func, ast.Name) and arg.func.id == "FunctionDeployment":
|
240
|
-
kwargs = get_kwargs(arg)
|
241
|
-
description = kwargs.pop("description", None)
|
242
|
-
return FunctionDeployment(
|
243
|
-
**kwargs,
|
244
|
-
parameters=get_parameters(resource),
|
245
|
-
description=get_description(description, resource),
|
246
|
-
runtime=get_runtime("function", kwargs.get("function", resource.name))
|
247
|
-
)
|
248
52
|
for arg in resource.decorator.keywords:
|
249
|
-
if
|
250
|
-
if isinstance(arg.value
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
parameters=get_parameters(resource)
|
264
|
-
|
265
|
-
runtime=get_runtime("function", kwargs.get("function", resource.name))
|
266
|
-
)
|
53
|
+
if arg.arg == "agent":
|
54
|
+
if isinstance(arg.value, ast.Dict):
|
55
|
+
value = arg_to_dict(arg.value)
|
56
|
+
metadata = EnvironmentMetadata(**value.get("metadata", {}))
|
57
|
+
spec = AgentSpec(**value.get("spec", {}))
|
58
|
+
agent = Agent(metadata=metadata, spec=spec)
|
59
|
+
return set_default_values(resource, agent)
|
60
|
+
if arg.arg == "function":
|
61
|
+
if isinstance(arg.value, ast.Dict):
|
62
|
+
value = arg_to_dict(arg.value)
|
63
|
+
metadata = EnvironmentMetadata(**value.get("metadata", {}))
|
64
|
+
spec = FunctionSpec(**value.get("spec", {}))
|
65
|
+
func = Function(metadata=metadata, spec=spec)
|
66
|
+
if not func.spec.parameters:
|
67
|
+
func.spec.parameters = get_parameters(resource)
|
68
|
+
return set_default_values(resource, func)
|
267
69
|
if resource.type == "agent":
|
268
|
-
|
269
|
-
|
270
|
-
description=get_description(None,resource),
|
271
|
-
runtime=get_runtime("agent", resource.name)
|
272
|
-
)
|
70
|
+
agent = Agent(metadata=EnvironmentMetadata(), spec=AgentSpec())
|
71
|
+
return set_default_values(resource, agent)
|
273
72
|
if resource.type == "function":
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
description=get_description(None,resource),
|
278
|
-
runtime=get_runtime("function", resource.name)
|
279
|
-
)
|
73
|
+
func = Function(metadata=EnvironmentMetadata(), spec=FunctionSpec())
|
74
|
+
func.spec.parameters = get_parameters(resource)
|
75
|
+
return set_default_values(resource, func)
|
280
76
|
return None
|
281
77
|
|
282
78
|
|
@@ -294,52 +90,9 @@ def get_flavors(flavors: list[Flavor]) -> str:
|
|
294
90
|
return "[]"
|
295
91
|
return json.dumps([flavor.to_dict() for flavor in flavors])
|
296
92
|
|
297
|
-
def
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
Args:
|
302
|
-
parameters (list[StoreFunctionParameter]): List of parameter objects
|
303
|
-
|
304
|
-
Returns:
|
305
|
-
str: YAML-formatted string of parameters
|
306
|
-
"""
|
307
|
-
if not parameters:
|
308
|
-
return "[]"
|
309
|
-
|
310
|
-
formatted = []
|
311
|
-
for param in parameters:
|
312
|
-
formatted.append(f"""
|
313
|
-
- name: {param.name}
|
314
|
-
type: {param.type_}
|
315
|
-
required: {str(param.required).lower()}
|
316
|
-
description: {param.description}""")
|
317
|
-
|
318
|
-
return "\n".join(formatted)
|
319
|
-
|
320
|
-
def format_agent_chain(agent_chain: list[AgentChain]) -> str:
|
321
|
-
"""
|
322
|
-
Formats agent chain configuration into YAML-compatible string.
|
323
|
-
|
324
|
-
Args:
|
325
|
-
agent_chain (list[AgentChain]): List of agent chain configurations
|
326
|
-
|
327
|
-
Returns:
|
328
|
-
str: YAML-formatted string of agent chain
|
329
|
-
"""
|
330
|
-
if not agent_chain:
|
331
|
-
return "[]"
|
332
|
-
formatted = []
|
333
|
-
|
334
|
-
for agent in agent_chain:
|
335
|
-
formatted.append(f"""
|
336
|
-
- agent: {agent.name}
|
337
|
-
enabled: {agent.enabled}""")
|
338
|
-
if agent.description:
|
339
|
-
formatted.append(f" description: {agent.description}")
|
340
|
-
return "\n".join(formatted)
|
341
|
-
|
342
|
-
def get_agent_yaml(agent: AgentDeployment, functions: list[tuple[Resource, FunctionDeployment]], settings: Settings) -> str:
|
93
|
+
def get_agent_yaml(
|
94
|
+
agent: Agent, functions: list[tuple[Resource, Function]], settings: Settings
|
95
|
+
) -> str:
|
343
96
|
"""
|
344
97
|
Generates YAML configuration for an agent deployment.
|
345
98
|
|
@@ -355,25 +108,26 @@ def get_agent_yaml(agent: AgentDeployment, functions: list[tuple[Resource, Funct
|
|
355
108
|
apiVersion: beamlit.com/v1alpha1
|
356
109
|
kind: Agent
|
357
110
|
metadata:
|
358
|
-
name: {agent.
|
111
|
+
name: {agent.metadata.name}
|
112
|
+
display_name: {agent.metadata.display_name or agent.metadata.name}
|
113
|
+
environment: {settings.environment}
|
114
|
+
workspace: {settings.workspace}
|
359
115
|
spec:
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
model: {agent.model}
|
368
|
-
runtime:
|
369
|
-
image: {agent.runtime.image}
|
116
|
+
enabled: true
|
117
|
+
policies: [{", ".join(agent.spec.policies or [])}]
|
118
|
+
functions: [{", ".join([f"{function.metadata.name}" for (_, function) in functions])}]
|
119
|
+
agent_chain: {format_agent_chain(agent.spec.agent_chain)}
|
120
|
+
model: {agent.spec.model}
|
121
|
+
runtime:
|
122
|
+
image: {agent.spec.runtime.image}
|
370
123
|
"""
|
371
|
-
if agent.description:
|
124
|
+
if agent.spec.description:
|
372
125
|
template += f""" description: |
|
373
|
-
{agent.description}"""
|
126
|
+
{agent.spec.description}"""
|
374
127
|
return template
|
375
128
|
|
376
|
-
|
129
|
+
|
130
|
+
def get_function_yaml(function: Function, settings: Settings) -> str:
|
377
131
|
"""
|
378
132
|
Generates YAML configuration for a function deployment.
|
379
133
|
|
@@ -388,28 +142,32 @@ def get_function_yaml(function: FunctionDeployment, settings: Settings) -> str:
|
|
388
142
|
apiVersion: beamlit.com/v1alpha1
|
389
143
|
kind: Function
|
390
144
|
metadata:
|
391
|
-
name: {function.
|
145
|
+
name: {function.metadata.name}
|
146
|
+
display_name: {function.metadata.display_name or function.metadata.name}
|
147
|
+
environment: {settings.environment}
|
392
148
|
spec:
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
parameters: {format_parameters(function.parameters)}
|
401
|
-
runtime:
|
402
|
-
image: {function.runtime.image}
|
149
|
+
enabled: true
|
150
|
+
policies: [{", ".join(function.spec.policies or [])}]
|
151
|
+
description: |
|
152
|
+
{function.spec.description}
|
153
|
+
parameters: {format_parameters(function.spec.parameters)}
|
154
|
+
runtime:
|
155
|
+
image: {function.spec.runtime.image}
|
403
156
|
"""
|
404
157
|
|
405
|
-
|
158
|
+
|
159
|
+
def dockerfile(
|
160
|
+
type: Literal["agent", "function"],
|
161
|
+
resource: Resource,
|
162
|
+
deployment: Agent | Function,
|
163
|
+
) -> str:
|
406
164
|
"""
|
407
165
|
Generates Dockerfile content for agent or function deployment.
|
408
166
|
|
409
167
|
Args:
|
410
168
|
type (Literal["agent", "function"]): Type of deployment
|
411
169
|
resource (Resource): Resource to be deployed
|
412
|
-
deployment (
|
170
|
+
deployment (Agent | Function): Resource configuration
|
413
171
|
|
414
172
|
Returns:
|
415
173
|
str: Dockerfile content
|
@@ -421,7 +179,7 @@ def dockerfile(type: Literal["agent", "function"], resource: Resource, deploymen
|
|
421
179
|
cmd = ["bl", "serve", "--port", "80", "--module", module]
|
422
180
|
if type == "agent":
|
423
181
|
cmd.append("--remote")
|
424
|
-
cmd_str =
|
182
|
+
cmd_str = ",".join([f'"{c}"' for c in cmd])
|
425
183
|
|
426
184
|
return f"""
|
427
185
|
FROM python:3.12-slim
|
@@ -448,6 +206,7 @@ ENV PATH="/beamlit/.venv/bin:$PATH"
|
|
448
206
|
ENTRYPOINT [{cmd_str}]
|
449
207
|
"""
|
450
208
|
|
209
|
+
|
451
210
|
def generate_beamlit_deployment(directory: str):
|
452
211
|
"""
|
453
212
|
Generates all necessary deployment files for Beamlit agents and functions.
|
@@ -463,25 +222,25 @@ def generate_beamlit_deployment(directory: str):
|
|
463
222
|
settings = init()
|
464
223
|
logger = getLogger(__name__)
|
465
224
|
logger.info(f"Importing server module: {settings.server.module}")
|
466
|
-
functions: list[tuple[Resource,
|
467
|
-
agents: list[tuple[Resource,
|
468
|
-
for
|
469
|
-
|
470
|
-
if
|
471
|
-
agents.append((
|
472
|
-
for
|
473
|
-
|
474
|
-
if
|
475
|
-
functions.append((
|
225
|
+
functions: list[tuple[Resource, Function]] = []
|
226
|
+
agents: list[tuple[Resource, Agent]] = []
|
227
|
+
for resource in get_resources("agent"):
|
228
|
+
agent = get_beamlit_deployment_from_resource(resource)
|
229
|
+
if agent:
|
230
|
+
agents.append((resource, agent))
|
231
|
+
for resource in get_resources("function"):
|
232
|
+
function = get_beamlit_deployment_from_resource(resource)
|
233
|
+
if function:
|
234
|
+
functions.append((resource, function))
|
476
235
|
|
477
236
|
agents_dir = os.path.join(directory, "agents")
|
478
237
|
functions_dir = os.path.join(directory, "functions")
|
479
238
|
# Create directory if it doesn't exist
|
480
239
|
os.makedirs(agents_dir, exist_ok=True)
|
481
240
|
os.makedirs(functions_dir, exist_ok=True)
|
482
|
-
for
|
241
|
+
for resource, agent in agents:
|
483
242
|
# write deployment file
|
484
|
-
agent_dir = os.path.join(agents_dir, agent.
|
243
|
+
agent_dir = os.path.join(agents_dir, agent.metadata.name)
|
485
244
|
os.makedirs(agent_dir, exist_ok=True)
|
486
245
|
with open(os.path.join(agent_dir, f"agent.yaml"), "w") as f:
|
487
246
|
content = get_agent_yaml(agent, functions, settings)
|
@@ -490,9 +249,9 @@ def generate_beamlit_deployment(directory: str):
|
|
490
249
|
with open(os.path.join(agent_dir, f"Dockerfile"), "w") as f:
|
491
250
|
content = dockerfile("agent", resource, agent)
|
492
251
|
f.write(content)
|
493
|
-
for
|
252
|
+
for resource, function in functions:
|
494
253
|
# write deployment file
|
495
|
-
function_dir = os.path.join(functions_dir, function.
|
254
|
+
function_dir = os.path.join(functions_dir, function.metadata.name)
|
496
255
|
os.makedirs(function_dir, exist_ok=True)
|
497
256
|
with open(os.path.join(function_dir, f"function.yaml"), "w") as f:
|
498
257
|
content = get_function_yaml(function, settings)
|
@@ -500,4 +259,4 @@ def generate_beamlit_deployment(directory: str):
|
|
500
259
|
# write dockerfile for build
|
501
260
|
with open(os.path.join(function_dir, f"Dockerfile"), "w") as f:
|
502
261
|
content = dockerfile("function", resource, function)
|
503
|
-
f.write(content)
|
262
|
+
f.write(content)
|
beamlit/deploy/format.py
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
import ast
|
3
|
+
from beamlit.models import AgentChain, StoreFunctionParameter
|
4
|
+
|
5
|
+
def arg_to_list(arg: ast.List):
|
6
|
+
value = []
|
7
|
+
for v in arg.elts:
|
8
|
+
value.append(format_value(v))
|
9
|
+
return value
|
10
|
+
|
11
|
+
def format_value(v):
|
12
|
+
if isinstance(v, ast.Constant):
|
13
|
+
return v.value
|
14
|
+
elif isinstance(v, ast.Dict):
|
15
|
+
return arg_to_dict(v)
|
16
|
+
elif isinstance(v, ast.List):
|
17
|
+
return arg_to_list(v)
|
18
|
+
|
19
|
+
def arg_to_dict(arg: ast.keyword):
|
20
|
+
value = {}
|
21
|
+
for k, v in zip(arg.keys, arg.values):
|
22
|
+
if isinstance(k, ast.Constant):
|
23
|
+
value[k.value] = format_value(v)
|
24
|
+
return value
|
25
|
+
|
26
|
+
def format_parameters(parameters: list[StoreFunctionParameter]) -> str:
|
27
|
+
"""
|
28
|
+
Formats function parameters into YAML-compatible string.
|
29
|
+
|
30
|
+
Args:
|
31
|
+
parameters (list[StoreFunctionParameter]): List of parameter objects
|
32
|
+
|
33
|
+
Returns:
|
34
|
+
str: YAML-formatted string of parameters
|
35
|
+
"""
|
36
|
+
if not parameters:
|
37
|
+
return "[]"
|
38
|
+
|
39
|
+
formatted = []
|
40
|
+
for param in parameters:
|
41
|
+
formatted.append(f"""
|
42
|
+
- name: {param.name}
|
43
|
+
type: {param.type_}
|
44
|
+
required: {str(param.required).lower()}
|
45
|
+
description: {param.description}""")
|
46
|
+
|
47
|
+
return "\n".join(formatted)
|
48
|
+
|
49
|
+
|
50
|
+
def format_agent_chain(agent_chain: list[AgentChain]) -> str:
|
51
|
+
"""
|
52
|
+
Formats agent chain configuration into YAML-compatible string.
|
53
|
+
|
54
|
+
Args:
|
55
|
+
agent_chain (list[AgentChain]): List of agent chain configurations
|
56
|
+
|
57
|
+
Returns:
|
58
|
+
str: YAML-formatted string of agent chain
|
59
|
+
"""
|
60
|
+
if not agent_chain:
|
61
|
+
return "[]"
|
62
|
+
formatted = []
|
63
|
+
|
64
|
+
for agent in agent_chain:
|
65
|
+
formatted.append(f"""
|
66
|
+
- agent: {agent.name}
|
67
|
+
enabled: {agent.enabled}""")
|
68
|
+
if agent.description:
|
69
|
+
formatted.append(f" description: {agent.description}")
|
70
|
+
return "\n".join(formatted)
|