Skip to content

Commit

Permalink
Merge branch 'main' into revise_few_shot_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shreya-51 authored Sep 15, 2024
2 parents 3f06ebf + 352b456 commit f4bc933
Show file tree
Hide file tree
Showing 10 changed files with 1,046 additions and 662 deletions.
327 changes: 327 additions & 0 deletions docs/blog/posts/anthropic-prompt-caching.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/blog/posts/introducing-structured-outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ print(resp)
#> name='JASON' age=25
```

This built-in retry logic allows for targetted correction to the generated response, ensuring that outputs are not only consistent with your schema but also corect for your use-case. This is invaluable in building reliable LLM systems.
This built-in retry logic allows for targetted correction to the generated response, ensuring that outputs are not only consistent with your schema but also correct for your use-case. This is invaluable in building reliable LLM systems.

### Real-time Streaming Validation

Expand Down Expand Up @@ -240,7 +240,7 @@ During the meeting, we agreed on several key points. The conference will be held
The budget for the event is set at $50,000, covering venue costs, speaker fees, and promotional activities. Each participant is expected to contribute an article to the conference blog by February 20th.
A follow-up meetingis scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
A follow-up meeting is scheduled for January 25th at 3 PM GMT to finalize the agenda and confirm the list of speakers.
"""


Expand Down
1 change: 1 addition & 0 deletions instructor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def from_openai(
instructor.Mode.PARALLEL_TOOLS,
instructor.Mode.MD_JSON,
instructor.Mode.TOOLS_STRICT,
instructor.Mode.JSON_O1,
}

if isinstance(client, openai.OpenAI):
Expand Down
4 changes: 2 additions & 2 deletions instructor/dsl/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def model_from_chunks(
partial_model = cls.get_partial_model()
for chunk in json_chunks:
potential_object += chunk
obj = from_json((potential_object or "{}").encode(), partial_mode="on")
obj = from_json((potential_object.strip() or "{}").encode(), partial_mode="on")
obj = partial_model.model_validate(obj, strict=None, **kwargs)
yield obj

Expand All @@ -141,7 +141,7 @@ async def model_from_chunks_async(
partial_model = cls.get_partial_model()
async for chunk in json_chunks:
potential_object += chunk
obj = from_json((potential_object or "{}").encode(), partial_mode="on")
obj = from_json((potential_object.strip() or "{}").encode(), partial_mode="on")
obj = partial_model.model_validate(obj, strict=None, **kwargs)
yield obj

Expand Down
2 changes: 1 addition & 1 deletion instructor/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def from_response(
if mode in {Mode.TOOLS, Mode.MISTRAL_TOOLS, Mode.TOOLS_STRICT}:
return cls.parse_tools(completion, validation_context, strict)

if mode in {Mode.JSON, Mode.JSON_SCHEMA, Mode.MD_JSON}:
if mode in {Mode.JSON, Mode.JSON_SCHEMA, Mode.MD_JSON, Mode.JSON_O1}:
return cls.parse_json(completion, validation_context, strict)

raise ValueError(f"Invalid patch mode: {mode}")
Expand Down
1 change: 1 addition & 0 deletions instructor/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Mode(enum.Enum):
TOOLS = "tool_call"
MISTRAL_TOOLS = "mistral_tools"
JSON = "json_mode"
JSON_O1 = "json_o1"
MD_JSON = "markdown_json_mode"
JSON_SCHEMA = "json_schema_mode"
ANTHROPIC_TOOLS = "anthropic_tools"
Expand Down
23 changes: 23 additions & 0 deletions instructor/process_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,29 @@ def handle_response_model(
"type": "function",
"function": {"name": response_model.openai_schema["name"]},
}
elif mode in {Mode.JSON_O1}:
# O1 doesn't accept system messages so you'll need to assert that
roles = [message["role"] for message in new_kwargs.get("messages", [])]
if "system" in roles:
raise ValueError("System messages are not supported For the O1 models")

message = dedent(
f"""
Understand the content and provide
the parsed objects in json that match the following json_schema:\n
{json.dumps(response_model.model_json_schema(), indent=2, ensure_ascii=False)}
Make sure to return an instance of the JSON, not the schema itself
"""
)

new_kwargs["messages"].append(
{
"role": "user",
"content": message,
},
)

elif mode in {Mode.JSON, Mode.MD_JSON, Mode.JSON_SCHEMA}:
# If its a JSON Mode we need to massage the prompt a bit
Expand Down
1,339 changes: 685 additions & 654 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "instructor"
version = "1.4.1"
version = "1.4.2"
description = "structured outputs for llm"
authors = ["Jason Liu <[email protected]>"]
license = "MIT"
Expand All @@ -10,7 +10,7 @@ repository = "https://github.com/jxnl/instructor"

[tool.poetry.dependencies]
python = "^3.9"
openai = "^1.40.0"
openai = "^1.45.0"
pydantic = "^2.8.0"
docstring-parser = "^0.16"
typer = ">=0.9.0,<1.0.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/dsl/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def test_partial():
"type": "object",
}, "Partial model JSON schema has changed"

for model in partial.model_from_chunks(['{"b": {"b": 1}}']):
# Handle any leading whitespace from the model
for model in partial.model_from_chunks(['\n', '\t', ' ', '{"b": {"b": 1}}']):
assert model.model_dump() == {"a": None, "b": {"b": 1}}


Expand Down

0 comments on commit f4bc933

Please sign in to comment.