|
| 1 | +from typing import Any, AsyncIterable, AsyncIterator, Dict, Iterable, Iterator, Union |
| 2 | + |
| 3 | +from litellm import ChatCompletionRequest, ModelResponse |
| 4 | + |
| 5 | +from codegate.providers.normalizer import ModelInputNormalizer, ModelOutputNormalizer |
| 6 | + |
| 7 | + |
| 8 | +class LLamaCppInputNormalizer(ModelInputNormalizer): |
| 9 | + def normalize(self, data: Dict) -> ChatCompletionRequest: |
| 10 | + """ |
| 11 | + Normalize the input data |
| 12 | + """ |
| 13 | + try: |
| 14 | + return ChatCompletionRequest(**data) |
| 15 | + except Exception as e: |
| 16 | + raise ValueError(f"Invalid completion parameters: {str(e)}") |
| 17 | + |
| 18 | + def denormalize(self, data: ChatCompletionRequest) -> Dict: |
| 19 | + """ |
| 20 | + Denormalize the input data |
| 21 | + """ |
| 22 | + return data |
| 23 | + |
| 24 | + |
| 25 | +class LLamaCppOutputNormalizer(ModelOutputNormalizer): |
| 26 | + def normalize_streaming( |
| 27 | + self, |
| 28 | + model_reply: Union[AsyncIterable[Any], Iterable[Any]], |
| 29 | + ) -> Union[AsyncIterator[ModelResponse], Iterator[ModelResponse]]: |
| 30 | + """ |
| 31 | + Normalize the output stream. This is a pass-through for liteLLM output normalizer |
| 32 | + as the liteLLM output is already in the normalized format. |
| 33 | + """ |
| 34 | + return model_reply |
| 35 | + |
| 36 | + def normalize(self, model_reply: Any) -> ModelResponse: |
| 37 | + """ |
| 38 | + Normalize the output data. This is a pass-through for liteLLM output normalizer |
| 39 | + as the liteLLM output is already in the normalized format. |
| 40 | + """ |
| 41 | + return model_reply |
| 42 | + |
| 43 | + def denormalize(self, normalized_reply: ModelResponse) -> Any: |
| 44 | + """ |
| 45 | + Denormalize the output data from the completion function to the format |
| 46 | + expected by the client |
| 47 | + """ |
| 48 | + return normalized_reply |
| 49 | + |
| 50 | + def denormalize_streaming( |
| 51 | + self, |
| 52 | + normalized_reply: Union[AsyncIterable[ModelResponse], Iterable[ModelResponse]], |
| 53 | + ) -> Union[AsyncIterator[Any], Iterator[Any]]: |
| 54 | + """ |
| 55 | + Denormalize the output stream from the completion function to the format |
| 56 | + expected by the client |
| 57 | + """ |
| 58 | + return normalized_reply |
0 commit comments