-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from stacklok/add-fim-pipeline
Add a FIM pipeline to Providers
- Loading branch information
Showing
9 changed files
with
293 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from litellm import ChatCompletionRequest | ||
|
||
from codegate.codegate_logging import setup_logging | ||
from codegate.pipeline.base import PipelineContext, PipelineResponse, PipelineResult, PipelineStep | ||
|
||
logger = setup_logging() | ||
|
||
|
||
class SecretAnalyzer(PipelineStep): | ||
"""Pipeline step that handles analyzing secrets in FIM pipeline.""" | ||
|
||
message_blocked = """ | ||
⚠️ CodeGate Security Warning! Analysis Report ⚠️ | ||
Potential leak of sensitive credentials blocked | ||
Recommendations: | ||
- Use environment variables for secrets | ||
""" | ||
|
||
@property | ||
def name(self) -> str: | ||
""" | ||
Returns the name of this pipeline step. | ||
Returns: | ||
str: The identifier 'fim-secret-analyzer' | ||
""" | ||
return "fim-secret-analyzer" | ||
|
||
async def process( | ||
self, | ||
request: ChatCompletionRequest, | ||
context: PipelineContext | ||
) -> PipelineResult: | ||
# We should call here Secrets Blocking module to see if the request messages contain secrets | ||
# messages_contain_secrets = [analyze_msg_secrets(msg) for msg in request.messages] | ||
# message_with_secrets = any(messages_contain_secretes) | ||
|
||
# For the moment to test shortcutting just treat all messages as if they contain secrets | ||
message_with_secrets = False | ||
if message_with_secrets: | ||
logger.info('Blocking message with secrets.') | ||
return PipelineResult( | ||
response=PipelineResponse( | ||
step_name=self.name, | ||
content=self.message_blocked, | ||
model=request["model"], | ||
), | ||
) | ||
|
||
# No messages with secrets, execute the rest of the pipeline | ||
return PipelineResult(request=request) |
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,33 @@ | ||
from typing import AsyncIterator, Optional, Union | ||
|
||
from litellm import ChatCompletionRequest, ModelResponse | ||
|
||
from codegate.providers.litellmshim import LiteLLmShim | ||
|
||
|
||
class AnthropicCompletion(LiteLLmShim): | ||
""" | ||
AnthropicCompletion used by the Anthropic provider to execute completions | ||
""" | ||
|
||
async def execute_completion( | ||
self, | ||
request: ChatCompletionRequest, | ||
api_key: Optional[str], | ||
stream: bool = False, | ||
) -> Union[ModelResponse, AsyncIterator[ModelResponse]]: | ||
""" | ||
Ensures the model name is prefixed with 'anthropic/' to explicitly route to Anthropic's API. | ||
LiteLLM automatically maps most model names, but prepending 'anthropic/' forces the request | ||
to Anthropic. This avoids issues with unrecognized names like 'claude-3-5-sonnet-latest', | ||
which LiteLLM doesn't accept as a valid Anthropic model. This safeguard may be unnecessary | ||
but ensures compatibility. | ||
For more details, refer to the | ||
[LiteLLM Documentation](https://docs.litellm.ai/docs/providers/anthropic). | ||
""" | ||
model_in_request = request['model'] | ||
if not model_in_request.startswith('anthropic/'): | ||
request['model'] = f'anthropic/{model_in_request}' | ||
return await super().execute_completion(request, api_key, stream) |
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
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
Oops, something went wrong.