beamlit 0.0.20rc5__py3-none-any.whl → 0.0.20rc6__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- beamlit/agents/__init__.py +4 -0
- beamlit/agents/chat.py +87 -0
- beamlit/agents/decorator.py +129 -0
- beamlit/api/agents/delete_agent_history.py +6 -2
- beamlit/api/agents/get_agent_deployment_logs.py +11 -11
- beamlit/api/agents/get_agent_history.py +6 -2
- beamlit/api/agents/list_agent_deployment_history.py +11 -11
- beamlit/api/agents/list_agent_deployments.py +11 -11
- beamlit/api/agents/list_agents.py +11 -11
- beamlit/api/agents/put_agent_history.py +6 -2
- beamlit/api/environments/list_environments.py +11 -11
- beamlit/api/functions/get_function_deployment_logs.py +11 -11
- beamlit/api/functions/list_function_deployments.py +11 -11
- beamlit/api/functions/list_functions.py +11 -11
- beamlit/api/history/get_agents_history.py +11 -11
- beamlit/api/history/list_agents_history.py +11 -11
- beamlit/api/integrations/list_integration_connections.py +11 -11
- beamlit/api/invitations/list_all_pending_invitations.py +11 -11
- beamlit/api/locations/list_locations.py +11 -11
- beamlit/api/model_providers/list_model_providers.py +11 -11
- beamlit/api/models/get_model_deployment_logs.py +11 -11
- beamlit/api/models/list_model_deployments.py +11 -11
- beamlit/api/models/list_models.py +11 -11
- beamlit/api/policies/list_policies.py +11 -11
- beamlit/api/service_accounts/get_workspace_service_accounts.py +11 -11
- beamlit/api/service_accounts/list_api_keys_for_service_account.py +11 -11
- beamlit/api/store/list_store_agents.py +11 -11
- beamlit/api/store/list_store_functions.py +11 -11
- beamlit/api/workspaces/list_workspace_users.py +11 -11
- beamlit/api/workspaces/list_workspaces.py +11 -11
- beamlit/authentication/__init__.py +26 -10
- beamlit/authentication/apikey.py +4 -4
- beamlit/authentication/authentication.py +35 -6
- beamlit/authentication/clientcredentials.py +18 -20
- beamlit/authentication/credentials.py +9 -8
- beamlit/authentication/device_mode.py +15 -12
- beamlit/common/__init__.py +13 -0
- beamlit/common/generate.py +25 -13
- beamlit/common/logger.py +9 -12
- beamlit/common/secrets.py +11 -0
- beamlit/common/settings.py +29 -11
- beamlit/common/utils.py +4 -2
- beamlit/functions/__init__.py +5 -0
- beamlit/functions/decorator.py +90 -0
- beamlit/models/acl.py +2 -2
- beamlit/models/agent.py +3 -3
- beamlit/models/agent_chain.py +2 -2
- beamlit/models/agent_configuration.py +2 -2
- beamlit/models/agent_deployment.py +25 -25
- beamlit/models/agent_deployment_configuration.py +2 -2
- beamlit/models/agent_deployment_history.py +5 -5
- beamlit/models/agent_deployment_history_event.py +9 -9
- beamlit/models/agent_deployment_pod_template.py +2 -2
- beamlit/models/agent_release.py +2 -2
- beamlit/models/agent_spec.py +15 -5
- beamlit/models/agent_with_deployments.py +6 -6
- beamlit/models/api_key.py +2 -2
- beamlit/models/authentication_provider_model.py +6 -6
- beamlit/models/authentication_provider_organization.py +2 -2
- beamlit/models/configuration.py +10 -10
- beamlit/models/continent.py +2 -2
- beamlit/models/core_spec.py +9 -3
- beamlit/models/country.py +2 -2
- beamlit/models/create_api_key_for_service_account_body.py +2 -2
- beamlit/models/create_workspace_service_account_body.py +2 -2
- beamlit/models/create_workspace_service_account_response_200.py +2 -2
- beamlit/models/delete_workspace_service_account_response_200.py +2 -2
- beamlit/models/deployment_configuration.py +2 -2
- beamlit/models/deployment_configurations.py +2 -2
- beamlit/models/deployment_serverless_config.py +2 -2
- beamlit/models/deployment_serverless_config_type_0.py +3 -1
- beamlit/models/environment.py +7 -7
- beamlit/models/environment_metrics.py +8 -8
- beamlit/models/flavor.py +9 -9
- beamlit/models/function.py +3 -3
- beamlit/models/function_configuration.py +2 -2
- beamlit/models/function_deployment.py +24 -24
- beamlit/models/function_deployment_configuration.py +2 -2
- beamlit/models/function_deployment_pod_template.py +2 -2
- beamlit/models/function_kit.py +5 -5
- beamlit/models/function_provider_ref.py +2 -2
- beamlit/models/function_release.py +2 -2
- beamlit/models/function_spec.py +9 -3
- beamlit/models/function_with_deployments.py +6 -6
- beamlit/models/get_workspace_service_accounts_response_200_item.py +2 -2
- beamlit/models/increase_and_rate_metric.py +5 -5
- beamlit/models/integration.py +12 -12
- beamlit/models/integration_config.py +2 -2
- beamlit/models/integration_connection.py +5 -5
- beamlit/models/integration_connection_config.py +2 -2
- beamlit/models/integration_connection_secret.py +2 -2
- beamlit/models/integration_model.py +6 -6
- beamlit/models/integration_secret.py +2 -2
- beamlit/models/invite_workspace_user_body.py +2 -2
- beamlit/models/labels_type_0.py +2 -2
- beamlit/models/location.py +5 -5
- beamlit/models/location_response.py +5 -5
- beamlit/models/metric.py +2 -2
- beamlit/models/metrics.py +8 -8
- beamlit/models/model.py +3 -3
- beamlit/models/model_deployment.py +18 -18
- beamlit/models/model_deployment_log.py +2 -2
- beamlit/models/model_deployment_metrics.py +9 -9
- beamlit/models/model_deployment_metrics_inference_per_second_per_region.py +5 -5
- beamlit/models/model_deployment_metrics_query_per_second_per_region_per_code.py +3 -3
- beamlit/models/model_deployment_pod_template.py +2 -2
- beamlit/models/model_metrics.py +6 -6
- beamlit/models/model_provider.py +11 -11
- beamlit/models/model_provider_ref.py +2 -2
- beamlit/models/model_release.py +2 -2
- beamlit/models/model_spec.py +9 -3
- beamlit/models/model_with_deployments.py +6 -6
- beamlit/models/pending_invitation.py +2 -2
- beamlit/models/pending_invitation_accept.py +3 -3
- beamlit/models/pending_invitation_render.py +5 -5
- beamlit/models/pending_invitation_render_invited_by.py +2 -2
- beamlit/models/pending_invitation_render_workspace.py +2 -2
- beamlit/models/pending_invitation_workspace_details.py +6 -6
- beamlit/models/policy.py +20 -20
- beamlit/models/policy_location.py +9 -9
- beamlit/models/policy_spec.py +9 -3
- beamlit/models/provider_config.py +7 -7
- beamlit/models/qps.py +2 -2
- beamlit/models/resource_deployment_log.py +2 -2
- beamlit/models/resource_deployment_metrics.py +16 -16
- beamlit/models/resource_deployment_metrics_inference_per_region.py +5 -5
- beamlit/models/resource_deployment_metrics_inference_per_region_type_0.py +6 -2
- beamlit/models/resource_deployment_metrics_inference_per_second_per_region.py +5 -5
- beamlit/models/resource_deployment_metrics_inference_per_second_per_region_type_0.py +6 -2
- beamlit/models/resource_deployment_metrics_query_per_region_per_code.py +3 -3
- beamlit/models/resource_deployment_metrics_query_per_second_per_region_per_code.py +3 -3
- beamlit/models/resource_environment_metrics.py +62 -24
- beamlit/models/resource_environment_metrics_inference_per_second_per_region.py +6 -2
- beamlit/models/resource_metrics.py +10 -10
- beamlit/models/runtime.py +23 -23
- beamlit/models/runtime_readiness_probe.py +2 -2
- beamlit/models/runtime_resources.py +2 -2
- beamlit/models/serverless_config.py +2 -2
- beamlit/models/standard_fields_dynamo_db.py +2 -2
- beamlit/models/store_agent.py +6 -6
- beamlit/models/store_agent_configuration.py +2 -2
- beamlit/models/store_agent_labels.py +2 -2
- beamlit/models/store_configuration.py +16 -16
- beamlit/models/store_configuration_option.py +2 -2
- beamlit/models/store_function.py +12 -12
- beamlit/models/store_function_configuration.py +2 -2
- beamlit/models/store_function_kit.py +5 -5
- beamlit/models/store_function_labels.py +2 -2
- beamlit/models/store_function_parameter.py +9 -9
- beamlit/models/update_workspace_service_account_body.py +2 -2
- beamlit/models/update_workspace_service_account_response_200.py +2 -2
- beamlit/models/update_workspace_user_role_body.py +2 -2
- beamlit/models/workspace.py +3 -3
- beamlit/models/workspace_labels.py +2 -2
- beamlit/models/workspace_user.py +2 -2
- beamlit/run.py +6 -6
- beamlit/serve.py +120 -0
- {beamlit-0.0.20rc5.dist-info → beamlit-0.0.20rc6.dist-info}/METADATA +5 -1
- beamlit-0.0.20rc6.dist-info/RECORD +290 -0
- beamlit-0.0.20rc5.dist-info/RECORD +0 -282
- {beamlit-0.0.20rc5.dist-info → beamlit-0.0.20rc6.dist-info}/WHEEL +0 -0
@@ -1,13 +1,19 @@
|
|
1
1
|
from dataclasses import dataclass
|
2
2
|
from typing import Dict, Generator
|
3
3
|
|
4
|
-
from beamlit.common.settings import Settings
|
5
4
|
from httpx import Auth, Request, Response
|
6
5
|
|
6
|
+
from beamlit.common.settings import Settings, get_settings
|
7
|
+
|
7
8
|
from ..client import AuthenticatedClient
|
8
9
|
from .apikey import ApiKeyProvider
|
9
10
|
from .clientcredentials import ClientCredentials
|
10
|
-
from .credentials import
|
11
|
+
from .credentials import (
|
12
|
+
Credentials,
|
13
|
+
current_context,
|
14
|
+
load_credentials,
|
15
|
+
load_credentials_from_settings,
|
16
|
+
)
|
11
17
|
from .device_mode import BearerToken
|
12
18
|
|
13
19
|
|
@@ -16,7 +22,6 @@ class PublicProvider(Auth):
|
|
16
22
|
yield request
|
17
23
|
|
18
24
|
|
19
|
-
|
20
25
|
@dataclass
|
21
26
|
class RunClientWithCredentials:
|
22
27
|
credentials: Credentials
|
@@ -34,6 +39,26 @@ def new_client_from_settings(settings: Settings):
|
|
34
39
|
)
|
35
40
|
return new_client_with_credentials(client_config)
|
36
41
|
|
42
|
+
|
43
|
+
def new_client():
|
44
|
+
context = current_context()
|
45
|
+
if context.workspace:
|
46
|
+
credentials = load_credentials(context.workspace)
|
47
|
+
client_config = RunClientWithCredentials(
|
48
|
+
credentials=credentials,
|
49
|
+
workspace=context.workspace,
|
50
|
+
)
|
51
|
+
else:
|
52
|
+
settings = get_settings()
|
53
|
+
credentials = load_credentials_from_settings(settings)
|
54
|
+
|
55
|
+
client_config = RunClientWithCredentials(
|
56
|
+
credentials=credentials,
|
57
|
+
workspace=settings.workspace,
|
58
|
+
)
|
59
|
+
return new_client_with_credentials(client_config)
|
60
|
+
|
61
|
+
|
37
62
|
def new_client_with_credentials(config: RunClientWithCredentials):
|
38
63
|
provider: Auth = None
|
39
64
|
if config.credentials.api_key:
|
@@ -41,12 +66,15 @@ def new_client_with_credentials(config: RunClientWithCredentials):
|
|
41
66
|
elif config.credentials.access_token:
|
42
67
|
provider = BearerToken(config.credentials, config.workspace, config.api_url)
|
43
68
|
elif config.credentials.client_credentials:
|
44
|
-
provider = ClientCredentials(
|
69
|
+
provider = ClientCredentials(
|
70
|
+
config.credentials, config.workspace, config.api_url
|
71
|
+
)
|
45
72
|
else:
|
46
73
|
provider = PublicProvider()
|
47
74
|
|
48
75
|
return AuthenticatedClient(base_url=config.api_url, provider=provider)
|
49
76
|
|
77
|
+
|
50
78
|
def get_authentication_headers(settings: Settings) -> Dict[str, str]:
|
51
79
|
credentials = load_credentials_from_settings(settings)
|
52
80
|
config = RunClientWithCredentials(
|
@@ -59,9 +87,10 @@ def get_authentication_headers(settings: Settings) -> Dict[str, str]:
|
|
59
87
|
elif config.credentials.access_token:
|
60
88
|
provider = BearerToken(config.credentials, config.workspace, config.api_url)
|
61
89
|
elif config.credentials.client_credentials:
|
62
|
-
provider = ClientCredentials(
|
90
|
+
provider = ClientCredentials(
|
91
|
+
config.credentials, config.workspace, config.api_url
|
92
|
+
)
|
63
93
|
|
64
94
|
if provider is None:
|
65
95
|
return None
|
66
|
-
|
67
96
|
return provider.get_headers()
|
@@ -5,9 +5,10 @@ from dataclasses import dataclass
|
|
5
5
|
from typing import Generator, Optional
|
6
6
|
|
7
7
|
import requests
|
8
|
-
from beamlit.common.settings import get_settings
|
9
8
|
from httpx import Auth, Request, Response, post
|
10
9
|
|
10
|
+
from beamlit.common.settings import get_settings
|
11
|
+
|
11
12
|
|
12
13
|
@dataclass
|
13
14
|
class DeviceLoginFinalizeResponse:
|
@@ -29,32 +30,32 @@ class ClientCredentials(Auth):
|
|
29
30
|
raise err
|
30
31
|
|
31
32
|
return {
|
32
|
-
|
33
|
-
|
33
|
+
"X-Beamlit-Authorization": f"Bearer {self.credentials.access_token}",
|
34
|
+
"X-Beamlit-Workspace": self.workspace_name,
|
34
35
|
}
|
35
36
|
|
36
37
|
def refresh_if_needed(self) -> Optional[Exception]:
|
37
38
|
settings = get_settings()
|
38
39
|
if self.credentials.client_credentials and not self.credentials.refresh_token:
|
39
|
-
headers = {
|
40
|
-
body = {
|
40
|
+
headers = {"Authorization": f"Basic {self.credentials.client_credentials}"}
|
41
|
+
body = {"grant_type": "client_credentials"}
|
41
42
|
response = requests.post(f"{settings.base_url}/oauth/token", headers=headers, json=body)
|
42
43
|
response.raise_for_status()
|
43
|
-
self.credentials.access_token = response.json()[
|
44
|
-
self.credentials.refresh_token = response.json()[
|
45
|
-
self.credentials.expires_in = response.json()[
|
44
|
+
self.credentials.access_token = response.json()["access_token"]
|
45
|
+
self.credentials.refresh_token = response.json()["refresh_token"]
|
46
|
+
self.credentials.expires_in = response.json()["expires_in"]
|
46
47
|
|
47
48
|
# Need to refresh token if expires in less than 10 minutes
|
48
|
-
parts = self.credentials.access_token.split(
|
49
|
+
parts = self.credentials.access_token.split(".")
|
49
50
|
if len(parts) != 3:
|
50
51
|
return Exception("Invalid JWT token format")
|
51
52
|
try:
|
52
|
-
claims_bytes = base64.urlsafe_b64decode(parts[1] +
|
53
|
+
claims_bytes = base64.urlsafe_b64decode(parts[1] + "=" * (-len(parts[1]) % 4))
|
53
54
|
claims = json.loads(claims_bytes)
|
54
55
|
except Exception as e:
|
55
56
|
return Exception(f"Failed to decode/parse JWT claims: {str(e)}")
|
56
57
|
|
57
|
-
exp_time = time.gmtime(claims[
|
58
|
+
exp_time = time.gmtime(claims["exp"])
|
58
59
|
# Refresh if token expires in less than 10 minutes
|
59
60
|
if time.time() + (10 * 60) > time.mktime(exp_time):
|
60
61
|
return self.do_refresh()
|
@@ -66,8 +67,8 @@ class ClientCredentials(Auth):
|
|
66
67
|
if err:
|
67
68
|
return err
|
68
69
|
|
69
|
-
request.headers[
|
70
|
-
request.headers[
|
70
|
+
request.headers["X-Beamlit-Authorization"] = f"Bearer {self.credentials.access_token}"
|
71
|
+
request.headers["X-Beamlit-Workspace"] = self.workspace_name
|
71
72
|
yield request
|
72
73
|
|
73
74
|
def do_refresh(self) -> Optional[Exception]:
|
@@ -79,15 +80,11 @@ class ClientCredentials(Auth):
|
|
79
80
|
"grant_type": "refresh_token",
|
80
81
|
"refresh_token": self.credentials.refresh_token,
|
81
82
|
"device_code": self.credentials.device_code,
|
82
|
-
"client_id": "beamlit"
|
83
|
+
"client_id": "beamlit",
|
83
84
|
}
|
84
85
|
|
85
86
|
try:
|
86
|
-
response = post(
|
87
|
-
url,
|
88
|
-
json=refresh_data,
|
89
|
-
headers={"Content-Type": "application/json"}
|
90
|
-
)
|
87
|
+
response = post(url, json=refresh_data, headers={"Content-Type": "application/json"})
|
91
88
|
response.raise_for_status()
|
92
89
|
finalize_response = DeviceLoginFinalizeResponse(**response.json())
|
93
90
|
|
@@ -95,11 +92,12 @@ class ClientCredentials(Auth):
|
|
95
92
|
finalize_response.refresh_token = self.credentials.refresh_token
|
96
93
|
|
97
94
|
from .credentials import Credentials, save_credentials
|
95
|
+
|
98
96
|
creds = Credentials(
|
99
97
|
access_token=finalize_response.access_token,
|
100
98
|
refresh_token=finalize_response.refresh_token,
|
101
99
|
expires_in=finalize_response.expires_in,
|
102
|
-
device_code=self.credentials.device_code
|
100
|
+
device_code=self.credentials.device_code,
|
103
101
|
)
|
104
102
|
|
105
103
|
self.credentials = creds
|
@@ -3,6 +3,7 @@ from pathlib import Path
|
|
3
3
|
from typing import List
|
4
4
|
|
5
5
|
import yaml
|
6
|
+
|
6
7
|
from beamlit.common.settings import Settings
|
7
8
|
|
8
9
|
|
@@ -15,6 +16,7 @@ class Credentials:
|
|
15
16
|
device_code: str = ""
|
16
17
|
client_credentials: str = ""
|
17
18
|
|
19
|
+
|
18
20
|
@dataclass
|
19
21
|
class WorkspaceConfig:
|
20
22
|
name: str
|
@@ -69,15 +71,15 @@ def save_config(config: Config):
|
|
69
71
|
"name": ws.name,
|
70
72
|
"credentials": {
|
71
73
|
"access_token": ws.credentials.access_token,
|
72
|
-
"api_key": ws.credentials.api_key
|
73
|
-
}
|
74
|
+
"api_key": ws.credentials.api_key,
|
75
|
+
},
|
74
76
|
}
|
75
77
|
for ws in config.workspaces
|
76
78
|
],
|
77
79
|
"context": {
|
78
80
|
"workspace": config.context.workspace,
|
79
|
-
"environment": config.context.environment
|
80
|
-
}
|
81
|
+
"environment": config.context.environment,
|
82
|
+
},
|
81
83
|
}
|
82
84
|
|
83
85
|
home_dir = Path.home()
|
@@ -117,11 +119,10 @@ def load_credentials(workspace_name: str) -> Credentials:
|
|
117
119
|
return workspace.credentials
|
118
120
|
return Credentials()
|
119
121
|
|
122
|
+
|
120
123
|
def load_credentials_from_settings(config: Settings) -> Credentials:
|
121
|
-
return Credentials(
|
122
|
-
|
123
|
-
client_credentials=config.client_credentials
|
124
|
-
)
|
124
|
+
return Credentials(api_key=config.api_key, client_credentials=config.client_credentials)
|
125
|
+
|
125
126
|
|
126
127
|
def create_home_dir_if_missing():
|
127
128
|
home_dir = Path.home()
|
@@ -50,23 +50,25 @@ class BearerToken(Auth):
|
|
50
50
|
if err:
|
51
51
|
raise err
|
52
52
|
return {
|
53
|
-
|
54
|
-
|
53
|
+
"X-Beamlit-Authorization": f"Bearer {self.credentials.access_token}",
|
54
|
+
"X-Beamlit-Workspace": self.workspace_name,
|
55
55
|
}
|
56
56
|
|
57
57
|
def refresh_if_needed(self) -> Optional[Exception]:
|
58
58
|
# Need to refresh token if expires in less than 10 minutes
|
59
|
-
parts = self.credentials.access_token.split(
|
59
|
+
parts = self.credentials.access_token.split(".")
|
60
60
|
if len(parts) != 3:
|
61
61
|
return Exception("Invalid JWT token format")
|
62
62
|
|
63
63
|
try:
|
64
|
-
claims_bytes = base64.urlsafe_b64decode(
|
64
|
+
claims_bytes = base64.urlsafe_b64decode(
|
65
|
+
parts[1] + "=" * (-len(parts[1]) % 4)
|
66
|
+
)
|
65
67
|
claims = json.loads(claims_bytes)
|
66
68
|
except Exception as e:
|
67
69
|
return Exception(f"Failed to decode/parse JWT claims: {str(e)}")
|
68
70
|
|
69
|
-
exp_time = time.gmtime(claims[
|
71
|
+
exp_time = time.gmtime(claims["exp"])
|
70
72
|
# Refresh if token expires in less than 10 minutes
|
71
73
|
if time.time() + (10 * 60) > time.mktime(exp_time):
|
72
74
|
return self.do_refresh()
|
@@ -78,8 +80,10 @@ class BearerToken(Auth):
|
|
78
80
|
if err:
|
79
81
|
return err
|
80
82
|
|
81
|
-
request.headers[
|
82
|
-
|
83
|
+
request.headers["X-Beamlit-Authorization"] = (
|
84
|
+
f"Bearer {self.credentials.access_token}"
|
85
|
+
)
|
86
|
+
request.headers["X-Beamlit-Workspace"] = self.workspace_name
|
83
87
|
yield request
|
84
88
|
|
85
89
|
def do_refresh(self) -> Optional[Exception]:
|
@@ -91,14 +95,12 @@ class BearerToken(Auth):
|
|
91
95
|
"grant_type": "refresh_token",
|
92
96
|
"refresh_token": self.credentials.refresh_token,
|
93
97
|
"device_code": self.credentials.device_code,
|
94
|
-
"client_id": "beamlit"
|
98
|
+
"client_id": "beamlit",
|
95
99
|
}
|
96
100
|
|
97
101
|
try:
|
98
102
|
response = post(
|
99
|
-
url,
|
100
|
-
json=refresh_data,
|
101
|
-
headers={"Content-Type": "application/json"}
|
103
|
+
url, json=refresh_data, headers={"Content-Type": "application/json"}
|
102
104
|
)
|
103
105
|
response.raise_for_status()
|
104
106
|
finalize_response = DeviceLoginFinalizeResponse(**response.json())
|
@@ -107,11 +109,12 @@ class BearerToken(Auth):
|
|
107
109
|
finalize_response.refresh_token = self.credentials.refresh_token
|
108
110
|
|
109
111
|
from .credentials import Credentials, save_credentials
|
112
|
+
|
110
113
|
creds = Credentials(
|
111
114
|
access_token=finalize_response.access_token,
|
112
115
|
refresh_token=finalize_response.refresh_token,
|
113
116
|
expires_in=finalize_response.expires_in,
|
114
|
-
device_code=self.credentials.device_code
|
117
|
+
device_code=self.credentials.device_code,
|
115
118
|
)
|
116
119
|
|
117
120
|
self.credentials = creds
|
@@ -0,0 +1,13 @@
|
|
1
|
+
from .logger import init as init_logger
|
2
|
+
from .secrets import Secret
|
3
|
+
from .settings import Settings, get_settings, init_agent
|
4
|
+
from .utils import copy_folder
|
5
|
+
|
6
|
+
__all__ = [
|
7
|
+
"Secret",
|
8
|
+
"Settings",
|
9
|
+
"get_settings",
|
10
|
+
"init_agent",
|
11
|
+
"copy_folder",
|
12
|
+
"init_logger",
|
13
|
+
]
|
beamlit/common/generate.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import
|
1
|
+
from typing import Tuple
|
2
2
|
|
3
3
|
from beamlit.common.settings import Settings, get_settings
|
4
4
|
from beamlit.models.agent_deployment import AgentDeployment
|
@@ -9,6 +9,7 @@ from beamlit.models.function_kit import FunctionKit
|
|
9
9
|
def get_titles_name(name: str) -> str:
|
10
10
|
return name.title().replace("-", "").replace("_", "")
|
11
11
|
|
12
|
+
|
12
13
|
def generate_kit_function_code(settings: Settings, function: FunctionDeployment, kit: FunctionKit) -> Tuple[str, str]:
|
13
14
|
export_code = ""
|
14
15
|
code = ""
|
@@ -24,7 +25,10 @@ def generate_kit_function_code(settings: Settings, function: FunctionDeployment,
|
|
24
25
|
export_code += export
|
25
26
|
return code, export_code
|
26
27
|
|
27
|
-
|
28
|
+
|
29
|
+
def generate_function_code(
|
30
|
+
settings: Settings, function: FunctionDeployment, force_name_in_endpoint: str = "", kit: bool = False
|
31
|
+
) -> Tuple[str, str]:
|
28
32
|
name = get_titles_name(function.function)
|
29
33
|
if function.parameters and len(function.parameters) > 0:
|
30
34
|
args_list = ", ".join(f"{param.name}: str" for param in function.parameters)
|
@@ -55,7 +59,8 @@ def generate_function_code(settings: Settings, function: FunctionDeployment, for
|
|
55
59
|
if len(body) > 0:
|
56
60
|
body += ", "
|
57
61
|
body += f'"name": "{function.function}"'
|
58
|
-
return
|
62
|
+
return (
|
63
|
+
f'''
|
59
64
|
|
60
65
|
class Beamlit{name}Input(BaseModel):
|
61
66
|
{args_schema}
|
@@ -78,13 +83,17 @@ class Beamlit{name}(BaseTool):
|
|
78
83
|
return response.json(), {{}}
|
79
84
|
except Exception as e:
|
80
85
|
return repr(e), {{}}
|
81
|
-
''',
|
86
|
+
''',
|
87
|
+
f"Beamlit{get_titles_name(function.function)},",
|
88
|
+
)
|
89
|
+
|
82
90
|
|
83
91
|
def generate_chain_code(settings: Settings, agent: AgentDeployment) -> Tuple[str, str]:
|
84
92
|
name = get_titles_name(agent.agent)
|
85
93
|
# TODO: add return direct in agent configuration
|
86
94
|
return_direct = False
|
87
|
-
return
|
95
|
+
return (
|
96
|
+
f'''
|
88
97
|
class BeamlitChain{name}Input(BaseModel):
|
89
98
|
input: str = Field(description='{agent.description}')
|
90
99
|
|
@@ -113,10 +122,13 @@ class BeamlitChain{name}(BaseTool):
|
|
113
122
|
return response.text, {{}}
|
114
123
|
except Exception as e:
|
115
124
|
return repr(e), {{}}
|
116
|
-
''',
|
125
|
+
''',
|
126
|
+
f"BeamlitChain{name},",
|
127
|
+
)
|
128
|
+
|
117
129
|
|
118
130
|
def generate(destination: str, dry_run: bool = False):
|
119
|
-
imports =
|
131
|
+
imports = """from logging import getLogger
|
120
132
|
from typing import Dict, List, Literal, Optional, Tuple, Type, Union
|
121
133
|
|
122
134
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
@@ -138,10 +150,10 @@ client_config = RunClientWithCredentials(
|
|
138
150
|
)
|
139
151
|
client = new_client_with_credentials(client_config)
|
140
152
|
run_client = RunClient(client=client)
|
141
|
-
|
153
|
+
"""
|
142
154
|
settings = get_settings()
|
143
|
-
export_code =
|
144
|
-
export_chain =
|
155
|
+
export_code = "\n\nfunctions = ["
|
156
|
+
export_chain = "\n\nchains = ["
|
145
157
|
code = imports
|
146
158
|
if settings.agent_functions and len(settings.agent_functions) > 0:
|
147
159
|
for function_config in settings.agent_functions:
|
@@ -160,12 +172,12 @@ run_client = RunClient(client=client)
|
|
160
172
|
export_chain += export
|
161
173
|
if settings.agent_functions and len(settings.agent_functions) > 0:
|
162
174
|
export_code = export_code[:-1]
|
163
|
-
export_code +=
|
175
|
+
export_code += "]"
|
164
176
|
if settings.agent_chain and len(settings.agent_chain) > 0:
|
165
177
|
export_chain = export_chain[:-1]
|
166
|
-
export_chain +=
|
178
|
+
export_chain += "]"
|
167
179
|
content = code + export_code + export_chain
|
168
180
|
if not dry_run:
|
169
181
|
with open(destination, "w") as f:
|
170
182
|
f.write(content)
|
171
|
-
return content
|
183
|
+
return content
|
beamlit/common/logger.py
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
import logging
|
2
|
-
import os
|
3
2
|
|
4
3
|
|
5
4
|
class ColoredFormatter(logging.Formatter):
|
6
5
|
COLORS = {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
6
|
+
"DEBUG": "\033[1;36m", # Cyan
|
7
|
+
"INFO": "\033[1;32m", # Green
|
8
|
+
"WARNING": "\033[1;33m", # Yellow
|
9
|
+
"ERROR": "\033[1;31m", # Red
|
10
|
+
"CRITICAL": "\033[1;41m", # Red background
|
12
11
|
}
|
13
12
|
|
14
13
|
def format(self, record):
|
15
|
-
color = self.COLORS.get(record.levelname,
|
14
|
+
color = self.COLORS.get(record.levelname, "\033[0m")
|
16
15
|
record.levelname = f"{color}{record.levelname}\033[0m"
|
17
16
|
return super().format(record)
|
18
17
|
|
18
|
+
|
19
19
|
def init(log_level: str):
|
20
20
|
logging.getLogger("uvicorn.access").handlers.clear()
|
21
21
|
logging.getLogger("uvicorn.access").propagate = False
|
@@ -25,8 +25,5 @@ def init(log_level: str):
|
|
25
25
|
logging.getLogger("httpx").propagate = False
|
26
26
|
|
27
27
|
handler = logging.StreamHandler()
|
28
|
-
handler.setFormatter(ColoredFormatter(
|
29
|
-
logging.basicConfig(
|
30
|
-
level=log_level,
|
31
|
-
handlers=[handler]
|
32
|
-
)
|
28
|
+
handler.setFormatter(ColoredFormatter("%(levelname)s:\t %(name)s - %(message)s"))
|
29
|
+
logging.basicConfig(level=log_level, handlers=[handler])
|
beamlit/common/settings.py
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
import os
|
2
2
|
from logging import getLogger
|
3
|
-
from typing import
|
3
|
+
from typing import Any, List, Tuple, Type, Union
|
4
|
+
|
5
|
+
from langchain_core.language_models.chat_models import BaseChatModel
|
6
|
+
from langgraph.graph.graph import CompiledGraph
|
7
|
+
from pydantic import Field
|
8
|
+
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, YamlConfigSettingsSource
|
4
9
|
|
5
10
|
from beamlit.api.functions import get_function_deployment
|
6
11
|
from beamlit.api.models import get_model_deployment
|
7
12
|
from beamlit.client import AuthenticatedClient
|
8
13
|
from beamlit.common.logger import init as init_logger
|
9
|
-
from beamlit.models.agent_chain import AgentChain
|
10
14
|
from beamlit.models.agent_deployment import AgentDeployment
|
11
15
|
from beamlit.models.function_deployment import FunctionDeployment
|
12
16
|
from beamlit.models.model_deployment import ModelDeployment
|
13
17
|
from beamlit.types import UNSET, Unset
|
14
|
-
from pydantic import Field
|
15
|
-
from pydantic_settings import (BaseSettings, PydanticBaseSettingsSource,
|
16
|
-
SettingsConfigDict, YamlConfigSettingsSource)
|
17
18
|
|
18
19
|
global SETTINGS
|
19
20
|
SETTINGS = None
|
20
21
|
|
22
|
+
|
21
23
|
def get_settings():
|
22
24
|
return SETTINGS
|
23
25
|
|
26
|
+
|
24
27
|
class Settings(BaseSettings):
|
25
28
|
model_config = SettingsConfigDict(
|
26
29
|
yaml_file="beamlit.yaml",
|
@@ -39,9 +42,13 @@ class Settings(BaseSettings):
|
|
39
42
|
api_key: Union[None, str] = None
|
40
43
|
jwt: Union[None, str] = None
|
41
44
|
client_credentials: Union[None, str] = None
|
42
|
-
|
45
|
+
agent_module: str = Field(default="main.main")
|
46
|
+
agent_chain: Union[Unset, List[AgentDeployment]] = UNSET
|
43
47
|
agent_functions: Union[Unset, List[FunctionDeployment]] = UNSET
|
44
48
|
agent_model: Union[Unset, ModelDeployment] = UNSET
|
49
|
+
agent: Union[None, CompiledGraph, BaseChatModel] = None
|
50
|
+
agent_chat_model: Union[None, BaseChatModel] = None
|
51
|
+
agent_functions: Union[None, List[Any]] = None
|
45
52
|
|
46
53
|
@classmethod
|
47
54
|
def settings_customise_sources(
|
@@ -52,9 +59,19 @@ class Settings(BaseSettings):
|
|
52
59
|
dotenv_settings: PydanticBaseSettingsSource,
|
53
60
|
file_secret_settings: PydanticBaseSettingsSource,
|
54
61
|
) -> Tuple[PydanticBaseSettingsSource, ...]:
|
55
|
-
return (
|
56
|
-
|
57
|
-
|
62
|
+
return (
|
63
|
+
env_settings,
|
64
|
+
dotenv_settings,
|
65
|
+
file_secret_settings,
|
66
|
+
YamlConfigSettingsSource(settings_cls),
|
67
|
+
init_settings,
|
68
|
+
)
|
69
|
+
|
70
|
+
|
71
|
+
def init_agent(
|
72
|
+
client: AuthenticatedClient,
|
73
|
+
destination: str = f"{os.getcwd()}/src/beamlit_generated.py",
|
74
|
+
):
|
58
75
|
from beamlit.api.agents import get_agent_deployment
|
59
76
|
from beamlit.common.generate import generate
|
60
77
|
|
@@ -86,18 +103,19 @@ def init_agent(client: AuthenticatedClient, destination: str = f"{os.getcwd()}/s
|
|
86
103
|
agent_chain_deployments.append(agent_deployment)
|
87
104
|
SETTINGS.agent_chain = agent_chain_deployments
|
88
105
|
if agent_deployment.model:
|
89
|
-
model_deployment = get_model_deployment.sync(agent_deployment.model, env
|
106
|
+
model_deployment = get_model_deployment.sync(agent_deployment.model, env, client=client)
|
90
107
|
SETTINGS.agent_model = model_deployment
|
91
108
|
|
92
109
|
content_generate = generate(destination, dry_run=True)
|
93
110
|
compared_content = None
|
94
111
|
if os.path.exists(destination):
|
95
|
-
compared_content = open(destination
|
112
|
+
compared_content = open(destination).read()
|
96
113
|
|
97
114
|
if not os.path.exists(destination) or (compared_content and content_generate != compared_content):
|
98
115
|
logger.info("Generating agent code")
|
99
116
|
generate(destination)
|
100
117
|
|
118
|
+
|
101
119
|
def init() -> Settings:
|
102
120
|
"""Parse the beamlit.yaml file to get configurations."""
|
103
121
|
global SETTINGS
|
beamlit/common/utils.py
CHANGED
@@ -9,5 +9,7 @@ def copy_folder(source_folder: str, destination_folder: str):
|
|
9
9
|
if not os.path.exists(f"{destination_folder}/{file}"):
|
10
10
|
os.makedirs(f"{destination_folder}/{file}")
|
11
11
|
copy_folder(f"{source_folder}/{file}", f"{destination_folder}/{file}")
|
12
|
-
elif not os.path.exists(f"{destination_folder}/{file}") or not filecmp.cmp(
|
13
|
-
|
12
|
+
elif not os.path.exists(f"{destination_folder}/{file}") or not filecmp.cmp(
|
13
|
+
f"{source_folder}/{file}", f"{destination_folder}/{file}"
|
14
|
+
):
|
15
|
+
shutil.copy(f"{source_folder}/{file}", f"{destination_folder}/{file}")
|