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.
Files changed (161) hide show
  1. beamlit/agents/__init__.py +4 -0
  2. beamlit/agents/chat.py +87 -0
  3. beamlit/agents/decorator.py +129 -0
  4. beamlit/api/agents/delete_agent_history.py +6 -2
  5. beamlit/api/agents/get_agent_deployment_logs.py +11 -11
  6. beamlit/api/agents/get_agent_history.py +6 -2
  7. beamlit/api/agents/list_agent_deployment_history.py +11 -11
  8. beamlit/api/agents/list_agent_deployments.py +11 -11
  9. beamlit/api/agents/list_agents.py +11 -11
  10. beamlit/api/agents/put_agent_history.py +6 -2
  11. beamlit/api/environments/list_environments.py +11 -11
  12. beamlit/api/functions/get_function_deployment_logs.py +11 -11
  13. beamlit/api/functions/list_function_deployments.py +11 -11
  14. beamlit/api/functions/list_functions.py +11 -11
  15. beamlit/api/history/get_agents_history.py +11 -11
  16. beamlit/api/history/list_agents_history.py +11 -11
  17. beamlit/api/integrations/list_integration_connections.py +11 -11
  18. beamlit/api/invitations/list_all_pending_invitations.py +11 -11
  19. beamlit/api/locations/list_locations.py +11 -11
  20. beamlit/api/model_providers/list_model_providers.py +11 -11
  21. beamlit/api/models/get_model_deployment_logs.py +11 -11
  22. beamlit/api/models/list_model_deployments.py +11 -11
  23. beamlit/api/models/list_models.py +11 -11
  24. beamlit/api/policies/list_policies.py +11 -11
  25. beamlit/api/service_accounts/get_workspace_service_accounts.py +11 -11
  26. beamlit/api/service_accounts/list_api_keys_for_service_account.py +11 -11
  27. beamlit/api/store/list_store_agents.py +11 -11
  28. beamlit/api/store/list_store_functions.py +11 -11
  29. beamlit/api/workspaces/list_workspace_users.py +11 -11
  30. beamlit/api/workspaces/list_workspaces.py +11 -11
  31. beamlit/authentication/__init__.py +26 -10
  32. beamlit/authentication/apikey.py +4 -4
  33. beamlit/authentication/authentication.py +35 -6
  34. beamlit/authentication/clientcredentials.py +18 -20
  35. beamlit/authentication/credentials.py +9 -8
  36. beamlit/authentication/device_mode.py +15 -12
  37. beamlit/common/__init__.py +13 -0
  38. beamlit/common/generate.py +25 -13
  39. beamlit/common/logger.py +9 -12
  40. beamlit/common/secrets.py +11 -0
  41. beamlit/common/settings.py +29 -11
  42. beamlit/common/utils.py +4 -2
  43. beamlit/functions/__init__.py +5 -0
  44. beamlit/functions/decorator.py +90 -0
  45. beamlit/models/acl.py +2 -2
  46. beamlit/models/agent.py +3 -3
  47. beamlit/models/agent_chain.py +2 -2
  48. beamlit/models/agent_configuration.py +2 -2
  49. beamlit/models/agent_deployment.py +25 -25
  50. beamlit/models/agent_deployment_configuration.py +2 -2
  51. beamlit/models/agent_deployment_history.py +5 -5
  52. beamlit/models/agent_deployment_history_event.py +9 -9
  53. beamlit/models/agent_deployment_pod_template.py +2 -2
  54. beamlit/models/agent_release.py +2 -2
  55. beamlit/models/agent_spec.py +15 -5
  56. beamlit/models/agent_with_deployments.py +6 -6
  57. beamlit/models/api_key.py +2 -2
  58. beamlit/models/authentication_provider_model.py +6 -6
  59. beamlit/models/authentication_provider_organization.py +2 -2
  60. beamlit/models/configuration.py +10 -10
  61. beamlit/models/continent.py +2 -2
  62. beamlit/models/core_spec.py +9 -3
  63. beamlit/models/country.py +2 -2
  64. beamlit/models/create_api_key_for_service_account_body.py +2 -2
  65. beamlit/models/create_workspace_service_account_body.py +2 -2
  66. beamlit/models/create_workspace_service_account_response_200.py +2 -2
  67. beamlit/models/delete_workspace_service_account_response_200.py +2 -2
  68. beamlit/models/deployment_configuration.py +2 -2
  69. beamlit/models/deployment_configurations.py +2 -2
  70. beamlit/models/deployment_serverless_config.py +2 -2
  71. beamlit/models/deployment_serverless_config_type_0.py +3 -1
  72. beamlit/models/environment.py +7 -7
  73. beamlit/models/environment_metrics.py +8 -8
  74. beamlit/models/flavor.py +9 -9
  75. beamlit/models/function.py +3 -3
  76. beamlit/models/function_configuration.py +2 -2
  77. beamlit/models/function_deployment.py +24 -24
  78. beamlit/models/function_deployment_configuration.py +2 -2
  79. beamlit/models/function_deployment_pod_template.py +2 -2
  80. beamlit/models/function_kit.py +5 -5
  81. beamlit/models/function_provider_ref.py +2 -2
  82. beamlit/models/function_release.py +2 -2
  83. beamlit/models/function_spec.py +9 -3
  84. beamlit/models/function_with_deployments.py +6 -6
  85. beamlit/models/get_workspace_service_accounts_response_200_item.py +2 -2
  86. beamlit/models/increase_and_rate_metric.py +5 -5
  87. beamlit/models/integration.py +12 -12
  88. beamlit/models/integration_config.py +2 -2
  89. beamlit/models/integration_connection.py +5 -5
  90. beamlit/models/integration_connection_config.py +2 -2
  91. beamlit/models/integration_connection_secret.py +2 -2
  92. beamlit/models/integration_model.py +6 -6
  93. beamlit/models/integration_secret.py +2 -2
  94. beamlit/models/invite_workspace_user_body.py +2 -2
  95. beamlit/models/labels_type_0.py +2 -2
  96. beamlit/models/location.py +5 -5
  97. beamlit/models/location_response.py +5 -5
  98. beamlit/models/metric.py +2 -2
  99. beamlit/models/metrics.py +8 -8
  100. beamlit/models/model.py +3 -3
  101. beamlit/models/model_deployment.py +18 -18
  102. beamlit/models/model_deployment_log.py +2 -2
  103. beamlit/models/model_deployment_metrics.py +9 -9
  104. beamlit/models/model_deployment_metrics_inference_per_second_per_region.py +5 -5
  105. beamlit/models/model_deployment_metrics_query_per_second_per_region_per_code.py +3 -3
  106. beamlit/models/model_deployment_pod_template.py +2 -2
  107. beamlit/models/model_metrics.py +6 -6
  108. beamlit/models/model_provider.py +11 -11
  109. beamlit/models/model_provider_ref.py +2 -2
  110. beamlit/models/model_release.py +2 -2
  111. beamlit/models/model_spec.py +9 -3
  112. beamlit/models/model_with_deployments.py +6 -6
  113. beamlit/models/pending_invitation.py +2 -2
  114. beamlit/models/pending_invitation_accept.py +3 -3
  115. beamlit/models/pending_invitation_render.py +5 -5
  116. beamlit/models/pending_invitation_render_invited_by.py +2 -2
  117. beamlit/models/pending_invitation_render_workspace.py +2 -2
  118. beamlit/models/pending_invitation_workspace_details.py +6 -6
  119. beamlit/models/policy.py +20 -20
  120. beamlit/models/policy_location.py +9 -9
  121. beamlit/models/policy_spec.py +9 -3
  122. beamlit/models/provider_config.py +7 -7
  123. beamlit/models/qps.py +2 -2
  124. beamlit/models/resource_deployment_log.py +2 -2
  125. beamlit/models/resource_deployment_metrics.py +16 -16
  126. beamlit/models/resource_deployment_metrics_inference_per_region.py +5 -5
  127. beamlit/models/resource_deployment_metrics_inference_per_region_type_0.py +6 -2
  128. beamlit/models/resource_deployment_metrics_inference_per_second_per_region.py +5 -5
  129. beamlit/models/resource_deployment_metrics_inference_per_second_per_region_type_0.py +6 -2
  130. beamlit/models/resource_deployment_metrics_query_per_region_per_code.py +3 -3
  131. beamlit/models/resource_deployment_metrics_query_per_second_per_region_per_code.py +3 -3
  132. beamlit/models/resource_environment_metrics.py +62 -24
  133. beamlit/models/resource_environment_metrics_inference_per_second_per_region.py +6 -2
  134. beamlit/models/resource_metrics.py +10 -10
  135. beamlit/models/runtime.py +23 -23
  136. beamlit/models/runtime_readiness_probe.py +2 -2
  137. beamlit/models/runtime_resources.py +2 -2
  138. beamlit/models/serverless_config.py +2 -2
  139. beamlit/models/standard_fields_dynamo_db.py +2 -2
  140. beamlit/models/store_agent.py +6 -6
  141. beamlit/models/store_agent_configuration.py +2 -2
  142. beamlit/models/store_agent_labels.py +2 -2
  143. beamlit/models/store_configuration.py +16 -16
  144. beamlit/models/store_configuration_option.py +2 -2
  145. beamlit/models/store_function.py +12 -12
  146. beamlit/models/store_function_configuration.py +2 -2
  147. beamlit/models/store_function_kit.py +5 -5
  148. beamlit/models/store_function_labels.py +2 -2
  149. beamlit/models/store_function_parameter.py +9 -9
  150. beamlit/models/update_workspace_service_account_body.py +2 -2
  151. beamlit/models/update_workspace_service_account_response_200.py +2 -2
  152. beamlit/models/update_workspace_user_role_body.py +2 -2
  153. beamlit/models/workspace.py +3 -3
  154. beamlit/models/workspace_labels.py +2 -2
  155. beamlit/models/workspace_user.py +2 -2
  156. beamlit/run.py +6 -6
  157. beamlit/serve.py +120 -0
  158. {beamlit-0.0.20rc5.dist-info → beamlit-0.0.20rc6.dist-info}/METADATA +5 -1
  159. beamlit-0.0.20rc6.dist-info/RECORD +290 -0
  160. beamlit-0.0.20rc5.dist-info/RECORD +0 -282
  161. {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 Credentials, load_credentials_from_settings
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(config.credentials, config.workspace, config.api_url)
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(config.credentials, config.workspace, config.api_url)
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
- 'X-Beamlit-Authorization': f'Bearer {self.credentials.access_token}',
33
- 'X-Beamlit-Workspace': self.workspace_name
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 = { "Authorization": f"Basic {self.credentials.client_credentials}" }
40
- body = { "grant_type": "client_credentials" }
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()['access_token']
44
- self.credentials.refresh_token = response.json()['refresh_token']
45
- self.credentials.expires_in = response.json()['expires_in']
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] + '=' * (-len(parts[1]) % 4))
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['exp'])
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['X-Beamlit-Authorization'] = f'Bearer {self.credentials.access_token}'
70
- request.headers['X-Beamlit-Workspace'] = self.workspace_name
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
- api_key=config.api_key,
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
- 'X-Beamlit-Authorization': f'Bearer {self.credentials.access_token}',
54
- 'X-Beamlit-Workspace': self.workspace_name
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(parts[1] + '=' * (-len(parts[1]) % 4))
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['exp'])
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['X-Beamlit-Authorization'] = f'Bearer {self.credentials.access_token}'
82
- request.headers['X-Beamlit-Workspace'] = self.workspace_name
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
+ ]
@@ -1,4 +1,4 @@
1
- from typing import Dict, Tuple
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
- def generate_function_code(settings: Settings, function: FunctionDeployment, force_name_in_endpoint: str = "", kit: bool = False) -> Tuple[str, str]:
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 f'''
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
- ''', f'Beamlit{get_titles_name(function.function)},'
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 f'''
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
- ''', f'BeamlitChain{name},'
125
+ ''',
126
+ f"BeamlitChain{name},",
127
+ )
128
+
117
129
 
118
130
  def generate(destination: str, dry_run: bool = False):
119
- imports = '''from logging import getLogger
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 = '\n\nfunctions = ['
144
- export_chain = '\n\nchains = ['
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
- 'DEBUG': '\033[1;36m', # Cyan
8
- 'INFO': '\033[1;32m', # Green
9
- 'WARNING': '\033[1;33m', # Yellow
10
- 'ERROR': '\033[1;31m', # Red
11
- 'CRITICAL': '\033[1;41m' # Red background
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, '\033[0m')
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('%(levelname)s:\t %(name)s - %(message)s'))
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])
@@ -0,0 +1,11 @@
1
+ import os
2
+
3
+
4
+ class Secret:
5
+ @staticmethod
6
+ def get(name: str):
7
+ return os.getenv(name, os.getenv(f"bl_{name}"))
8
+
9
+ @staticmethod
10
+ def set(name: str, value: str):
11
+ os.environ[name] = value
@@ -1,26 +1,29 @@
1
1
  import os
2
2
  from logging import getLogger
3
- from typing import Dict, List, Tuple, Type, Union
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
- agent_chain: Union[Unset, List[AgentDeployment]] = UNSET
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 (env_settings, dotenv_settings, file_secret_settings, YamlConfigSettingsSource(settings_cls), init_settings, )
56
-
57
- def init_agent(client: AuthenticatedClient, destination: str = f"{os.getcwd()}/src/beamlit_generated.py"):
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 , client=client)
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, "r").read()
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(f"{source_folder}/{file}", f"{destination_folder}/{file}"):
13
- shutil.copy(f"{source_folder}/{file}", f"{destination_folder}/{file}")
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}")
@@ -0,0 +1,5 @@
1
+ """Functions package providing function decorators and utilities."""
2
+
3
+ from .decorator import function, kit
4
+
5
+ __all__ = ["function", "kit"]