Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gkaretka committed Oct 24, 2024
1 parent 99b9842 commit df64fde
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ which would lead to this in your prompt:
- C
```

### Using structured output (function calling and structured output)

*Structured Outputs is a feature that ensures the model will always generate responses that adhere to your supplied JSON Schema, so you don't need to worry about the model omitting a required key, or hallucinating an invalid enum value.* - OpenAI website

#### How to get it to work

1. Check with the model developer/provider whether the model supports some kind of structured output.
2. Toggle structured output switch
3. Select one of the supported structured output methods (a model might support all of them but also none of them):
- `None` - no structured output is used (equals to toggle being in off state)
- `Function calling` - hacky way of implementing structured outputs before `Response format` was implemented into API
- `Response format` - new way of implementing structured outputs
4. Provide JSON schema in `json schema` text input (can be generated from `pydantic` model or `zod` if you use `nodejs`):
```json
{
"title": "get_delivery_date",
"description": "Get the delivery date for a customer's order. Call this whenever you need to know the delivery date, for example when a customer asks 'Where is my package'",
"type": "object",
"properties": {
"order_id": {
"type": "string"
}
},
"required": ["order_id"],
"additionalProperties": false
}
```
example from OpenAI website (slightly modified). For more examples see [https://json-schema.org/learn/miscellaneous-examples](https://json-schema.org/learn/miscellaneous-examples).

### Postprocessing the model outputs

When working with LLMs, you would often postprocess the raw generated text. Prompterator
Expand Down
8 changes: 5 additions & 3 deletions prompterator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,11 @@ def set_up_ui_generation():
)
selected_model: c.ModelProperties = st.session_state.model
available_structured_output_settings = selected_model.supports_structured_output
structured_output_available = False
if len(available_structured_output_settings) > 1:
structured_output_available = True

structured_output_available = (
len(set(available_structured_output_settings) - {c.StructuredOutputImplementation.NONE})
> 0
)

structured_output_enabled = col3.toggle(
label="Structured output",
Expand Down
16 changes: 13 additions & 3 deletions prompterator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,16 +382,26 @@ def format_traceback_for_markdown(text):
return re.sub(r"\n", "\n\n", text)


def normalize_name(name: str):
return re.sub("[^a-zA-Z0-9_]", "_", name).lower()


def build_function_calling_tooling(json_schema):
schema = json.loads(json_schema)
function = schema.copy()
function_name = function.pop("title")
function_name = normalize_name(function.pop("title"))
print(function_name)
description = (
function.pop("description")
if function.get("description", None) is not None
else function_name
)
tools = [
{
"type": "function",
"function": {
"name": function_name,
"description": function.pop("description"),
"description": description,
"parameters": function,
},
}
Expand All @@ -402,7 +412,7 @@ def build_function_calling_tooling(json_schema):

def build_response_format(json_schema):
json_schema = json.loads(json_schema)
schema = {"name": json_schema.pop("title"), "schema": json_schema, "strict": True}
schema = {"name": normalize_name(json_schema.pop("title")), "schema": json_schema, "strict": True}
response_format = {"type": "json_schema", "json_schema": schema}

return response_format
Expand Down

0 comments on commit df64fde

Please sign in to comment.