-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add openai v1 compatible endpoint for chat completions
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
from typing import List, Dict, Any | ||
from guardrails.classes import ValidationOutcome | ||
|
||
def outcome_to_stream_response(validation_outcome: ValidationOutcome): | ||
stream_chunk_template ={ | ||
"choices": [ | ||
{ | ||
"delta": { | ||
"content": validation_outcome.validated_output, | ||
}, | ||
} | ||
], | ||
"guardrails": { | ||
"reask": validation_outcome.reask or None, | ||
"validation_passed": validation_outcome.validation_passed, | ||
"error": validation_outcome.error or None, | ||
}, | ||
} | ||
stream_chunk = getattr(validation_outcome, "full_raw_llm_output", stream_chunk_template) | ||
stream_chunk["choices"][0]["delta"]["content"] = validation_outcome.validated_output | ||
return stream_chunk | ||
|
||
def outcome_to_chat_completion(validation_outcome: ValidationOutcome, has_tool_gd_tool_call=False,): | ||
completion_template = { | ||
"choices": [{"message": {"content": ""}}] | ||
} if not has_tool_gd_tool_call else { | ||
"choices": [{"message": {"tool_calls": [{"function": {"arguments": ""}}]}}] | ||
} | ||
completion = getattr(validation_outcome, "full_raw_llm_output", completion_template) | ||
completion["guardrails"] = { | ||
"reask": validation_outcome.reask or None, | ||
"validation_passed": validation_outcome.validation_passed, | ||
"error": validation_outcome.error or None, | ||
} | ||
|
||
# string completion | ||
try: | ||
completion["choices"][0]["message"]["content"] = validation_outcome.validated_output | ||
except: | ||
pass | ||
|
||
# tool completion | ||
try: | ||
choice = completion["choices"][0] | ||
# if this is accessible it means a tool was called so set our validated output to that | ||
choice["message"]["tool_calls"][-1]["function"]["arguments"] = validation_outcome.validated_output | ||
except: | ||
pass | ||
|
||
return completion |
This file contains 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