22
33import os
44from dataclasses import dataclass
5- from typing import TYPE_CHECKING
65
76from typing_extensions import Self
87
9- try :
10- from openai import AzureOpenAI , OpenAI
11- except ImportError :
12- OpenAI = None
13- AzureOpenAI = None
14-
15- try :
16- from azure .ai .inference import ChatCompletionsClient
17- from azure .core .credentials import AzureKeyCredential
18- except ImportError :
19- ChatCompletionsClient = None
20- AzureKeyCredential = None
21-
22- if TYPE_CHECKING :
23- from openai import OpenAI
24- from azure .ai .inference import ChatCompletionsClient
25- from azure .core .credentials import AzureKeyCredential
26-
278from codemodder .logging import logger
289
2910__all__ = [
3011 "MODELS" ,
31- "setup_openai_llm_client" ,
32- "setup_azure_llama_llm_client" ,
33- "MisconfiguredAIClient" ,
3412 "TokenUsage" ,
3513 "log_token_usage" ,
3614]
4220 "o1-mini" ,
4321 "o1" ,
4422]
45- DEFAULT_AZURE_OPENAI_API_VERSION = "2024-02-01"
4623
4724
4825class ModelRegistry (dict ):
@@ -66,68 +43,6 @@ def __getattr__(self, name):
6643MODELS = ModelRegistry (models )
6744
6845
69- def setup_openai_llm_client () -> OpenAI | None :
70- """Configure either the Azure OpenAI LLM client or the OpenAI client, in that order."""
71- if not AzureOpenAI :
72- logger .info ("Azure OpenAI API client not available" )
73- return None
74-
75- azure_openapi_key = os .getenv ("CODEMODDER_AZURE_OPENAI_API_KEY" )
76- azure_openapi_endpoint = os .getenv ("CODEMODDER_AZURE_OPENAI_ENDPOINT" )
77- if bool (azure_openapi_key ) ^ bool (azure_openapi_endpoint ):
78- raise MisconfiguredAIClient (
79- "Azure OpenAI API key and endpoint must both be set or unset"
80- )
81-
82- if azure_openapi_key and azure_openapi_endpoint :
83- logger .info ("Using Azure OpenAI API client" )
84- return AzureOpenAI (
85- api_key = azure_openapi_key ,
86- api_version = os .getenv (
87- "CODEMODDER_AZURE_OPENAI_API_VERSION" ,
88- DEFAULT_AZURE_OPENAI_API_VERSION ,
89- ),
90- azure_endpoint = azure_openapi_endpoint ,
91- )
92-
93- if not OpenAI :
94- logger .info ("OpenAI API client not available" )
95- return None
96-
97- if not (api_key := os .getenv ("CODEMODDER_OPENAI_API_KEY" )):
98- logger .info ("OpenAI API key not found" )
99- return None
100-
101- logger .info ("Using OpenAI API client" )
102- return OpenAI (api_key = api_key )
103-
104-
105- def setup_azure_llama_llm_client () -> ChatCompletionsClient | None :
106- """Configure the Azure Llama LLM client."""
107- if not ChatCompletionsClient :
108- logger .info ("Azure Llama client not available" )
109- return None
110-
111- azure_llama_key = os .getenv ("CODEMODDER_AZURE_LLAMA_API_KEY" )
112- azure_llama_endpoint = os .getenv ("CODEMODDER_AZURE_LLAMA_ENDPOINT" )
113- if bool (azure_llama_key ) ^ bool (azure_llama_endpoint ):
114- raise MisconfiguredAIClient (
115- "Azure Llama API key and endpoint must both be set or unset"
116- )
117-
118- if azure_llama_key and azure_llama_endpoint :
119- logger .info ("Using Azure Llama API client" )
120- return ChatCompletionsClient (
121- credential = AzureKeyCredential (azure_llama_key ),
122- endpoint = azure_llama_endpoint ,
123- )
124- return None
125-
126-
127- class MisconfiguredAIClient (ValueError ):
128- pass
129-
130-
13146@dataclass
13247class TokenUsage :
13348 completion_tokens : int = 0
0 commit comments