Skip to content

Instantly share code, notes, and snippets.

@Donavan
Created October 22, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Donavan/9be122ff9d471c07da7bc74bab1d49ee to your computer and use it in GitHub Desktop.
Save Donavan/9be122ff9d471c07da7bc74bab1d49ee to your computer and use it in GitHub Desktop.
JSON Schemas for Open AI.
@json_schema('Query a vector store to find relevant documents.',
{
'query': {'type': 'string', 'description': 'The text you want to find relevant documents for', 'required': True},
'max_docs': {'type': 'integer', 'description': 'How many relevant documents to return. Defaults to 10'},
'min_relevance': {'type': 'number', 'description': 'Only return docs that are relevant by this percentage from 0.0 to 1.9. Defaults to 0.92'},
})
async def query_vector_store(self, **kwargs: Union[str, int, float]) -> str:
"""
Queries the vector store to find relevant documents.
:param kwargs: Contains query, max_docs, and min_relevance parameters.
:return: A JSON string containing relevant documents.
"""
query: str = kwargs.get("query")
def __functions(self, obj) -> List[Dict[str, Any]]:
"""
Extracts JSON schemas from the methods in the class.
:return: A list of JSON schemas.
"""
schemas = []
for name, method in inspect.getmembers(obj, predicate=inspect.ismethod):
if hasattr(method, 'schema'):
schemas.append(method.schema)
return schemas
def json_schema(description, params):
def decorator(func):
parameters = {
'type': 'object',
'properties': {}
}
required = []
for param, info in params.items():
if info.get('required', False):
required.append(param)
parameters['properties'][param] = {
'type': info.get('type', 'string'),
'description': info.get('description', '')
}
schema = {
'name': func.__name__,
'description': description,
'parameters': parameters
}
if len(required) > 0:
schema['required'] = required
func.schema = schema
# Return the original function
return func
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment