From 1f5c579ef4de385c14306f8b0ec539e39a36e432 Mon Sep 17 00:00:00 2001 From: Bagatur Date: Wed, 30 Aug 2023 13:37:50 -0700 Subject: [PATCH] add --- .../langchain/utils/openai_functions.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 libs/langchain/langchain/utils/openai_functions.py diff --git a/libs/langchain/langchain/utils/openai_functions.py b/libs/langchain/langchain/utils/openai_functions.py new file mode 100644 index 00000000000..48c49541dcf --- /dev/null +++ b/libs/langchain/langchain/utils/openai_functions.py @@ -0,0 +1,29 @@ +from typing import Dict, Optional, Type, TypedDict, cast + +from langchain.pydantic_v1 import BaseModel +from langchain.utils.json_schema import dereference_refs + + +class FunctionDescription(TypedDict): + """Representation of a callable function to the OpenAI API.""" + + name: str + """The name of the function.""" + description: str + """A description of the function.""" + parameters: dict + """The parameters of the function.""" + + +def convert_pydantic_to_openai_function( + model: Type[BaseModel], + *, + name: Optional[str] = None, + description: Optional[str] = None +) -> FunctionDescription: + schema = cast(Dict, dereference_refs(model.schema())) + return { + "name": name or schema["title"], + "description": description or schema["description"], + "parameters": schema, + }