Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add more types to the type_map from python -> json schema #2095

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions letta/functions/schema_generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import inspect
from typing import Any, Dict, Optional, Type, Union, get_args, get_origin
from typing import Any, Dict, List, Optional, Type, Union, get_args, get_origin

from docstring_parser import parse
from pydantic import BaseModel
Expand Down Expand Up @@ -38,15 +38,29 @@ def type_to_json_schema_type(py_type):

# Mapping of Python types to JSON schema types
type_map = {
# Basic types
int: "integer",
str: "string",
bool: "boolean",
float: "number",
list[str]: "array",
# Add more mappings as needed
# Collections
List[str]: "array",
List[int]: "array",
list: "array",
tuple: "array",
set: "array",
# Dictionaries
dict: "object",
Dict[str, Any]: "object",
# Special types
None: "null",
type(None): "null",
# Optional types
# Optional[str]: "string", # NOTE: caught above ^
Union[str, None]: "string",
}
if py_type not in type_map:
raise ValueError(f"Python type {py_type} has no corresponding JSON schema type")
raise ValueError(f"Python type {py_type} has no corresponding JSON schema type - full map: {type_map}")

return type_map.get(py_type, "string") # Default to "string" if type not in map

Expand Down
2 changes: 1 addition & 1 deletion letta/local_llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def num_tokens_from_functions(functions: List[dict], model: str = "gpt-4"):
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
print("Warning: model not found. Using cl100k_base encoding.")
warnings.warn("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")

num_tokens = 0
Expand Down
Loading