Spaces:
Sleeping
Sleeping
Ankush Rana
commited on
Commit
·
972a04b
1
Parent(s):
03a3aa7
fix error
Browse files
tools.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import random
|
| 2 |
from abc import ABC, abstractmethod
|
|
|
|
| 3 |
import os
|
| 4 |
# from langchain.tools import tool
|
| 5 |
import json
|
|
@@ -8,6 +9,7 @@ from openai import OpenAI
|
|
| 8 |
from typing import Dict, Optional, Union
|
| 9 |
import random
|
| 10 |
import copy
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
|
|
@@ -50,8 +52,21 @@ class ToolBase(BaseModel, ABC):
|
|
| 50 |
for field_name, field_info in cls.model_fields.items():
|
| 51 |
# Field properties
|
| 52 |
field_type = "string" # Default to string, will adjust if it's a different type
|
| 53 |
-
annotation = field_info.annotation.__args__[0] if getattr(field_info.annotation, "__origin__", None) is Union else field_info.annotation
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if annotation == int:
|
| 56 |
field_type = "integer"
|
| 57 |
elif annotation == bool:
|
|
@@ -66,10 +81,13 @@ class ToolBase(BaseModel, ABC):
|
|
| 66 |
# Determine if the field is required (not Optional or None)
|
| 67 |
if field_info.is_required():
|
| 68 |
function_metadata["function"]["parameters"]["required"].append(field_name)
|
|
|
|
| 69 |
|
| 70 |
# If there's an enum (like for `unit`), add it to the properties
|
| 71 |
if hasattr(field_info, 'default') and field_info.default is not None and isinstance(field_info.default, list):
|
| 72 |
function_metadata["function"]["parameters"]["properties"][field_name]["enum"] = field_info.default
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return function_metadata
|
| 75 |
|
|
@@ -309,7 +327,7 @@ class modify_reservation(ToolBase):
|
|
| 309 |
"""
|
| 310 |
|
| 311 |
|
| 312 |
-
new_room_type: str = Field(default=list(json_data["room_types"].keys())
|
| 313 |
new_check_in_date: str = Field(default=None, description="New check out date in format DD/MM/YYYY")
|
| 314 |
new_check_out_date: str = Field(default=None, description="New check out date in format DD/MM/YYYY")
|
| 315 |
guests: int = Field(default=None, description="New number of guests for the reservation.")
|
|
|
|
| 1 |
import random
|
| 2 |
from abc import ABC, abstractmethod
|
| 3 |
+
from typing import get_origin, get_args
|
| 4 |
import os
|
| 5 |
# from langchain.tools import tool
|
| 6 |
import json
|
|
|
|
| 9 |
from typing import Dict, Optional, Union
|
| 10 |
import random
|
| 11 |
import copy
|
| 12 |
+
from types import UnionType
|
| 13 |
|
| 14 |
|
| 15 |
|
|
|
|
| 52 |
for field_name, field_info in cls.model_fields.items():
|
| 53 |
# Field properties
|
| 54 |
field_type = "string" # Default to string, will adjust if it's a different type
|
| 55 |
+
annotation = field_info.annotation.__args__[0] if getattr(field_info.annotation, "__origin__", None) is Union else field_info.annotation
|
| 56 |
+
|
| 57 |
+
has_none = False
|
| 58 |
+
if get_origin(annotation) is UnionType: # Check if it's a Union type
|
| 59 |
+
args = get_args(annotation)
|
| 60 |
+
if type(None) in args:
|
| 61 |
+
has_none = True
|
| 62 |
+
args = [arg for arg in args if type(None) != arg]
|
| 63 |
+
if len(args) > 1:
|
| 64 |
+
raise TypeError("It can be union of only a valid type (str, int, bool, etc) and None")
|
| 65 |
+
elif len(args) == 0:
|
| 66 |
+
raise TypeError("There must be a valid type (str, int, bool, etc) not only None")
|
| 67 |
+
else:
|
| 68 |
+
annotation = args[0]
|
| 69 |
+
|
| 70 |
if annotation == int:
|
| 71 |
field_type = "integer"
|
| 72 |
elif annotation == bool:
|
|
|
|
| 81 |
# Determine if the field is required (not Optional or None)
|
| 82 |
if field_info.is_required():
|
| 83 |
function_metadata["function"]["parameters"]["required"].append(field_name)
|
| 84 |
+
has_none = True
|
| 85 |
|
| 86 |
# If there's an enum (like for `unit`), add it to the properties
|
| 87 |
if hasattr(field_info, 'default') and field_info.default is not None and isinstance(field_info.default, list):
|
| 88 |
function_metadata["function"]["parameters"]["properties"][field_name]["enum"] = field_info.default
|
| 89 |
+
if not has_none:
|
| 90 |
+
function_metadata["function"]["parameters"]["required"].append(field_name)
|
| 91 |
|
| 92 |
return function_metadata
|
| 93 |
|
|
|
|
| 327 |
"""
|
| 328 |
|
| 329 |
|
| 330 |
+
new_room_type: str | None = Field(default=list(json_data["room_types"].keys()), description=f"The type of new room to be modified, if {None} same room will be modified.")
|
| 331 |
new_check_in_date: str = Field(default=None, description="New check out date in format DD/MM/YYYY")
|
| 332 |
new_check_out_date: str = Field(default=None, description="New check out date in format DD/MM/YYYY")
|
| 333 |
guests: int = Field(default=None, description="New number of guests for the reservation.")
|