-
Notifications
You must be signed in to change notification settings - Fork 0
feat(llm): structured output (proposal 0016) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
861af60
chore: bump spec to v0.15.0; add jsonschema; skip deferred fixtures
chris-colinsky e9298af
feat(llm): add StructuredOutputInvalid error category
chris-colinsky 069cfc8
feat(llm): add Response.parsed field
chris-colinsky 5a889c7
feat(llm): validate_response_schema + strict_mode_supported helpers
chris-colinsky 8203605
feat(llm): Provider Protocol gains response_schema parameter
chris-colinsky 467f74b
feat(llm/openai): native response_format wire path + Pydantic overload
chris-colinsky 16fb3c4
feat(llm/openai): prompt-augmentation fallback + inspect property
chris-colinsky 66aa908
test(conformance): capability-agnostic harness helpers for wire + car…
chris-colinsky adf617c
test: drive 0016 fixtures 021-028 + add structured-output unit tests
chris-colinsky 1d1e2df
docs: changelog entry for proposal 0016 under [Unreleased]
chris-colinsky 03bcf23
fix(llm): address CoPilot review on PR #42
chris-colinsky 2d39a56
docs: upgrade hello-world to demo structured output
chris-colinsky e7cb0de
fix(examples): hello-world base_url default and observer trace
chris-colinsky c9326e8
docs(examples): wire research + summarize nodes to real LLM calls
chris-colinsky 1ae405b
docs: structured output concepts page + model-providers updates
chris-colinsky 58f6c2f
docs: fix Provider.complete docstring Returns rendering
chris-colinsky 8ed334c
fix(llm): second CoPilot review pass on PR #42
chris-colinsky 1b5fbb0
fix: third CoPilot review pass on PR #42
chris-colinsky cddb2b1
fix: fourth CoPilot review pass on PR #42
chris-colinsky b283f97
docs: cumulative strict-mode constraint list + spec-version-drift test
chris-colinsky 4c12add
fix: fifth CoPilot review pass on PR #42
chris-colinsky b8ffc43
fix(test): replace git-describe submodule check with CHANGELOG parse
chris-colinsky 578322d
fix(test): satisfy CodeQL on the changelog-parsing test
chris-colinsky e5f1426
fix: seventh CoPilot review pass on PR #42
chris-colinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| """Hello-world demo: a 3-node graph that classifies a query with an LLM | ||
| (via structured output) and routes to one of two follow-up nodes. | ||
|
|
||
| **Demonstrates:** | ||
|
|
||
| - Typed ``State`` with three reducer policies (``last_write_wins``, | ||
| ``append``, ``merge``). | ||
| - ``OpenAIProvider`` from ``openarmature.llm`` against any | ||
| OpenAI-compatible endpoint. | ||
| - Structured output via a Pydantic class — the model's response comes | ||
| back as a validated ``Classification`` instance, not a string. | ||
| - Conditional routing as a pure function of state (``route``). | ||
| - ``attach_observer`` for boundary visibility. | ||
|
|
||
| **Configuration** (env vars; OpenAI defaults shown): | ||
|
|
||
| - ``LLM_BASE_URL`` — defaults to ``https://api.openai.com/v1``. | ||
| - ``LLM_MODEL`` — defaults to ``gpt-4o-mini``. | ||
| - ``LLM_API_KEY`` — required (your OpenAI API key, or empty for | ||
| local servers that don't authenticate). | ||
|
|
||
| Run with: | ||
|
|
||
| uv sync --group examples | ||
| LLM_API_KEY=sk-... uv run python examples/00-hello-world/main.py | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import os | ||
| from collections.abc import Mapping | ||
| from typing import Annotated, Any, Literal | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| from openarmature.graph import ( | ||
| END, | ||
| CompiledGraph, | ||
| GraphBuilder, | ||
| NodeEvent, | ||
| State, | ||
| append, | ||
| merge, | ||
| ) | ||
| from openarmature.llm import OpenAIProvider, UserMessage | ||
|
|
||
|
|
||
| class Classification(BaseModel): | ||
| """The Pydantic schema the model is constrained to produce. | ||
|
|
||
| Passed as ``response_schema`` to ``provider.complete()``; the | ||
| framework converts to JSON Schema, instructs the provider to | ||
| return matching content, validates the response, and yields a | ||
| ``Classification`` instance via ``Response.parsed``. | ||
| """ | ||
|
|
||
| intent: Literal["research", "summarize"] | ||
| rationale: str | ||
|
|
||
|
|
||
| class PipelineState(State): | ||
| query: str | ||
| classification: Classification | None = None | ||
| sources: Annotated[list[str], append] = Field(default_factory=list) | ||
| metadata: Annotated[dict[str, str], merge] = Field(default_factory=dict) | ||
|
|
||
|
|
||
| _provider = OpenAIProvider( | ||
| base_url=os.environ.get("LLM_BASE_URL", "https://api.openai.com/v1"), | ||
| model=os.environ.get("LLM_MODEL", "gpt-4o-mini"), | ||
| api_key=os.environ.get("LLM_API_KEY"), | ||
|
chris-colinsky marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
|
|
||
| async def classify(state: PipelineState) -> Mapping[str, Any]: | ||
| response = await _provider.complete( | ||
| [ | ||
| UserMessage( | ||
| content=( | ||
| f"Route this query to either 'research' (look something up) or " | ||
| f"'summarize' (condense known material): {state.query!r}" | ||
| ) | ||
| ) | ||
| ], | ||
| response_schema=Classification, | ||
| ) | ||
| return {"classification": response.parsed, "metadata": {"classified_by": "llm"}} | ||
|
|
||
|
|
||
| async def research(state: PipelineState) -> Mapping[str, Any]: | ||
| return {"sources": ["wikipedia", "arxiv"], "metadata": {"tool": "search"}} | ||
|
|
||
|
|
||
| async def summarize(state: PipelineState) -> Mapping[str, Any]: | ||
| return {"sources": ["cache"], "metadata": {"tool": "summarizer"}} | ||
|
|
||
|
|
||
| def route(state: PipelineState) -> str: | ||
| if state.classification is None: | ||
| raise RuntimeError("classify did not populate state.classification") | ||
| return state.classification.intent | ||
|
|
||
|
|
||
| async def trace(event: NodeEvent) -> None: | ||
| if event.phase == "completed" and event.error is None: | ||
| print(f"{event.node_name}: sources={event.post_state.sources}") | ||
|
|
||
|
|
||
| def build_graph() -> CompiledGraph[PipelineState]: | ||
| return ( | ||
| GraphBuilder(PipelineState) | ||
| .add_node("classify", classify) | ||
| .add_node("research", research) | ||
| .add_node("summarize", summarize) | ||
| .add_conditional_edge("classify", route) | ||
| .add_edge("research", END) | ||
| .add_edge("summarize", END) | ||
| .set_entry("classify") | ||
| .compile() | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| graph = build_graph() | ||
| graph.attach_observer(trace) | ||
| try: | ||
| final = await graph.invoke(PipelineState(query="what is RAG?")) | ||
| print(f"\nclassification: {final.classification}") | ||
| print(f"sources: {final.sources}") | ||
| print(f"metadata: {final.metadata}") | ||
| finally: | ||
| await graph.drain() | ||
|
chris-colinsky marked this conversation as resolved.
chris-colinsky marked this conversation as resolved.
chris-colinsky marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule openarmature-spec
updated
111 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.