Skip to content

Commit

Permalink
Add JSON Mode option to OpenAIModelComponent (#2386)
Browse files Browse the repository at this point in the history
* feat: Add JSON Mode option to OpenAIModelComponent

* ♻️ (OpenAIModel.py): add type ignore comment

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ogabrielluiz and github-actions[bot] authored Jun 26, 2024
1 parent 2094b6b commit a81f686
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/backend/base/langflow/components/models/OpenAIModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class OpenAIModelComponent(LCModelComponent):
info="The maximum number of tokens to generate. Set to 0 for unlimited tokens.",
),
DictInput(name="model_kwargs", display_name="Model Kwargs", advanced=True),
BoolInput(
name="json_mode",
display_name="JSON Mode",
advanced=True,
info="If True, it will output JSON regardless of passing a schema.",
),
DictInput(
name="output_schema",
is_list=True,
Expand Down Expand Up @@ -84,7 +90,7 @@ def build_model(self) -> LanguageModel:
max_tokens = self.max_tokens
model_kwargs = self.model_kwargs or {}
openai_api_base = self.openai_api_base or "https://api.openai.com/v1"
json_mode = bool(output_schema_dict)
json_mode = bool(output_schema_dict) or self.json_mode
seed = self.seed
model_kwargs["seed"] = seed

Expand All @@ -101,7 +107,10 @@ def build_model(self) -> LanguageModel:
temperature=temperature or 0.1,
)
if json_mode:
output = output.with_structured_output(schema=output_schema_dict, method="json_mode") # type: ignore
if output_schema_dict:
output = output.with_structured_output(schema=output_schema_dict, method="json_mode") # type: ignore
else:
output = output.bind(response_format={"type": "json_object"}) # type: ignore

return output

Expand Down

0 comments on commit a81f686

Please sign in to comment.