-
Notifications
You must be signed in to change notification settings - Fork 10
Make models serializable #174
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -49,25 +49,13 @@ | |
| MODEL_ID = "gateway:anthropic/claude-sonnet-4.6" | ||
|
|
||
|
|
||
| # ── Workflow-safe model placeholder ────────────────────────────── | ||
| # | ||
| # ``agent.run`` requires a ``Model``, but a real one can't be built | ||
| # inside the workflow: ``ai.get_model("gateway:...")`` constructs an | ||
| # ``httpx.AsyncClient`` at provider-init time, which imports | ||
| # httpcore/anyio and trips the Temporal sandbox (``threading.local`` | ||
| # at module load). Our loop never calls the model directly anyway -- | ||
| # every LLM call is delegated to ``llm_call_activity``, which runs | ||
| # outside the sandbox and resolves the real model by id there. | ||
| # | ||
| # So hand the workflow a placeholder ``Model`` whose provider builds | ||
| # no client. It carries the real model id (so the activity can | ||
| # resolve it) but is safe to construct inside the sandbox. | ||
| class WorkflowModelProvider(ai.Provider[Any]): | ||
| """A clientless provider, safe to construct in a workflow sandbox.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| super().__init__(name="workflow-placeholder", base_url="") | ||
|
|
||
| # ``ai.Model`` is fully serializable: it stores a provider *recipe* | ||
| # (factory reference + JSON args) and only builds the provider — and | ||
| # its ``httpx.AsyncClient`` — on first use. The workflow can therefore | ||
| # construct the model with ``ai.get_model`` inside the Temporal sandbox | ||
| # (nothing network-shaped is created there) and ship it to | ||
| # ``llm_call_activity`` as plain JSON, where the provider gets built | ||
| # for real. | ||
|
|
||
| # ── Tool definitions ───────────────────────────────────────────── | ||
| # | ||
|
|
@@ -116,7 +104,7 @@ async def get_population_activity(city: str) -> int: | |
|
|
||
| @dataclasses.dataclass | ||
| class LLMParams: | ||
| model_id: str | ||
| model: dict[str, Any] | ||
| messages: list[dict[str, Any]] | ||
| tool_schemas: list[dict[str, Any]] | ||
|
|
||
|
|
@@ -129,7 +117,7 @@ class LLMResult: | |
| @temporalio.activity.defn | ||
| async def llm_call_activity(params: LLMParams) -> LLMResult: | ||
| """Call the LLM, drain the stream, return the final message.""" | ||
| model = ai.get_model(params.model_id) | ||
| model = ai.Model.model_validate(params.model) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing to do about it, but the overloading of model is unfortunate |
||
| messages = [ai.messages.Message.model_validate(m) for m in params.messages] | ||
| tools = [ | ||
| ai.Tool( | ||
|
|
@@ -172,7 +160,7 @@ async def loop( | |
| result = await temporalio.workflow.execute_activity( | ||
| llm_call_activity, | ||
| LLMParams( | ||
| model_id=context.model.id, | ||
| model=context.model.model_dump(mode="json"), | ||
| messages=[m.model_dump() for m in context.messages], | ||
| tool_schemas=tool_schemas, | ||
| ), | ||
|
|
@@ -238,7 +226,9 @@ async def _call() -> ai.events.ToolCallResult: | |
| class WeatherWorkflow: | ||
| @temporalio.workflow.run | ||
| async def run(self, user_query: str) -> str: | ||
| model = ai.Model(MODEL_ID, provider=WorkflowModelProvider()) | ||
| # Safe in the sandbox: the provider (and its HTTP client) is | ||
| # only built lazily, inside the LLM activity. | ||
| model = ai.get_model(MODEL_ID) | ||
| messages: list[ai.messages.Message] = [ | ||
| ai.system_message( | ||
| "Answer questions using the weather and population tools." | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classic backward-looking AI comment. Probably delete?