-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support non-textual requests (#84)
- Loading branch information
Showing
5 changed files
with
437 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from logging import Logger | ||
from typing import List | ||
|
||
|
||
def get_chat_completion_request_contents( | ||
logger: Logger, request: dict | ||
) -> List[str]: | ||
return [ | ||
content | ||
for message in request["messages"] | ||
for content in _get_chat_completion_message_contents(logger, message) | ||
] | ||
|
||
|
||
def get_chat_completion_response_contents( | ||
logger: Logger, response: dict | ||
) -> List[str]: | ||
message = response["choices"][0]["message"] | ||
return _get_chat_completion_message_contents(logger, message) | ||
|
||
|
||
def get_embeddings_request_contents(logger: Logger, request: dict) -> List[str]: | ||
inp = request.get("input") | ||
|
||
if isinstance(inp, str): | ||
return [inp] | ||
elif isinstance(inp, list): | ||
return [i for i in inp if isinstance(i, str)] | ||
else: | ||
logger.warning(f"Unexpected type of embeddings input: {type(inp)}") | ||
return [] | ||
|
||
|
||
def _get_chat_completion_message_contents( | ||
logger: Logger, message: dict | ||
) -> List[str]: | ||
content = message.get("content") | ||
if content is None: | ||
return [] | ||
elif isinstance(content, str): | ||
return [content] | ||
elif isinstance(content, list): | ||
ret: List[str] = [] | ||
for content_part in content: | ||
if isinstance(content_part, dict): | ||
if content_part.get("type") == "text" and ( | ||
text := content_part.get("content") | ||
): | ||
ret.extend(text) | ||
return ret | ||
else: | ||
logger.warning(f"Unexpected message content type: {type(content)}") | ||
return [] |
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
Oops, something went wrong.