beamlit 0.0.41rc85__py3-none-any.whl → 0.0.41rc86__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- beamlit/agents/chat.py +2 -1
- beamlit/common/settings.py +6 -2
- beamlit/functions/__init__.py +2 -1
- beamlit/functions/common.py +143 -0
- beamlit/functions/decorator.py +1 -137
- beamlit/functions/mcp/mcp.py +0 -1
- beamlit/functions/remote/remote.py +2 -1
- {beamlit-0.0.41rc85.dist-info → beamlit-0.0.41rc86.dist-info}/METADATA +1 -1
- {beamlit-0.0.41rc85.dist-info → beamlit-0.0.41rc86.dist-info}/RECORD +10 -9
- {beamlit-0.0.41rc85.dist-info → beamlit-0.0.41rc86.dist-info}/WHEEL +0 -0
beamlit/agents/chat.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
from logging import getLogger
|
2
2
|
from typing import Tuple, Union
|
3
3
|
|
4
|
+
from langchain_core.language_models import BaseChatModel
|
5
|
+
|
4
6
|
from beamlit.api.models import get_model
|
5
7
|
from beamlit.authentication import get_authentication_headers, new_client
|
6
8
|
from beamlit.common.settings import get_settings
|
7
9
|
from beamlit.models import Model
|
8
|
-
from langchain_core.language_models import BaseChatModel
|
9
10
|
|
10
11
|
logger = getLogger(__name__)
|
11
12
|
|
beamlit/common/settings.py
CHANGED
@@ -4,8 +4,12 @@ from typing import Tuple, Type, Union
|
|
4
4
|
from langchain_core.language_models.chat_models import BaseChatModel
|
5
5
|
from langgraph.graph.graph import CompiledGraph
|
6
6
|
from pydantic import Field
|
7
|
-
from pydantic_settings import (
|
8
|
-
|
7
|
+
from pydantic_settings import (
|
8
|
+
BaseSettings,
|
9
|
+
PydanticBaseSettingsSource,
|
10
|
+
SettingsConfigDict,
|
11
|
+
YamlConfigSettingsSource,
|
12
|
+
)
|
9
13
|
|
10
14
|
from beamlit.common.logger import init as init_logger
|
11
15
|
from beamlit.models import Agent, Function, Model
|
beamlit/functions/__init__.py
CHANGED
@@ -0,0 +1,143 @@
|
|
1
|
+
"""Decorators for creating function tools with Beamlit and LangChain integration."""
|
2
|
+
import ast
|
3
|
+
import asyncio
|
4
|
+
import importlib.util
|
5
|
+
import os
|
6
|
+
from logging import getLogger
|
7
|
+
from typing import Union
|
8
|
+
|
9
|
+
from langchain_core.tools import StructuredTool
|
10
|
+
from langchain_core.tools.base import create_schema_from_function
|
11
|
+
|
12
|
+
from beamlit.authentication import new_client
|
13
|
+
from beamlit.client import AuthenticatedClient
|
14
|
+
from beamlit.common import slugify
|
15
|
+
from beamlit.common.settings import get_settings
|
16
|
+
from beamlit.functions.remote.remote import RemoteToolkit
|
17
|
+
from beamlit.models import AgentChain
|
18
|
+
|
19
|
+
logger = getLogger(__name__)
|
20
|
+
|
21
|
+
def get_functions(
|
22
|
+
remote_functions:Union[list[str], None]=None,
|
23
|
+
client:Union[AuthenticatedClient, None]=None,
|
24
|
+
dir:Union[str, None]=None,
|
25
|
+
chain:Union[list[AgentChain], None]=None,
|
26
|
+
remote_functions_empty:bool=True,
|
27
|
+
from_decorator:str="function",
|
28
|
+
warning:bool=True,
|
29
|
+
):
|
30
|
+
from beamlit.agents.chain import ChainToolkit
|
31
|
+
|
32
|
+
settings = get_settings()
|
33
|
+
if client is None:
|
34
|
+
client = new_client()
|
35
|
+
if dir is None:
|
36
|
+
dir = settings.agent.functions_directory
|
37
|
+
|
38
|
+
functions = []
|
39
|
+
logger = getLogger(__name__)
|
40
|
+
settings = get_settings()
|
41
|
+
|
42
|
+
# Walk through all Python files in functions directory and subdirectories
|
43
|
+
if not os.path.exists(dir):
|
44
|
+
if remote_functions_empty and warning:
|
45
|
+
logger.warn(f"Functions directory {dir} not found")
|
46
|
+
if os.path.exists(dir):
|
47
|
+
for root, _, files in os.walk(dir):
|
48
|
+
for file in files:
|
49
|
+
if file.endswith(".py"):
|
50
|
+
file_path = os.path.join(root, file)
|
51
|
+
# Read and compile the file content
|
52
|
+
with open(file_path) as f:
|
53
|
+
try:
|
54
|
+
file_content = f.read()
|
55
|
+
# Parse the file content to find decorated functions
|
56
|
+
tree = ast.parse(file_content)
|
57
|
+
|
58
|
+
# Look for function definitions with decorators
|
59
|
+
for node in ast.walk(tree):
|
60
|
+
if (
|
61
|
+
not isinstance(node, ast.FunctionDef)
|
62
|
+
and not isinstance(node, ast.AsyncFunctionDef)
|
63
|
+
) or len(node.decorator_list) == 0:
|
64
|
+
continue
|
65
|
+
decorator = node.decorator_list[0]
|
66
|
+
|
67
|
+
decorator_name = ""
|
68
|
+
if isinstance(decorator, ast.Call):
|
69
|
+
decorator_name = decorator.func.id
|
70
|
+
if isinstance(decorator, ast.Name):
|
71
|
+
decorator_name = decorator.id
|
72
|
+
if decorator_name == from_decorator:
|
73
|
+
# Get the function name and decorator name
|
74
|
+
func_name = node.name
|
75
|
+
|
76
|
+
# Import the module to get the actual function
|
77
|
+
spec = importlib.util.spec_from_file_location(func_name, file_path)
|
78
|
+
module = importlib.util.module_from_spec(spec)
|
79
|
+
spec.loader.exec_module(module)
|
80
|
+
# Check if kit=True in the decorator arguments
|
81
|
+
is_kit = False
|
82
|
+
if isinstance(decorator, ast.Call):
|
83
|
+
for keyword in decorator.keywords:
|
84
|
+
if keyword.arg == "kit" and isinstance(
|
85
|
+
keyword.value, ast.Constant
|
86
|
+
):
|
87
|
+
is_kit = keyword.value.value
|
88
|
+
if is_kit and not settings.remote:
|
89
|
+
kit_functions = get_functions(
|
90
|
+
client=client,
|
91
|
+
dir=os.path.join(root),
|
92
|
+
remote_functions_empty=remote_functions_empty,
|
93
|
+
from_decorator="kit",
|
94
|
+
)
|
95
|
+
functions.extend(kit_functions)
|
96
|
+
|
97
|
+
# Get the decorated function
|
98
|
+
if not is_kit and hasattr(module, func_name):
|
99
|
+
func = getattr(module, func_name)
|
100
|
+
if settings.remote:
|
101
|
+
toolkit = RemoteToolkit(client, slugify(func.__name__))
|
102
|
+
toolkit.initialize()
|
103
|
+
functions.extend(toolkit.get_tools())
|
104
|
+
else:
|
105
|
+
if asyncio.iscoroutinefunction(func):
|
106
|
+
functions.append(
|
107
|
+
StructuredTool(
|
108
|
+
name=func.__name__,
|
109
|
+
description=func.__doc__,
|
110
|
+
func=func,
|
111
|
+
coroutine=func,
|
112
|
+
args_schema=create_schema_from_function(func.__name__, func)
|
113
|
+
)
|
114
|
+
)
|
115
|
+
else:
|
116
|
+
|
117
|
+
functions.append(
|
118
|
+
StructuredTool(
|
119
|
+
name=func.__name__,
|
120
|
+
description=func.__doc__,
|
121
|
+
func=func,
|
122
|
+
args_schema=create_schema_from_function(func.__name__, func)
|
123
|
+
)
|
124
|
+
)
|
125
|
+
except Exception as e:
|
126
|
+
logger.warning(f"Error processing {file_path}: {e!s}")
|
127
|
+
|
128
|
+
if remote_functions:
|
129
|
+
for function in remote_functions:
|
130
|
+
try:
|
131
|
+
toolkit = RemoteToolkit(client, function)
|
132
|
+
toolkit.initialize()
|
133
|
+
functions.extend(toolkit.get_tools())
|
134
|
+
except Exception as e:
|
135
|
+
logger.warn(f"Failed to initialize remote function {function}: {e!s}")
|
136
|
+
|
137
|
+
if chain:
|
138
|
+
toolkit = ChainToolkit(client, chain)
|
139
|
+
toolkit.initialize()
|
140
|
+
functions.extend(toolkit.get_tools())
|
141
|
+
|
142
|
+
return functions
|
143
|
+
|
beamlit/functions/decorator.py
CHANGED
@@ -1,151 +1,15 @@
|
|
1
1
|
"""Decorators for creating function tools with Beamlit and LangChain integration."""
|
2
|
-
import ast
|
3
2
|
import asyncio
|
4
3
|
import functools
|
5
|
-
import importlib.util
|
6
|
-
import os
|
7
4
|
from collections.abc import Callable
|
8
5
|
from logging import getLogger
|
9
|
-
from typing import Union
|
10
6
|
|
11
7
|
from fastapi import Request
|
12
|
-
from langchain_core.tools import StructuredTool
|
13
|
-
from langchain_core.tools.base import create_schema_from_function
|
14
8
|
|
15
|
-
from beamlit.
|
16
|
-
from beamlit.client import AuthenticatedClient
|
17
|
-
from beamlit.common import slugify
|
18
|
-
from beamlit.common.settings import get_settings
|
19
|
-
from beamlit.functions.remote.remote import RemoteToolkit
|
20
|
-
from beamlit.models import AgentChain, Function, FunctionKit
|
9
|
+
from beamlit.models import Function, FunctionKit
|
21
10
|
|
22
11
|
logger = getLogger(__name__)
|
23
12
|
|
24
|
-
def get_functions(
|
25
|
-
remote_functions:Union[list[str], None]=None,
|
26
|
-
client:Union[AuthenticatedClient, None]=None,
|
27
|
-
dir:Union[str, None]=None,
|
28
|
-
chain:Union[list[AgentChain], None]=None,
|
29
|
-
remote_functions_empty:bool=True,
|
30
|
-
from_decorator:str="function",
|
31
|
-
warning:bool=True,
|
32
|
-
):
|
33
|
-
from beamlit.agents.chain import ChainToolkit
|
34
|
-
|
35
|
-
settings = get_settings()
|
36
|
-
if client is None:
|
37
|
-
client = new_client()
|
38
|
-
if dir is None:
|
39
|
-
dir = settings.agent.functions_directory
|
40
|
-
|
41
|
-
functions = []
|
42
|
-
logger = getLogger(__name__)
|
43
|
-
settings = get_settings()
|
44
|
-
|
45
|
-
# Walk through all Python files in functions directory and subdirectories
|
46
|
-
if not os.path.exists(dir):
|
47
|
-
if remote_functions_empty and warning:
|
48
|
-
logger.warn(f"Functions directory {dir} not found")
|
49
|
-
if os.path.exists(dir):
|
50
|
-
for root, _, files in os.walk(dir):
|
51
|
-
for file in files:
|
52
|
-
if file.endswith(".py"):
|
53
|
-
file_path = os.path.join(root, file)
|
54
|
-
# Read and compile the file content
|
55
|
-
with open(file_path) as f:
|
56
|
-
try:
|
57
|
-
file_content = f.read()
|
58
|
-
# Parse the file content to find decorated functions
|
59
|
-
tree = ast.parse(file_content)
|
60
|
-
|
61
|
-
# Look for function definitions with decorators
|
62
|
-
for node in ast.walk(tree):
|
63
|
-
if (
|
64
|
-
not isinstance(node, ast.FunctionDef)
|
65
|
-
and not isinstance(node, ast.AsyncFunctionDef)
|
66
|
-
) or len(node.decorator_list) == 0:
|
67
|
-
continue
|
68
|
-
decorator = node.decorator_list[0]
|
69
|
-
|
70
|
-
decorator_name = ""
|
71
|
-
if isinstance(decorator, ast.Call):
|
72
|
-
decorator_name = decorator.func.id
|
73
|
-
if isinstance(decorator, ast.Name):
|
74
|
-
decorator_name = decorator.id
|
75
|
-
if decorator_name == from_decorator:
|
76
|
-
# Get the function name and decorator name
|
77
|
-
func_name = node.name
|
78
|
-
|
79
|
-
# Import the module to get the actual function
|
80
|
-
spec = importlib.util.spec_from_file_location(func_name, file_path)
|
81
|
-
module = importlib.util.module_from_spec(spec)
|
82
|
-
spec.loader.exec_module(module)
|
83
|
-
# Check if kit=True in the decorator arguments
|
84
|
-
is_kit = False
|
85
|
-
if isinstance(decorator, ast.Call):
|
86
|
-
for keyword in decorator.keywords:
|
87
|
-
if keyword.arg == "kit" and isinstance(
|
88
|
-
keyword.value, ast.Constant
|
89
|
-
):
|
90
|
-
is_kit = keyword.value.value
|
91
|
-
if is_kit and not settings.remote:
|
92
|
-
kit_functions = get_functions(
|
93
|
-
client=client,
|
94
|
-
dir=os.path.join(root),
|
95
|
-
remote_functions_empty=remote_functions_empty,
|
96
|
-
from_decorator="kit",
|
97
|
-
)
|
98
|
-
functions.extend(kit_functions)
|
99
|
-
|
100
|
-
# Get the decorated function
|
101
|
-
if not is_kit and hasattr(module, func_name):
|
102
|
-
func = getattr(module, func_name)
|
103
|
-
if settings.remote:
|
104
|
-
toolkit = RemoteToolkit(client, slugify(func.__name__))
|
105
|
-
toolkit.initialize()
|
106
|
-
functions.extend(toolkit.get_tools())
|
107
|
-
else:
|
108
|
-
if asyncio.iscoroutinefunction(func):
|
109
|
-
functions.append(
|
110
|
-
StructuredTool(
|
111
|
-
name=func.__name__,
|
112
|
-
description=func.__doc__,
|
113
|
-
func=func,
|
114
|
-
coroutine=func,
|
115
|
-
args_schema=create_schema_from_function(func.__name__, func)
|
116
|
-
)
|
117
|
-
)
|
118
|
-
else:
|
119
|
-
|
120
|
-
functions.append(
|
121
|
-
StructuredTool(
|
122
|
-
name=func.__name__,
|
123
|
-
description=func.__doc__,
|
124
|
-
func=func,
|
125
|
-
args_schema=create_schema_from_function(func.__name__, func)
|
126
|
-
)
|
127
|
-
)
|
128
|
-
except Exception as e:
|
129
|
-
logger.warning(f"Error processing {file_path}: {e!s}")
|
130
|
-
|
131
|
-
if remote_functions:
|
132
|
-
for function in remote_functions:
|
133
|
-
try:
|
134
|
-
toolkit = RemoteToolkit(client, function)
|
135
|
-
toolkit.initialize()
|
136
|
-
functions.extend(toolkit.get_tools())
|
137
|
-
except Exception as e:
|
138
|
-
logger.warn(f"Failed to initialize remote function {function}: {e!s}")
|
139
|
-
|
140
|
-
if chain:
|
141
|
-
toolkit = ChainToolkit(client, chain)
|
142
|
-
toolkit.initialize()
|
143
|
-
functions.extend(toolkit.get_tools())
|
144
|
-
|
145
|
-
return functions
|
146
|
-
|
147
|
-
|
148
|
-
|
149
13
|
def kit(bl_kit: FunctionKit = None, **kwargs: dict) -> Callable:
|
150
14
|
"""Create function tools with Beamlit and LangChain integration."""
|
151
15
|
|
beamlit/functions/mcp/mcp.py
CHANGED
@@ -5,6 +5,8 @@ from typing import Callable
|
|
5
5
|
|
6
6
|
import pydantic
|
7
7
|
import typing_extensions as t
|
8
|
+
from langchain_core.tools.base import BaseTool, ToolException
|
9
|
+
|
8
10
|
from beamlit.api.functions import get_function, list_functions
|
9
11
|
from beamlit.authentication.authentication import AuthenticatedClient
|
10
12
|
from beamlit.common.settings import get_settings
|
@@ -12,7 +14,6 @@ from beamlit.errors import UnexpectedStatus
|
|
12
14
|
from beamlit.functions.mcp.mcp import MCPClient, MCPToolkit
|
13
15
|
from beamlit.models import Function, StoreFunctionParameter
|
14
16
|
from beamlit.run import RunClient
|
15
|
-
from langchain_core.tools.base import BaseTool, ToolException
|
16
17
|
|
17
18
|
|
18
19
|
def create_dynamic_schema(name: str, parameters: list[StoreFunctionParameter]) -> type[pydantic.BaseModel]:
|
@@ -6,7 +6,7 @@ beamlit/run.py,sha256=HtDYDjD7oVfQ8r3T5_t4qN5UDJOJfsQILi45Z21ArAg,1446
|
|
6
6
|
beamlit/types.py,sha256=E1hhDh_zXfsSQ0NCt9-uw90_Mr5iIlsdfnfvxv5HarU,1005
|
7
7
|
beamlit/agents/__init__.py,sha256=bWsFaXUbAps3IsL3Prti89m1s714vICXodbQi77h3vY,206
|
8
8
|
beamlit/agents/chain.py,sha256=vfCjiFHuu02uTTGicxMlFzjyICQkIjpXrBGs-7uJEsg,2826
|
9
|
-
beamlit/agents/chat.py,sha256=
|
9
|
+
beamlit/agents/chat.py,sha256=6iZuaP25hUvrKdth-6sbtqxkZ1wM0Fv13eRttxNZlIU,4715
|
10
10
|
beamlit/agents/decorator.py,sha256=R5evLoproCgYjKXmH-Vq31r6G2lzZvUitXD1NoTflcA,6014
|
11
11
|
beamlit/agents/thread.py,sha256=LN5Ss-uOf5_hdB0WV1dqpn-N-pDJB3C2hUvlCzdqtdk,519
|
12
12
|
beamlit/api/__init__.py,sha256=zTSiG_ujSjAqWPyc435YXaX9XTlpMjiJWBbV-f-YtdA,45
|
@@ -135,17 +135,18 @@ beamlit/common/error.py,sha256=f9oJDFxhoHK-vpjxBgEp0NwWIk0N_THPemUI7uQxVzU,270
|
|
135
135
|
beamlit/common/instrumentation.py,sha256=L4Y6Xu7rQwIHngfp-d0LtWxReaPte6rIzykvkDaIyzE,9288
|
136
136
|
beamlit/common/logger.py,sha256=nN_dSOl4bs13QU3Rod-w3e3jYOnlSrHx3_bs-ACY6Aw,1115
|
137
137
|
beamlit/common/secrets.py,sha256=sid81bOe3LflkMKDHwBsBs9nIju8bp5-v9qU9gkyNMc,212
|
138
|
-
beamlit/common/settings.py,sha256=
|
138
|
+
beamlit/common/settings.py,sha256=C7iZrLLvl6ap5VCVCYOoK20WdSFqacmd6HtaeZ7SF6s,3760
|
139
139
|
beamlit/common/slugify.py,sha256=nR29r37IdWS2i44ZC6ZsXRgqKPYmvMGtFQ7BuIQUTlc,90
|
140
140
|
beamlit/common/utils.py,sha256=jouz5igBvT37Xn_e94-foCHyQczVim-UzVcoIF6RWJ4,657
|
141
141
|
beamlit/deploy/__init__.py,sha256=GS7l7Jtm2yKs7iNLKcfjYO-rAhUzggQ3xiYSf3oxLBY,91
|
142
142
|
beamlit/deploy/deploy.py,sha256=J5VvXWeyIyz6CWy2ctld28T7sVfas-kiJZ1nx6Zw7t0,10374
|
143
143
|
beamlit/deploy/format.py,sha256=U6UZEFAYLnGJJ7O2YmSdlUUFhnWNGAv6NZ-DW4KTgvI,2049
|
144
144
|
beamlit/deploy/parser.py,sha256=Ga0poCZkoRnuTw082QnTcNGCBJncoRAnVsn8-1FsaJE,6907
|
145
|
-
beamlit/functions/__init__.py,sha256=
|
146
|
-
beamlit/functions/
|
147
|
-
beamlit/functions/
|
148
|
-
beamlit/functions/
|
145
|
+
beamlit/functions/__init__.py,sha256=oejZ6GVd4-2kGKsOwRUq1u2AuSyrrn9kWIuH6WxMqhE,189
|
146
|
+
beamlit/functions/common.py,sha256=HbP_F7IYywB2jqxobjkjFqDaIsJXym-HPeOfLzoHSBo,7014
|
147
|
+
beamlit/functions/decorator.py,sha256=ZtSQsPLI70WKwi2jPhA7DaqREQUINpqt9BDVugeV_sg,1714
|
148
|
+
beamlit/functions/mcp/mcp.py,sha256=JnkeIh9jDyiZstBA0OH57aGx2JjEJ_DPtnM17Xw6ykM,4060
|
149
|
+
beamlit/functions/remote/remote.py,sha256=pY7gsmonkH8iN1anscMIWX-tW__OaBw14bodSUy1lCE,4845
|
149
150
|
beamlit/models/__init__.py,sha256=DB2EBTVX5lTO7CzXvZaqiPtgc9n8SMPu59zGD5_Vvno,9652
|
150
151
|
beamlit/models/acl.py,sha256=tH67gsl_BMaviSbTaaIkO1g9cWZgJ6VgAnYVjQSzGZY,3952
|
151
152
|
beamlit/models/agent.py,sha256=w2f_d_nLGVHW7blklH34Nmm2TI3MCoEiQN3HMVga5XI,4002
|
@@ -277,6 +278,6 @@ beamlit/serve/app.py,sha256=ROS_tb9cO4GvOQKCwloyAzpYraTdIb3oG6sChXikeNw,3285
|
|
277
278
|
beamlit/serve/middlewares/__init__.py,sha256=1dVmnOmhAQWvWktqHkKSIX-YoF6fmMU8xkUQuhg_rJU,148
|
278
279
|
beamlit/serve/middlewares/accesslog.py,sha256=Mu4T4_9OvHybjA0ApzZFpgi2C8f3X1NbUk-76v634XM,631
|
279
280
|
beamlit/serve/middlewares/processtime.py,sha256=lDAaIasZ4bwvN-HKHvZpaD9r-yrkVNZYx4abvbjbrCg,411
|
280
|
-
beamlit-0.0.
|
281
|
-
beamlit-0.0.
|
282
|
-
beamlit-0.0.
|
281
|
+
beamlit-0.0.41rc86.dist-info/METADATA,sha256=pVWwJpoKf9vfO83lbZWKk_Dek4MCfbIeA_-hBpf0aTk,3715
|
282
|
+
beamlit-0.0.41rc86.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
283
|
+
beamlit-0.0.41rc86.dist-info/RECORD,,
|
File without changes
|