Skip to content

Commit

Permalink
Add support for OpenAI style function call support
Browse files Browse the repository at this point in the history
User should pass OpenAI style dictionary of function and
it should be internally converted to same json schema as
callable function (#1376)
  • Loading branch information
punitvara committed Jan 18, 2025
1 parent 063291d commit 900db31
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions outlines/generate/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def json(
regex_str = build_regex_from_schema(schema, whitespace_pattern)
generator = regex(model, regex_str, sampler)
generator.format_sequence = lambda x: pyjson.loads(x)
elif isinstance(schema_object, dict) and schema_object.get("type") == "function":
# Handle OpenAI function call format
schema = pyjson.dumps(schema_object["function"]["parameters"])
regex_str = build_regex_from_schema(schema, whitespace_pattern)
generator = regex(model, regex_str, sampler)
generator.format_sequence = lambda x: pyjson.loads(x)
else:
raise ValueError(
f"Cannot parse schema {schema_object}. The schema must be either "
Expand Down

0 comments on commit 900db31

Please sign in to comment.