文件历史

135 次代码提交

作者 SHA1 备注 提交日期
Simon Willison aae4eedf16 OpenAI: reconstruct conversation input history via prev.prompt.messages
Iterate prev_response.prompt.messages (the computed Message list) to
rebuild prior turn inputs, and funnel each through
_append_message_from_message. Output side still uses the flat text /
tool_calls accumulators (text_or_raise, tool_calls_or_raise) to avoid
calling _build_parts on historical responses whose StreamEvent shape
might have used the same part_index for mixed content types.
2026-04-12 21:20:57 -07:00
Simon Willison a6a25b26e5 Make Prompt.messages authoritative, OpenAI adapter reads only messages
prompt.messages is now a computed property that synthesizes Messages
from legacy inputs (system=, parts=, prompt=, attachments=,
tool_results=) when messages= was not explicitly passed. Explicit
messages= passes through verbatim.

OpenAI build_messages() for the current prompt now has a single code
path that iterates prompt.messages — the old if/elif over _parts vs
legacy fields is gone. Conversation history reconstruction still uses
legacy fields (will flip in a later commit).
2026-04-12 21:13:42 -07:00
Simon Willison d833616276 Add messages=[...] parameter to model.prompt()
Accept messages= alongside the existing parts= parameter.
Conversation/AsyncConversation/Model/AsyncModel prompt() forward it to
Prompt, which stores it as prompt.messages.

OpenAI adapter gains _append_message_from_message which translates one
llm.Message into the correct OpenAI message dict(s), including the
parallel-tool-calls case (one assistant message with multiple
ToolCallParts becomes one OpenAI message with a tool_calls array).

Legacy paths untouched: parts=, prompt=, system=, attachments=, and
tool_results= still work when messages= is not set.
2026-04-12 21:10:28 -07:00
Simon Willison 5217a0bace Parts: serialization round-trip, stream safety, provider_metadata
Round out the parts API so transcripts survive serialization and so
providers can stash opaque multi-turn state on parts and stream events.

Serialization:
- AttachmentPart.from_dict now supported; inline content bytes round-trip
  as base64.
- ToolResultPart.attachments round-trip through to_dict/from_dict.

Stream assembler:
- _build_parts raises ValueError when an incompatible StreamEvent type
  appears at the same part_index, instead of silently overwriting the
  earlier part. tool_call_name and tool_call_args stay compatible.

OpenAI parts=[] support:
- build_messages emits assistant tool_calls and role:"tool" messages for
  ToolCallPart and ToolResultPart passed via parts=.

provider_metadata:
- New optional dict on TextPart, ReasoningPart, ToolCallPart,
  ToolResultPart, and StreamEvent for opaque provider data that must be
  echoed back on the next request (Anthropic signature/encrypted_content,
  Gemini thoughtSignature, OpenAI Responses encrypted_content).
- StreamEvent values merge onto the finalized Part per top-level namespace
  key, last non-None wins.
- Persisted via existing content_json column and reloaded by
  _load_parts_from_db; no schema change.
- Plugin author guide in docs/plugins/advanced-model-plugins.md.

Types:
- Widen execute() return types to Iterator[str | StreamEvent] /
  AsyncGenerator[str | StreamEvent, None] on abstract Model/AsyncModel
  bases and OpenAI Chat implementations.
- Initialize _reasoning_token_count on _BaseResponse so mypy stops
  flagging the OpenAI plugin.
2026-04-12 20:59:21 -07:00
Simon Willison f773ab2fa4 Formatting and type annotation fixes 2026-04-12 17:31:21 -07:00
Simon Willison 7c0a192341 Reformatted with Black
Test / test (macos-latest, 3.10) (push) Has been cancelled
Test / test (macos-latest, 3.11) (push) Has been cancelled
Test / test (macos-latest, 3.12) (push) Has been cancelled
Test / test (macos-latest, 3.13) (push) Has been cancelled
Test / test (macos-latest, 3.14) (push) Has been cancelled
Test / test (ubuntu-latest, 3.10) (push) Has been cancelled
Test / test (ubuntu-latest, 3.11) (push) Has been cancelled
Test / test (ubuntu-latest, 3.12) (push) Has been cancelled
Test / test (ubuntu-latest, 3.13) (push) Has been cancelled
Test / test (ubuntu-latest, 3.14) (push) Has been cancelled
Test / test (windows-latest, 3.10) (push) Has been cancelled
Test / test (windows-latest, 3.11) (push) Has been cancelled
Test / test (windows-latest, 3.12) (push) Has been cancelled
Test / test (windows-latest, 3.13) (push) Has been cancelled
Test / test (windows-latest, 3.14) (push) Has been cancelled
2026-04-12 17:18:37 -07:00
Simon Willison 1280082b6d Rename prompt.input_parts to prompt.parts 2026-04-12 17:17:42 -07:00
Simon Willison 1610030020 Default OpenAI plugin now handles .prompt(parts=[...]) 2026-04-07 12:18:51 -07:00
Simon Willison 0367001d18 OpenAI plugin emits StreamEvent objects (Phase 3)
Chat.execute() and AsyncChat.execute() now yield StreamEvent instead
of bare strings. Text chunks, tool call names/args are all emitted
as typed events. Reasoning token counts from usage data are stored
on the response and _build_parts() prepends a redacted ReasoningPart.

