forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
from typing import Any, Dict
|
|
|
|
|
|
def _resolve_schema_references(schema: Any, definitions: Dict[str, Any]) -> Any:
|
|
"""
|
|
Resolves the $ref keys in a JSON schema object using the provided definitions.
|
|
"""
|
|
if isinstance(schema, list):
|
|
for i, item in enumerate(schema):
|
|
schema[i] = _resolve_schema_references(item, definitions)
|
|
elif isinstance(schema, dict):
|
|
if "$ref" in schema:
|
|
ref_key = schema.pop("$ref").split("/")[-1]
|
|
ref = definitions.get(ref_key, {})
|
|
schema.update(ref)
|
|
else:
|
|
for key, value in schema.items():
|
|
schema[key] = _resolve_schema_references(value, definitions)
|
|
return schema
|
|
|
|
|
|
def _convert_schema(schema: dict) -> dict:
|
|
props = {k: {"title": k, **v} for k, v in schema["properties"].items()}
|
|
return {
|
|
"type": "object",
|
|
"properties": props,
|
|
"required": schema.get("required", []),
|
|
}
|
|
|
|
|
|
def get_llm_kwargs(function: dict) -> dict:
|
|
return {"functions": [function], "function_call": {"name": function["name"]}}
|