-
Notifications
You must be signed in to change notification settings - Fork 55
Add SSL verification and chat model support for LiteLLM provider #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NiklasPhabian
wants to merge
1
commit into
plmbr:main
Choose a base branch
from
NiklasPhabian:feature/litellm-ssl-and-chat-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ def __init__(self, provider: "LiteLLMCompatibleLLMProvider"): | |
| LLMProviderProperty("base_url", "Base URL", "Base URL", "", False), | ||
| LLMProviderProperty("api_key", "API key", "API key", "", True), | ||
| LLMProviderProperty("context_window", "Context window", "Context window length", "", True), | ||
| LLMProviderProperty("verify_ssl", "Verify SSL", "Verify SSL certificates (disable for self-signed certificates)", "true", True), | ||
| ] | ||
|
|
||
| @property | ||
|
|
@@ -42,6 +43,16 @@ def completions(self, messages: list[dict], tools: list[dict] = None, response: | |
| base_url = self.get_property("base_url").value | ||
| api_key_prop = self.get_property("api_key") | ||
| api_key = api_key_prop.value if api_key_prop is not None else None | ||
|
|
||
| # Read SSL verification setting (default to True for security) | ||
| verify_ssl_prop = self.get_property("verify_ssl") | ||
| verify_ssl = True | ||
| if verify_ssl_prop is not None and verify_ssl_prop.value: | ||
| verify_ssl = verify_ssl_prop.value.lower() not in ('false', '0', 'no', 'off') | ||
|
|
||
| # Set SSL verification on litellm module | ||
| litellm.ssl_verify = verify_ssl | ||
|
|
||
| litellm_resp = litellm.completion( | ||
| model=model_id, | ||
| messages=messages.copy(), | ||
|
|
@@ -77,6 +88,7 @@ def __init__(self, provider: "LiteLLMCompatibleLLMProvider"): | |
| LLMProviderProperty("base_url", "Base URL", "Base URL", "", False), | ||
| LLMProviderProperty("api_key", "API key", "API key", "", True), | ||
| LLMProviderProperty("context_window", "Context window", "Context window length", "", True), | ||
| LLMProviderProperty("verify_ssl", "Verify SSL", "Verify SSL certificates (disable for self-signed certificates)", "true", True), | ||
| ] | ||
|
|
||
| @property | ||
|
|
@@ -102,16 +114,62 @@ def inline_completions(self, prefix, suffix, language, filename, context: Comple | |
| base_url = self.get_property("base_url").value | ||
| api_key_prop = self.get_property("api_key") | ||
| api_key = api_key_prop.value if api_key_prop is not None else None | ||
| litellm_resp = litellm.completion( | ||
| model=model_id, | ||
| prompt=prefix, | ||
| suffix=suffix, | ||
| stream=False, | ||
| api_base=base_url, | ||
| api_key=api_key, | ||
| ) | ||
|
|
||
| return litellm_resp.choices[0].message.content | ||
| # Read SSL verification setting (default to True for security) | ||
| verify_ssl_prop = self.get_property("verify_ssl") | ||
| verify_ssl = True | ||
| if verify_ssl_prop is not None and verify_ssl_prop.value: | ||
| verify_ssl = verify_ssl_prop.value.lower() not in ('false', '0', 'no', 'off') | ||
|
|
||
| # Set SSL verification on litellm module | ||
| litellm.ssl_verify = verify_ssl | ||
|
|
||
| # Try FIM format first (for models that support fill-in-the-middle) | ||
| try: | ||
| litellm_resp = litellm.completion( | ||
| model=model_id, | ||
| prompt=prefix, | ||
| suffix=suffix, | ||
| stream=False, | ||
| api_base=base_url, | ||
| api_key=api_key, | ||
| ) | ||
| return litellm_resp.choices[0].message.content | ||
| except Exception as e: | ||
| # Fall back to chat format for models that don't support FIM | ||
| prompt_text = f"You are an autocomplete engine. Complete the {language} code at the cursor position. " | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be better to move this logic to a method. |
||
| prompt_text += f"Return ONLY the raw code to insert at the cursor. No markdown, no code blocks, no explanations.\n\n" | ||
| prompt_text += f"Code before cursor:\n{prefix}\n\n" | ||
| if suffix: | ||
| prompt_text += f"Code after cursor:\n{suffix}\n\n" | ||
| prompt_text += "Complete the code at the cursor position with ONLY raw code:" | ||
|
|
||
| messages = [ | ||
| {"role": "user", "content": prompt_text} | ||
| ] | ||
|
|
||
| litellm_resp = litellm.completion( | ||
| model=model_id, | ||
| messages=messages, | ||
| stream=False, | ||
| api_base=base_url, | ||
| api_key=api_key, | ||
| ) | ||
|
|
||
| completion = litellm_resp.choices[0].message.content | ||
|
|
||
| # Strip markdown code blocks if present | ||
| if completion.startswith("```"): | ||
| # Remove opening code fence | ||
| lines = completion.split("\n") | ||
| if lines[0].startswith("```"): | ||
| lines = lines[1:] | ||
| # Remove closing code fence | ||
| if lines and lines[-1].strip() == "```": | ||
| lines = lines[:-1] | ||
| completion = "\n".join(lines) | ||
|
|
||
| return completion | ||
|
|
||
| class LiteLLMCompatibleLLMProvider(LLMProvider): | ||
| def __init__(self): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NiklasPhabian can you use the same logic / prompts as in #117?