feat: add LiteLLM as AI gateway prompt target#2154
Conversation
|
I've been wanting to do this also; @RheagalFire this is a great start. I'll likely push to your branch to flush it out and support all the things (multi-modal, identifiers, underlying model, integration tests, capabilities detection, other gaps). So from your perspective I would consider this "merged" and I'll take it and try to get it in before the next release. TY for the help and nudge! |
…s, token usage) Extends the LiteLLM target for parity with OpenAIChatTarget and shares logic instead of reinventing it: - Extract shared Chat Completions helpers (chat_completions_message_builder, chat_completions_response_parser) used by both OpenAIChatTarget and LiteLLMChatTarget for request building and response parsing (text, image, audio, tool calls, content-filter handling). - Add multimodal support (image + audio input, audio output via audio_response_config) with capabilities derived from LiteLLM's model registry and a conservative text-only fallback. - Support the full OpenAI parameter set plus an extra_body_parameters passthrough; auth via sync/async token providers; identifiers that exclude the api_key; underlying_model capability lookup; and LiteLLM-owned retry (num_retries from PyRIT's global convention) to avoid double-retrying. - Add a provider-neutral TokenUsage value object (input/output/total/reasoning/cached + extra) and capture it for both targets; capture LiteLLM per-call cost. - Add litellm as an optional extra and include it in the 'all' extra. - Modernize type syntax (X | None), tidy docstrings per the style guide, and add unit + integration tests (image/audio on a gpt-5 deployment). Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… object Make TokenUsage a pure value object (fields + to_metadata/from_metadata) and move Chat Completions usage parsing into chat_completions_response_parser as token_usage_from_chat_completion, explicit to the one wire shape both chat targets actually send. - Drop the speculative Responses-API sniffing (no caller sends that shape); a Responses target should parse its own usage in its own module. - Tolerate dict-or-attribute usage payloads so a mapping no longer silently yields all-None counts. - Capture LiteLLM/Anthropic top-level cache fields (cache_read_input_tokens -> cached_tokens, cache_creation_input_tokens -> extra), preserving a zero cached count. - Move/expand parsing tests into test_chat_completions_helpers; test_token_usage now covers only metadata round-tripping. - Convert remaining Sphinx roles to plain double-backtick references. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ts to gpt-5.4 Revert the PLATFORM_OPENAI_CHAT_MODEL / PLATFORM_OPENAI_AUDIO_MODEL additions to .env_example and default the chat/vision integration fixtures to the deployed gpt-5.4 model instead of the generic "gpt-5". Also convert two stray Sphinx roles in the integration test docstrings to plain double-backtick references. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
| "azure-cognitiveservices-speech>=1.46.0", | ||
| ] | ||
| litellm = [ | ||
| "litellm>=1.83.0,<2.0.0", |
There was a problem hiding this comment.
FWIW it doesn't add a ton on top of what we already have. Could consider making it a dependency without extra. That could change for litellm, of course, and then we'd have to move it into an extra. The current state as extra is probably the safest.
There was a problem hiding this comment.
I like a lot about liteLLM, I could see it being a default target (instead of OpenAI). But ofc I want to battle test it.
So maybe making it a dependency is a good idea.
Description
Adds
LiteLLMChatTarget- a new prompt target that uses the LiteLLM SDK to access 100+ LLM providers (Anthropic, AWS Bedrock, Google Vertex, Cohere, etc.) directly, without requiring a separate proxy server.Changes:
pyrit/prompt_target/litellm_chat_target.py- NewLiteLLMChatTargetextendingPromptTargetwith:litellm.acompletion()for async chat completionsdrop_params=Trueby default (silently drops provider-unsupported kwargs for cross-provider compatibility)litellm.exceptions.*mapped to PyRIT'sRateLimitException/PyritExceptionhierarchy (no bareExceptioncatches)import litellmso users without the package are unaffectedpyrit/prompt_target/__init__.py- RegisteredLiteLLMChatTargetpyproject.toml- Addedlitellm>=1.83.0,<2.0.0as optional dependency (pip install pyrit[litellm])Tests and Documentation
22 unit tests in
tests/unit/prompt_target/target/test_litellm_chat_target.py:ValueError,LITELLM_MODELenv var fallback, explicit overridedrop_params=Truedefault + opt-out withdrop_params=FalseEmptyResponseException, empty content raisesEmptyResponseExceptionlitellm.exceptions.RateLimitError->RateLimitException,AuthenticationError->PyritException,APIConnectionError/Timeout-> retryableRateLimitException, unknown errors ->PyritExceptionstop,length,tool_calls,content_filteraccepted; unknown reasons raisePyritExceptionAll 22 tests pass:
Ruff lint clean (line-length=120, preview=true).
JupyText not applicable - no notebook documentation added for this target yet.