Skip to content

Added Azure OpenAI support #41

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcpcli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def cli_main():

parser.add_argument(
"--provider",
choices=["openai", "anthropic", "ollama"],
choices=["openai", "azure-openai", "anthropic", "ollama"],
default="openai",
help="LLM provider to use. Defaults to 'openai'.",
)
Expand Down
38 changes: 37 additions & 1 deletion src/mcpcli/llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import ollama
from dotenv import load_dotenv
from openai import OpenAI
from openai import OpenAI, AzureOpenAI
from anthropic import Anthropic

# Load environment variables
Expand All @@ -25,6 +25,18 @@ def __init__(self, provider="openai", model="gpt-4o-mini", api_key=None):
self.api_key = self.api_key or os.getenv("OPENAI_API_KEY")
if not self.api_key:
raise ValueError("The OPENAI_API_KEY environment variable is not set.")
# check azure openai api key
elif provider == "azure-openai":
self.az_endpoint = os.getenv("AZ_OPENAI_ENDPOINT")
self.az_api_key = os.getenv("AZ_OPENAI_API_KEY")
self.az_api_version = os.getenv("AZ_OPENAI_API_VERSION")

if not self.az_endpoint:
raise ValueError("The AZ_OPENAI_ENDPOINT environment variable is not set.")
if not self.az_api_key:
raise ValueError("The AZ_OPENAI_API_KEY environment variable is not set.")
if not self.az_api_version:
raise ValueError("The AZ_OPENAI_API_VERSION environment variable is not set.")
# check anthropic api key
elif provider == "anthropic":
self.api_key = self.api_key or os.getenv("ANTHROPIC_API_KEY")
Expand All @@ -41,6 +53,9 @@ def create_completion(
if self.provider == "openai":
# perform an openai completion
return self._openai_completion(messages, tools)
elif self.provider == "azure-openai":
# perform an azure openai completion
return self._azure_openai_completion(messages, tools)
elif self.provider == "anthropic":
# perform an anthropic completion
return self._anthropic_completion(messages, tools)
Expand Down Expand Up @@ -73,6 +88,27 @@ def _openai_completion(self, messages: List[Dict], tools: List) -> Dict[str, Any
# error
logging.error(f"OpenAI API Error: {str(e)}")
raise ValueError(f"OpenAI API Error: {str(e)}")

def _azure_openai_completion(self, messages: List[Dict], tools: List) -> Dict[str, Any]:
# get the azure openai client
client = AzureOpenAI(
api_key=self.az_api_key,
azure_endpoint=self.az_endpoint,
api_version=self.az_api_version,
)

# make a request, passing in tools
response = client.chat.completions.create(
model=self.model,
messages=messages,
tools=tools or [],
)

# return the response
return {
"response": response.choices[0].message.content,
"tool_calls": getattr(response.choices[0].message, "tool_calls", []),
}

def _anthropic_completion(self, messages: List[Dict], tools: List) -> Dict[str, Any]:
"""Handle Anthropic chat completions."""
Expand Down