Merge branch 'master' into autogpt/agent-protocol

pull/5612/head
Reinier van der Leer 2023-10-07 19:11:27 -07:00
commit 2b8d91fb1b
No known key found for this signature in database
GPG Key ID: CDC1180FDAE06193
2 changed files with 15 additions and 7 deletions

View File

@ -33,7 +33,7 @@ class PromptScratchpad(BaseModel):
self,
name: str,
description: str,
params: dict[str, str],
params: dict[str, str | dict],
function: Callable,
) -> None:
"""
@ -50,15 +50,20 @@ class PromptScratchpad(BaseModel):
function (callable, optional): A callable function to be called when
the command is executed. Defaults to None.
"""
for p, t in params.items():
for p, s in params.items():
invalid = False
if t not in JSONSchema.Type._value2member_map_:
if type(s) == str and s not in JSONSchema.Type._value2member_map_:
invalid = True
logger.warning(
f"Cannot add command '{name}':"
f" parameter '{p}' has invalid type '{t}'."
f" parameter '{p}' has invalid type '{s}'."
f" Valid types are: {JSONSchema.Type._value2member_map_.keys()}"
)
elif isinstance(s, dict):
try:
JSONSchema.from_dict(s)
except KeyError:
invalid = True
if invalid:
return
@ -66,9 +71,10 @@ class PromptScratchpad(BaseModel):
name=name,
description=description,
parameters={
# TODO: require plugins to specify parameters as a JSON schema
name: JSONSchema(type=JSONSchema.Type._value2member_map_[type])
for name, type in params.items()
name: JSONSchema(type=JSONSchema.Type._value2member_map_[spec])
if type(spec) == str
else JSONSchema.from_dict(spec)
for name, spec in params.items()
},
method=function,
)

View File

@ -148,6 +148,8 @@ class JSONSchema(BaseModel):
if not self.properties:
return "Record<string, any>"
return self.to_typescript_object_interface()
elif self.enum:
return " | ".join(repr(v) for v in self.enum)
else:
raise NotImplementedError(
f"JSONSchema.typescript_type does not support Type.{self.type.name} yet"