Skip to content
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

Extending to Azure OpenAI implementation #1470

Open
wants to merge 5 commits 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
5 changes: 3 additions & 2 deletions evals/completion_fns/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from evals.api import CompletionFn, CompletionResult
from evals.base import CompletionFnSpec
from evals.openai_client.openai_client_provider import OpenAIClientProvider
from evals.prompt.base import (
ChatCompletionPrompt,
CompletionPrompt,
Expand Down Expand Up @@ -116,7 +117,7 @@ def __call__(
openai_create_prompt: OpenAICreatePrompt = prompt.to_formatted_prompt()

result = openai_completion_create_retrying(
OpenAI(api_key=self.api_key, base_url=self.api_base),
OpenAIClientProvider(api_key=self.api_key).get_client(),
model=self.model,
prompt=openai_create_prompt,
**{**kwargs, **self.extra_options},
Expand Down Expand Up @@ -166,7 +167,7 @@ def __call__(
openai_create_prompt: OpenAICreateChatPrompt = prompt.to_formatted_prompt()

result = openai_chat_completion_create_retrying(
OpenAI(api_key=self.api_key, base_url=self.api_base),
OpenAIClientProvider(api_key=self.api_key).get_client(),
model=self.model,
messages=openai_create_prompt,
**{**kwargs, **self.extra_options},
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions evals/openai_client/openai_client_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
from abc import ABC

from openai import OpenAI
from openai.lib.azure import AzureOpenAI


class OpenAIClientProvider(ABC):
def __init__(self, api_key, **kwargs):
self.api_key = api_key or os.environ.get("OPENAI_API_KEY")
self.azure_endpoint = kwargs.pop("azure_endpoint", None) or os.environ.get(
"OPENAI_AZURE_ENDPOINT"
)
self.api_version = kwargs.pop("opena_api_version", None) or os.environ.get(
"OPENAI_API_VERSION"
)

self.kwargs = kwargs

def get_client(self):
if not self.azure_endpoint:
return OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
return AzureOpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
azure_endpoint=os.environ.get("OPENAI_AZURE_ENDPOINT"),
api_version=os.environ.get("OPENAI_API_VERSION"),
azure_deployment=os.environ.get("OPENAI_AZURE_DEPLOYMENT"),
)
5 changes: 3 additions & 2 deletions evals/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@

import openai
import yaml
from openai import OpenAI


from evals import OpenAIChatCompletionFn, OpenAICompletionFn
from evals.api import CompletionFn, DummyCompletionFn
from evals.base import BaseEvalSpec, CompletionFnSpec, EvalSetSpec, EvalSpec
from evals.elsuite.modelgraded.base import ModelGradedSpec
from evals.openai_client.openai_client_provider import OpenAIClientProvider
from evals.utils.misc import make_object

client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
client = OpenAIClientProvider(api_key=os.environ.get("OPENAI_API_KEY")).get_client()

logger = logging.getLogger(__name__)

Expand Down