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

Remove global OpenAI client initialization #1539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

michaelAlvarino
Copy link
Contributor

This PR removes OpenAI initialization calls that happen at import time so developers who want to use Evals as a library rather than a CLI tool, without the OPENAI_API_KEY environment variable specified, can do so.

Importing Evals libraries from python for use as a library rather
than a CLI tool fails if the OPENAI_API_KEY is not specified.
Normally this wouldn't be a problem, if we were evaluating an OpenAI
model, however there are cases where we may not be evaluating an
OpenAI model. In those cases we may not set the OPENAI_API_KEY in
advance.

In addition, this PR makes changes to some unit tests either to
pass, or skip those tests when the necessary API keys are not in
the environment.
@@ -72,4 +69,4 @@ def model_output_empty_tags(message: str) -> bool:


def openai_chatcompletion_create(*args, **kwargs):
return client.chat.completions.create(*args, **kwargs)
return OpenAI(api_key=os.environ.get("OPENAI_API_KEY")).chat.completions.create(*args, **kwargs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of creating the client fresh on every call of this function, can we pass it in from the caller? Ideally it would be instantiated in an __init__ in a class like the changes you made in other files

@@ -263,14 +263,6 @@ def record_final_report(self, final_report: Any):
logging.info(f"Final report: {final_report}. Not writing anywhere.")


def _green(str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume we don't want these deletions in this file?

@@ -103,14 +103,18 @@ def is_chat_model(model_name: str) -> bool:
class Registry:
def __init__(self, registry_paths: Sequence[Union[str, Path]] = DEFAULT_PATHS):
self._registry_paths = [Path(p) if isinstance(p, str) else p for p in registry_paths]
try:
self.client = OpenAI()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))?

@@ -172,6 +170,7 @@ def get_embeddings(
A list of Embedding namedtuples where each Embedding
represents the input string and its corresponding vector.
"""
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to an __init__ like in the GPTValidator?

@@ -2,7 +2,7 @@

import anthropic
from anthropic import Anthropic
from anthropic.types import ContentBlock, MessageParam, Usage
from anthropic.types import TextBlock, MessageParam, Usage
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unrelated to this PR, maybe let's pull it into a different one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants