Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gateway/routes/allowlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
"/rerank",
"/v1/decisions",
"/decisions",
"/v1/systemone",
"/systemone",
"/v1/ocr",
"/ocr",
"/v1/rag/",
Expand Down
127 changes: 127 additions & 0 deletions litellm/decisions/openai_transformation.py
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,
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
),
)
2 changes: 1 addition & 1 deletion litellm/proxy/_lazy_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def matches(self, path: str) -> bool:
LazyFeature(
name="decisions",
module_path="litellm.proxy.decisions_endpoints.endpoints",
path_prefixes=("/v1/decisions", "/decisions"),
path_prefixes=("/v1/decisions", "/decisions", "/v1/systemone", "/systemone"),
),
LazyFeature(
name="claude_code_marketplace",
Expand Down
48 changes: 48 additions & 0 deletions litellm/proxy/_lazy_openapi_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -9521,6 +9521,30 @@
]
}
},
"/systemone": {
"post": {
"operationId": "systemone_systemone_post",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {}
}
},
"description": "Successful Response"
}
},
"security": [
{
"APIKeyHeader": []
}
],
"summary": "Systemone",
"tags": [
"decisions"
]
}
},
"/v1/decisions": {
"post": {
"operationId": "decisions_v1_decisions_post",
Expand All @@ -9544,6 +9568,30 @@
"decisions"
]
}
},
"/v1/systemone": {
"post": {
"operationId": "systemone_v1_systemone_post",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {}
}
},
"description": "Successful Response"
}
},
"security": [
{
"APIKeyHeader": []
}
],
"summary": "Systemone",
"tags": [
"decisions"
]
}
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions litellm/proxy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,8 @@ class LiteLLMRoutes(enum.Enum):
"/v1/search/{search_tool_name}",
"/decisions",
"/v1/decisions",
"/systemone",
"/v1/systemone",
# OCR
"/ocr",
"/v1/ocr",
Expand Down
2 changes: 2 additions & 0 deletions litellm/proxy/agent_endpoints/auth/managed_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"moderations",
"rerank",
"decisions",
"systemone",
"ocr",
),
)
Expand Down Expand Up @@ -74,6 +75,7 @@
"/audio/speech": "speech",
"/rerank": "body",
"/decisions": "body",
"/systemone": "body",
"/messages/count_tokens": "body",
":countTokens": "path",
}
Expand Down
Loading
Loading