forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
142 lines (107 loc) · 4.46 KB
/
utils.py
File metadata and controls
142 lines (107 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2025 © BeeAI a Series of LF Projects, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from importlib import import_module
from typing import Any, Literal, TypeVar, Union
import json_repair
import jsonref # type: ignore
from openai.lib._pydantic import to_strict_json_schema
from pydantic import ConfigDict, Field, RootModel, create_model
from beeai_framework.backend.constants import (
BackendProviders,
ProviderDef,
ProviderModelDef,
ProviderName,
)
from beeai_framework.backend.errors import BackendError
from beeai_framework.backend.types import ChatModelToolChoice
from beeai_framework.tools.tool import AnyTool, Tool
T = TypeVar("T")
# TODO: `${ProviderName}:${string}`
FullModelName: str
def find_provider_def(value: str) -> ProviderDef | None:
for provider in BackendProviders.values():
if value == provider.name or value == provider.module or value in provider.aliases:
return provider
return None
def parse_model(name: str) -> ProviderModelDef:
if not name:
raise BackendError("Neither 'provider' nor 'provider:model' was specified.")
# provider_id:model_id
# e.g., ollama:llama3.1
# keep remainder of string intact (maxsplit=1) because model name can also have colons
name_parts = name.split(":", maxsplit=1)
provider_def = find_provider_def(name_parts[0])
if not provider_def:
raise BackendError("Model does not contain provider name!")
return ProviderModelDef(
provider_id=name_parts[0],
model_id=name_parts[1] if len(name_parts) > 1 else None,
provider_def=provider_def,
)
def load_model(name: ProviderName | str, model_type: Literal["embedding", "chat"] = "chat") -> type[T]:
parsed = parse_model(name)
provider_def = parsed.provider_def
module_path = f"beeai_framework.adapters.{provider_def.module}.backend.{model_type}"
module = import_module(module_path)
class_name = f"{provider_def.name}{model_type.capitalize()}Model"
return getattr(module, class_name) # type: ignore
def parse_broken_json(input: str) -> Any:
return json_repair.loads(input)
def inline_schema_refs(schema: dict[str, Any], *, force: bool = False) -> dict[str, Any]:
if schema.get("$defs") is not None or force is True:
schema = jsonref.replace_refs(
schema, base_uri="", load_on_repr=True, merge_props=True, proxies=False, lazy_load=False
)
schema.pop("$defs", None)
return schema
def generate_tool_union_schema(tools: list[AnyTool], *, strict: bool) -> dict[str, Any]:
if not tools:
raise ValueError("No tools provided!")
tool_schemas = [
create_model( # type: ignore
tool.name,
__module__="fn",
__config__=ConfigDict(extra="forbid", populate_by_name=True, title=tool.name),
**{
"name": (Literal[tool.name], Field(description="Tool Name")),
"parameters": (tool.input_schema, Field(description="Tool Parameters")),
},
)
for tool in tools
]
if len(tool_schemas) == 1:
schema = tool_schemas[0]
else:
class AvailableTools(RootModel[Union[*tool_schemas]]): # type: ignore
pass
schema = AvailableTools
return {
"type": "json_schema",
"json_schema": {
"name": "ToolCall",
"schema": inline_schema_refs(to_strict_json_schema(schema) if strict else schema.model_json_schema()),
},
"strict": strict,
}
def filter_tools_by_tool_choice(tools: list[AnyTool], value: ChatModelToolChoice | None) -> list[AnyTool]:
if value == "none":
return []
if value == "required" or value == "auto" or value is None:
return tools
if isinstance(value, Tool):
tool = [tool for tool in tools if tool is value]
if not tool:
raise ValueError("Invalid tool choice provided! Tool was not found.")
return tool
raise RuntimeError(f"Unknown tool choice: {value}")