StreamEvent gains server_executed and tool_name fields for use by
plugins with server-side tool execution.

Tested live against gpt-5.4-mini: text streaming, tool calls, and
reasoning tokens (with reasoning_effort='high') all work correctly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-05 07:24:40 -07:00
Simon Willison cad03fb4f4 Register async models for extra-openai-models.yaml, closes #1395
Note that Completion models do not have an async class so will not be registered as async.
2026-04-04 07:07:14 -07:00
Simon Willison 07dccc00ef GPT-5.4, 5.4-mini, 5.4-nano
Closes #1376
2026-03-17 11:24:52 -07:00
Claude 947feaa0c9 Add gpt-5.4 and gpt-5.4-2026-03-05 model support
https://claude.ai/code/session_01HwqZ4WeDCrspfF8E7STiPA
2026-03-06 00:42:46 +00:00
Arjan Mossel 73548c479a Add type annotations for OpenAI Chat/AsyncChat/Completion execute methods (#1315)
* Add type annotations for OpenAI Chat/AsyncChat/Completion execute methods
* Add type hint for OpenAI _Shared class
* cast(Response) to make mypy happy

Co-authored-by: Simon Willison <swillison@gmail.com>
2025-12-11 14:17:17 -08:00
Simon Willison 1753eb74ee gpt-5.2, gpt-5.2-chat-latest - refs #1317 2025-12-11 11:50:08 -08:00
Simon Willison 0526abeeea gpt-5.1 and gpt-5.1-chat-latest, refs #1300 2025-11-13 11:35:14 -08:00
James Sanford 2a54939951 Fix streaming tool calls with tests for many variants. (#1218)
* Recorded instance of streaming tool response variant "a".

This is the typical response, where "arguments":"" arrives
first in the stream, followed by "arguments":"{}"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.

* Recorded instance of streaming tool response variant "b".

This is a streaming response where the first arguments
you get is a fully formed "arguments":"{}"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.

* Test cases for streaming tool responses.

Note that the replays are marked as "read-only", as they are variants
seen in the wild where the streaming tool call argument fragments
arrive in a specific order.

* Fix streaming tool response variant "b", where "arguments":"{}" is what arrives first.

The previous code erroneously caused the first "arguments" to be duplicated,
by using "+=" even when being initially set.

This went unnoticed as many models stream "arguments":"" first.

When a more fully formed "arguments" fragment arrived first, it was causing
"Error: Extra data: line 1 column 3 (char 2)"

* Recorded instance of streaming tool response variant "c".

This was failing with "Error: unsupported operand type(s) for +=: 'NoneType' and 'str'"

The response data is a real capture from the OpenRouter API,
however some request and header data may be from other test fixtures.

* Test case for streaming tool response variant "c".

* Fix streaming tool response variant "c".

However, I'm not sure why arguments was initially not present or seen as None.
2025-08-11 14:17:52 -07:00
Simon Willison cbd3aab511 GPT-5 model IDs, refs #1229 2025-08-11 13:23:36 -07:00
Simon Willison b5d1c5ee90 Tools can now return attachments
Closes #1014

- llm.ToolOutput(output='...', attachments=[...]) for tools to return attachments
- New table: `tool_results_attachments`
- Table is populated when tools return attachments
- llm --tools-debug shows attachments returned by tools
- llm logs shows attachments returned by tools
2025-06-01 10:08:36 -07:00
Simon Willison 2bc6d7679c New default tool, llm_time, closes #1103 2025-05-26 23:00:18 -07:00
Simon Willison b4365ceb35 Move llm_version into llm.tools namespace 2025-05-26 21:44:33 -07:00
Simon Willison 9bbb37fae0 New default llm_version tool, closes #1096
Refs https://github.com/simonw/llm/issues/1095#issuecomment-2910574597
2025-05-26 13:30:47 -07:00
Mahesh Hegde d5f7bf9073 Support supports_tools parameters in openai compatible models (#1068) 2025-05-23 22:30:51 -07:00
Simon Willison 3cb875fa3d Async tool support (#1063)
* Sync models can now call async tools, refs #987
* Test for async tool functions in sync context, refs #987
* Test for asyncio tools, plus test that they run in parallel
* Docs for async tool usage
2025-05-21 21:42:19 -07:00
Simon Willison ad7dc2ef71 Enable vision for o3 and o4-mini, closes #1037 2025-05-15 10:22:13 -04:00
Simon Willison bed617cd62 Enable tools for o3, o3-mini, o4-mini, refs #988 2025-05-13 17:19:30 -07:00
Simon Willison 88b806ae1a Got multi-tool OpenAI chat working, in no-stream mode too
Refs #1017, #1019
2025-05-13 17:19:30 -07:00
Simon Willison 0ff24b34c4 dict() is now model_dump() 2025-05-13 17:19:30 -07:00
Simon Willison f994196b32 tool_calls_or_raise()
Refs #992, #998, #999
2025-05-13 17:19:30 -07:00
Simon Willison 7dbf0b8586 Got a tool call to run through OpenAI
Refs https://github.com/simonw/llm/issues/937#issuecomment-2870434157
2025-05-13 17:19:30 -07:00
Simon Willison c990578934 ToolCall.tool_call_id property, refs #937 2025-05-13 17:19:30 -07:00
Simon Willison 7bc2f78156 Capture tool calls from OpenAI streaming sync responses
Refs https://github.com/simonw/llm/issues/988#issuecomment-2869079084
2025-05-13 17:19:30 -07:00
Simon Willison 84ab4cd409 supports_tools Model property, Tool.function(..., name=) option
Refs https://github.com/simonw/llm/issues/935#issuecomment-2869042481
2025-05-13 17:19:30 -07:00
Simon Willison 8e68c5e2d9 o4-mini, closes #976 2025-05-04 16:04:28 -07:00
Kevin Burke 5d0a2bba59 llm/default_plugins: add o3 model (#945)
* llm/default_plugins: add o3 model

This is the newest model released by OpenAI and is available through
the API.

* Ran cog

---------

Co-authored-by: Simon Willison <swillison@gmail.com>
2025-05-04 16:01:55 -07:00
Abizer Lokhandwala 0b37123a38 Add GPT-4.1 model family to default OpenAI plugin (#965)
* openai: add gpt-4.1 models
* Refactor and run cog

---------

Co-authored-by: Simon Willison <swillison@gmail.com>
2025-05-04 10:27:12 -07:00
giuli007 51db7afddb Support vision and audio for extra-openai-models.yaml (#843)
Add a vision option to enable OpenAI-compatible
models to receive image and audio attachments
2025-03-22 16:14:18 -07:00
adaitche de87d37c28 Add supports_schema to extra-openai-models (#819)
Recently support for structured output was added. But custom
OpenAI-compatible models didn't support the `supports_schema` property
in the config file `extra-openai-models.yaml`.
2025-03-21 16:59:34 -07:00
Simon Willison 2cbe46304b PDF support for vision models, refs #834 2025-03-18 15:38:28 -07:00
Simon Willison efe265137d Only some OpenAI models support schemas, closes #794 2025-02-27 15:02:39 -08:00
Simon Willison 6bec92fd78 Assign gpt-4.5 default alias, refs #795 2025-02-27 14:51:09 -08:00
Simon Willison 801b08bf40 gpt-4.5-preview and gpt-4.5-preview-2025-02-27, refs #795 2025-02-27 12:25:04 -08:00
Kasper Primdal Lauritzen 6cb16a1d1a Allow "reasoning" for extra-openai-models.yaml (#766)
* Allow "reasoning" for extra-openai-models.yaml

Currently you get an error when trying to use `-o reasoning_effort high` with a model that has been defined in `extra-openai-models.yaml`. 
This allows a `reasoning` field.

* Mention reasoning: true in other OpenAI models docs

---------

Co-authored-by: Simon Willison <swillison@gmail.com>
2025-02-26 21:50:14 -08:00
Simon Willison 9922d5bb6a model.prompt(prompt= is now optional, closes #784 2025-02-26 19:31:38 -08:00
Simon Willison 62c90dd472 llm prompt --schema X option and model.prompt(..., schema=) parameter (#777)
Refs #776

* Implemented new llm prompt --schema and model.prompt(schema=)
* Log schema to responses.schema_id and schemas table
* Include schema in llm logs Markdown output
* Test for schema=pydantic_model
* Initial --schema CLI documentation
* Python docs for schema=
* Advanced plugin docs on schemas
2025-02-26 16:58:28 -08:00
Simon Willison 849c65fe9d Upgrade to Pydantic v2 (#775)
* Upgrade to Pydantic v2
* Stop testing against Pydantic v1

Closes #520
2025-02-26 10:05:54 -08:00
Simon Willison 6c6b100f3e KeyModel and AsyncKeyModel classes for models that taken keys (#753)
* New KeyModel and AsyncKeyModel classes for models that taken keys - closes #744
* llm prompt --key now uses new mechanism, including for async
* use new key mechanism in llm chat command
* Python API tests for llm.KeyModel and llm.AsyncKeyModel
* Python API docs for for prompt(... key="")
* Mention await model.prompt() takes other parameters, reorg sections
* Better title for the model tutorial
* Docs on writing model plugins that take a key
2025-02-16 14:38:51 -08:00
Simon Willison b331b1e674 OpenAI model chatgpt-4o-latest, closes #752 2025-02-15 17:45:09 -08:00
Simon Willison eb0e1e761b o3-mini and reasoning_effort option, refs #728 2025-01-31 12:14:02 -08:00
Simon Willison 57d3baac42 Update embedding model names in docs, refs #654
Also ran Black.
2025-01-22 20:35:17 -08:00
web-sst 6f7ea406bf Register full embedding model names (#654)
Provide backward compatible aliases.
This makes available the same model names that ttok uses.
2025-01-22 20:14:03 -08:00