beamlit 0.0.20rc5__py3-none-any.whl → 0.0.20rc7__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 +147 -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 +36 -5
- beamlit/authentication/clientcredentials.py +18 -20
- beamlit/authentication/credentials.py +18 -11
- beamlit/authentication/device_mode.py +15 -12
- beamlit/common/__init__.py +13 -0
- beamlit/common/generate.py +31 -19
- beamlit/common/logger.py +9 -12
- beamlit/common/secrets.py +11 -0
- beamlit/common/settings.py +78 -30
- 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/app.py +73 -0
- beamlit/serve/middlewares/__init__.py +4 -0
- beamlit/serve/middlewares/accesslog.py +14 -0
- beamlit/serve/middlewares/processtime.py +12 -0
- {beamlit-0.0.20rc5.dist-info → beamlit-0.0.20rc7.dist-info}/METADATA +7 -1
- beamlit-0.0.20rc7.dist-info/RECORD +293 -0
- beamlit-0.0.20rc5.dist-info/RECORD +0 -282
- {beamlit-0.0.20rc5.dist-info → beamlit-0.0.20rc7.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:
|
@@ -47,8 +72,15 @@ def new_client_with_credentials(config: RunClientWithCredentials):
|
|
47
72
|
|
48
73
|
return AuthenticatedClient(base_url=config.api_url, provider=provider)
|
49
74
|
|
75
|
+
|
50
76
|
def get_authentication_headers(settings: Settings) -> Dict[str, str]:
|
51
|
-
|
77
|
+
context = current_context()
|
78
|
+
if context.workspace:
|
79
|
+
credentials = load_credentials(context.workspace)
|
80
|
+
else:
|
81
|
+
settings = get_settings()
|
82
|
+
credentials = load_credentials_from_settings(settings)
|
83
|
+
|
52
84
|
config = RunClientWithCredentials(
|
53
85
|
credentials=credentials,
|
54
86
|
workspace=settings.workspace,
|
@@ -63,5 +95,4 @@ def get_authentication_headers(settings: Settings) -> Dict[str, str]:
|
|
63
95
|
|
64
96
|
if provider is None:
|
65
97
|
return None
|
66
|
-
|
67
98
|
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
|
@@ -1,10 +1,14 @@
|
|
1
1
|
from dataclasses import dataclass
|
2
|
+
from logging import getLogger
|
2
3
|
from pathlib import Path
|
3
4
|
from typing import List
|
4
5
|
|
5
6
|
import yaml
|
7
|
+
|
6
8
|
from beamlit.common.settings import Settings
|
7
9
|
|
10
|
+
logger = getLogger(__name__)
|
11
|
+
|
8
12
|
|
9
13
|
@dataclass
|
10
14
|
class Credentials:
|
@@ -15,6 +19,7 @@ class Credentials:
|
|
15
19
|
device_code: str = ""
|
16
20
|
client_credentials: str = ""
|
17
21
|
|
22
|
+
|
18
23
|
@dataclass
|
19
24
|
class WorkspaceConfig:
|
20
25
|
name: str
|
@@ -69,15 +74,15 @@ def save_config(config: Config):
|
|
69
74
|
"name": ws.name,
|
70
75
|
"credentials": {
|
71
76
|
"access_token": ws.credentials.access_token,
|
72
|
-
"api_key": ws.credentials.api_key
|
73
|
-
}
|
77
|
+
"api_key": ws.credentials.api_key,
|
78
|
+
},
|
74
79
|
}
|
75
80
|
for ws in config.workspaces
|
76
81
|
],
|
77
82
|
"context": {
|
78
83
|
"workspace": config.context.workspace,
|
79
|
-
"environment": config.context.environment
|
80
|
-
}
|
84
|
+
"environment": config.context.environment,
|
85
|
+
},
|
81
86
|
}
|
82
87
|
|
83
88
|
home_dir = Path.home()
|
@@ -117,34 +122,36 @@ def load_credentials(workspace_name: str) -> Credentials:
|
|
117
122
|
return workspace.credentials
|
118
123
|
return Credentials()
|
119
124
|
|
120
|
-
|
125
|
+
|
126
|
+
def load_credentials_from_settings(settings: Settings) -> Credentials:
|
121
127
|
return Credentials(
|
122
|
-
api_key=
|
123
|
-
client_credentials=
|
128
|
+
api_key=settings.authentication.api_key,
|
129
|
+
client_credentials=settings.authentication.client_credentials,
|
124
130
|
)
|
125
131
|
|
132
|
+
|
126
133
|
def create_home_dir_if_missing():
|
127
134
|
home_dir = Path.home()
|
128
135
|
if not home_dir:
|
129
|
-
|
136
|
+
logger.error("Error getting home directory")
|
130
137
|
return
|
131
138
|
|
132
139
|
credentials_dir = home_dir / ".beamlit"
|
133
140
|
credentials_file = credentials_dir / "credentials.json"
|
134
141
|
|
135
142
|
if credentials_file.exists():
|
136
|
-
|
143
|
+
logger.warning("You are already logged in. Enter a new API key to overwrite it.")
|
137
144
|
else:
|
138
145
|
try:
|
139
146
|
credentials_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
|
140
147
|
except Exception as e:
|
141
|
-
|
148
|
+
logger.error(f"Error creating credentials directory: {e}")
|
142
149
|
|
143
150
|
|
144
151
|
def save_credentials(workspace_name: str, credentials: Credentials):
|
145
152
|
create_home_dir_if_missing()
|
146
153
|
if not credentials.access_token and not credentials.api_key:
|
147
|
-
|
154
|
+
logger.info("No credentials to save, error")
|
148
155
|
return
|
149
156
|
|
150
157
|
config = load_config()
|
@@ -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,13 +150,13 @@ 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
|
-
if settings.
|
147
|
-
for function_config in settings.
|
158
|
+
if settings.agent.functions and len(settings.agent.functions) > 0:
|
159
|
+
for function_config in settings.agent.functions:
|
148
160
|
if function_config.kit and len(function_config.kit) > 0:
|
149
161
|
new_code, export = generate_kit_function_code(settings, function_config, function_config.kit)
|
150
162
|
code += new_code
|
@@ -153,19 +165,19 @@ run_client = RunClient(client=client)
|
|
153
165
|
new_code, export = generate_function_code(settings, function_config)
|
154
166
|
code += new_code
|
155
167
|
export_code += export
|
156
|
-
if settings.
|
157
|
-
for agent in settings.
|
168
|
+
if settings.agent.chain and len(settings.agent.chain) > 0:
|
169
|
+
for agent in settings.agent.chain:
|
158
170
|
new_code, export = generate_chain_code(settings, agent)
|
159
171
|
code += new_code
|
160
172
|
export_chain += export
|
161
|
-
if settings.
|
173
|
+
if settings.agent.functions and len(settings.agent.functions) > 0:
|
162
174
|
export_code = export_code[:-1]
|
163
|
-
export_code +=
|
164
|
-
if settings.
|
175
|
+
export_code += "]"
|
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])
|