-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move setup llm and models to own module * limit what is imported * add differror to catch it
- Loading branch information
1 parent
88959ea
commit 571e809
Showing
7 changed files
with
134 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
BaseDjangoCodemodTest, | ||
BaseSASTCodemodTest, | ||
BaseSemgrepCodemodTest, | ||
DiffError, | ||
) |
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,83 @@ | ||
import os | ||
from typing import TYPE_CHECKING | ||
|
||
try: | ||
from openai import AzureOpenAI, OpenAI | ||
except ImportError: | ||
OpenAI = None | ||
AzureOpenAI = None | ||
|
||
|
||
if TYPE_CHECKING: | ||
from openai import OpenAI | ||
|
||
from codemodder.logging import logger | ||
|
||
__all__ = [ | ||
"MODELS", | ||
"setup_llm_client", | ||
"MisconfiguredAIClient", | ||
] | ||
|
||
models = ["gpt-4-turbo-2024-04-09", "gpt-4o-2024-05-13"] | ||
DEFAULT_AZURE_OPENAI_API_VERSION = "2024-02-01" | ||
|
||
|
||
class ModelRegistry(dict): | ||
def __init__(self, models): | ||
super().__init__() | ||
self.models = models | ||
for model in models: | ||
attribute_name = model.replace("-", "_") | ||
self[attribute_name] = model | ||
|
||
def __getattr__(self, name): | ||
if name in self: | ||
return os.getenv( | ||
f"CODEMODDER_AZURE_OPENAI_{self[name].upper()}_DEPLOYMENT", self[name] | ||
) | ||
raise AttributeError( | ||
f"'{self.__class__.__name__}' object has no attribute '{name}'" | ||
) | ||
|
||
|
||
MODELS = ModelRegistry(models) | ||
|
||
|
||
def setup_llm_client() -> OpenAI | None: | ||
if not AzureOpenAI: | ||
logger.info("Azure OpenAI API client not available") | ||
return None | ||
|
||
azure_openapi_key = os.getenv("CODEMODDER_AZURE_OPENAI_API_KEY") | ||
azure_openapi_endpoint = os.getenv("CODEMODDER_AZURE_OPENAI_ENDPOINT") | ||
if bool(azure_openapi_key) ^ bool(azure_openapi_endpoint): | ||
raise MisconfiguredAIClient( | ||
"Azure OpenAI API key and endpoint must both be set or unset" | ||
) | ||
|
||
if azure_openapi_key and azure_openapi_endpoint: | ||
logger.info("Using Azure OpenAI API client") | ||
return AzureOpenAI( | ||
api_key=azure_openapi_key, | ||
api_version=os.getenv( | ||
"CODEMODDER_AZURE_OPENAI_API_VERSION", | ||
DEFAULT_AZURE_OPENAI_API_VERSION, | ||
), | ||
azure_endpoint=azure_openapi_endpoint, | ||
) | ||
|
||
if not OpenAI: | ||
logger.info("OpenAI API client not available") | ||
return None | ||
|
||
if not (api_key := os.getenv("CODEMODDER_OPENAI_API_KEY")): | ||
logger.info("OpenAI API key not found") | ||
return None | ||
|
||
logger.info("Using OpenAI API client") | ||
return OpenAI(api_key=api_key) | ||
|
||
|
||
class MisconfiguredAIClient(ValueError): | ||
pass |
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,21 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from codemodder.llm import MODELS | ||
|
||
|
||
class TestModels: | ||
def test_get_model_name(self): | ||
assert MODELS.gpt_4_turbo_2024_04_09 == "gpt-4-turbo-2024-04-09" | ||
|
||
@pytest.mark.parametrize("model", ["gpt-4-turbo-2024-04-09", "gpt-4o-2024-05-13"]) | ||
def test_model_get_name_from_env(self, mocker, model): | ||
name = "my-awesome-deployment" | ||
mocker.patch.dict( | ||
os.environ, | ||
{ | ||
f"CODEMODDER_AZURE_OPENAI_{model.upper()}_DEPLOYMENT": name, | ||
}, | ||
) | ||
assert getattr(MODELS, model.replace("-", "_")) == name |