-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
68 lines (54 loc) · 2.15 KB
/
models.py
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
from typing import List, Optional
import json
from pydantic import BaseModel, Field, root_validator
with open("reference.json") as f:
FUNCTIONS = json.load(f)
class Parameter(BaseModel):
name: str
type_: str
value: str
class Function(BaseModel):
name: str = Field(description="Name of the function.")
parameters: List[Parameter] = Field(description="Parameters of the function.")
# pylint: disable=no-self-argument
@root_validator(pre=True)
def validate_function(cls, values):
"""Validate the function."""
values["name"] = values["name"].replace("OBB.", "")
func = cls.to_endpoint(values["name"])
if func not in FUNCTIONS:
raise ValueError(f"Function '{func}' not found in reference.")
return values
@staticmethod
def to_endpoint(name: str) -> str:
"""Check if the function is valid."""
return "/" + "/".join(name.lower().split("."))
class FunctionMessage(BaseModel):
reasoning: str = Field(description="Short reasoning, keep it to 1 sentence.")
function: Optional[Function] = Field(description="Function to be executed.")
@staticmethod
def _get_schema(name: str, field: str) -> dict:
"""Get the schema of the function."""
return FUNCTIONS[Function.to_endpoint(name)][field]
def to_xl(self) -> str:
"""Translate the function to Excel."""
snippet = ""
if self.function:
incoming = {p.name: p.value for p in self.function.parameters}
reference = self._get_schema(self.function.name, "parameters")
args = ""
for p_name, p_schema in reference.items():
if p_name in incoming:
if p_schema["type"] == "Text":
args += f'"{incoming[p_name]}",'
else:
args += f"{incoming[p_name]},"
else:
args += ","
args = args.strip(",")
snippet += "```excel\n"
snippet += f"=OBB.{self.function.name}({args})\n"
snippet += "```\n"
t = self.reasoning + "\n\n"
t += snippet
return t