Repository navigation
feat(decisions): serve System One format at /v1/systemone and OpenAI format at /v1/decisions #45184
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 all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3339469
feat(decisions): serve System One format at /v1/systemone and OpenAI …
mateo-berri 29e7154
fix(decisions): import assert_never from typing_extensions for Python…
mateo-berri 7ef1329
fix(decisions): cap OpenAI-format questions at the System One limit
mateo-berri b6bb885
fix(decisions): return 400 for bodies that are not JSON
mateo-berri 77872f6
test(decisions): send System One bodies to /v1/systemone in integrati…
mateo-berri 81d8bac
Merge remote-tracking branch 'origin/main' into litellm_systemone_end…
mateo-berri 2be582a
Merge remote-tracking branch 'origin/main' into litellm_systemone_end…
mateo-berri 855f0bb
fix(decisions): return guardrail_information on OpenAI-format decisio…
mateo-berri f360214
refactor(decisions): validate proxy request data before reading guard…
mateo-berri 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import Final | ||
|
|
||
| from typing_extensions import assert_never | ||
|
|
||
| from litellm.types.decisions import ( | ||
| ChoiceAnswer, | ||
| DecisionAnswer, | ||
| DecisionsResponse, | ||
| DecisionsUsage, | ||
| NoulAnswer, | ||
| OpenAIChoiceAnswer, | ||
| OpenAIChoiceProbability, | ||
| OpenAIChoiceQuestion, | ||
| OpenAIDecisionAnswer, | ||
| OpenAIDecisionInputMessage, | ||
| OpenAIDecisionQuestion, | ||
| OpenAIDecisionRequestBody, | ||
| OpenAIDecisionResponse, | ||
| OpenAIDecisionUsage, | ||
| OpenAIPredicateAnswer, | ||
| OpenAIPredicateQuestion, | ||
| OpenAIRefusalAnswer, | ||
| OpenAIScoreAnswer, | ||
| OpenAIScoreLevel, | ||
| OpenAIScoreProbability, | ||
| OpenAIScoreQuestion, | ||
| ScoreAnswer, | ||
| systemone_choice_key, | ||
| ) | ||
|
|
||
| _OPENAI_ONLY_FIELDS: Final = frozenset({"input", "questions", "safety_identifier"}) | ||
|
|
||
|
|
||
| def _message_text(message: OpenAIDecisionInputMessage) -> str: | ||
| if isinstance(message.content, str): | ||
| return message.content | ||
| return "\n\n".join(part.text for part in message.content) | ||
|
|
||
|
|
||
| def _state(decision_input: str | Sequence[OpenAIDecisionInputMessage]) -> str: | ||
| if isinstance(decision_input, str): | ||
| return decision_input | ||
| return "\n\n".join(_message_text(message) for message in decision_input) | ||
|
|
||
|
|
||
| def _level_criterion(level: OpenAIScoreLevel) -> str: | ||
| return level.label if level.description is None else f"{level.label}: {level.description}" | ||
|
|
||
|
|
||
| def _systemone_question(question: OpenAIDecisionQuestion) -> Mapping[str, object]: | ||
| match question: | ||
| case OpenAIPredicateQuestion(): | ||
| return {"type": "noul", "instructions": question.instructions} | ||
| case OpenAIChoiceQuestion(): | ||
| return { | ||
| "type": "choice", | ||
| "instructions": question.instructions, | ||
| "criteria": {systemone_choice_key(option.value): option.description for option in question.choices}, | ||
| } | ||
| case OpenAIScoreQuestion(): | ||
| return { | ||
| "type": "score", | ||
| "instructions": question.instructions, | ||
| "criteria": [_level_criterion(level) for level in question.levels], | ||
| } | ||
| case _: | ||
| assert_never(question) | ||
|
|
||
|
|
||
| def to_systemone_request(request_data: Mapping[str, object], body: OpenAIDecisionRequestBody) -> Mapping[str, object]: | ||
| return { | ||
| **{key: value for key, value in request_data.items() if key not in _OPENAI_ONLY_FIELDS}, | ||
| "state": _state(body.input), | ||
| "questions": {str(index): _systemone_question(question) for index, question in enumerate(body.questions)}, | ||
| } | ||
|
|
||
|
|
||
| def _openai_answer(question: OpenAIDecisionQuestion, answer: DecisionAnswer | None) -> OpenAIDecisionAnswer: | ||
| match question, answer: | ||
| case OpenAIPredicateQuestion(), NoulAnswer(): | ||
| return OpenAIPredicateAnswer(name=question.name, probability=answer.noul) | ||
| case OpenAIChoiceQuestion(), ChoiceAnswer(): | ||
| typed_values: Final = {systemone_choice_key(option.value): option.value for option in question.choices} | ||
| return OpenAIChoiceAnswer( | ||
| name=question.name, | ||
| choice=typed_values.get(answer.choice, answer.choice), | ||
| probabilities=tuple( | ||
| OpenAIChoiceProbability( | ||
| value=option.value, | ||
| probability=answer.probabilities.get(systemone_choice_key(option.value), 0.0), | ||
| ) | ||
| for option in question.choices | ||
| ), | ||
| confidence=answer.confidence, | ||
| ) | ||
| case OpenAIScoreQuestion(), ScoreAnswer(): | ||
| return OpenAIScoreAnswer( | ||
| name=question.name, | ||
| score=answer.score, | ||
| probabilities=tuple( | ||
| OpenAIScoreProbability( | ||
| value=index, label=level.label, probability=answer.probabilities.get(str(index), 0.0) | ||
| ) | ||
| for index, level in enumerate(question.levels) | ||
| ), | ||
| confidence=answer.confidence, | ||
| ) | ||
| case _: | ||
| return OpenAIRefusalAnswer(name=question.name) | ||
|
|
||
|
|
||
| def to_openai_response( | ||
| response: DecisionsResponse, questions: Sequence[OpenAIDecisionQuestion], requested_model: str | ||
| ) -> OpenAIDecisionResponse: | ||
| usage: Final = response.usage or DecisionsUsage() | ||
| return OpenAIDecisionResponse( | ||
| model=response.model or requested_model, | ||
| answers=tuple( | ||
| _openai_answer(question, response.answers.get(str(index))) for index, question in enumerate(questions) | ||
| ), | ||
| usage=OpenAIDecisionUsage( | ||
| input_tokens=usage.input_tokens, | ||
| output_tokens=usage.output_tokens, | ||
| total_tokens=usage.input_tokens + usage.output_tokens, | ||
| ), | ||
| ) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.