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

Support asynchronous API call of OpenAIBasedEvaluator.get_score() #104

Merged
merged 7 commits into from
Mar 28, 2024

Conversation

Alnusjaponica
Copy link
Contributor

Motivation

The bottleneck of the OpenAI-based evaluator is IO time. This PR introduces an asynchronous API call option for each metric evaluation.

@Alnusjaponica Alnusjaponica marked this pull request as ready for review March 26, 2024 08:57
@Alnusjaponica
Copy link
Contributor Author

Alnusjaponica commented Mar 26, 2024

I ran the following code in my local environment and get this result:

import time

from dotenv import load_dotenv
from langcheck.metrics.en import toxicity

load_dotenv()

generated_outputs = [
    "You look beautiful",
    "You look ugly",
    "You are a good person",
    "You are a bad person",
    "I have a pen",
    "I have an apple",
    "I have a pine apple",
    # "I have a gun", # Intentionally cause an error.
]

# Async
start = time.time()
toxicity_value = toxicity(
    generated_outputs,
    model_type="azure_openai",
    openai_args={"model": "gpt-4-turbo"},
    use_async=True,
)
print(f'Async call: {time.time() - start}[s]')

# No async
start = time.time()
toxicity_value = toxicity(
    generated_outputs,
    model_type="azure_openai",
    openai_args={"model": "gpt-4-turbo"},
    use_async=False,
)
print(f'No async call: {time.time() - start}[s]')
Async call: 27.088413953781128[s]
Intermediate assessments (1/2): 100%|████████████████| 7/7 [01:09<00:00,  9.96s/it]
Scores (2/2): 100%|█████████████████████████| 7/7 [00:17<00:00,  2.44s/it]
No async call: 86.82365489006042[s]

Copy link
Contributor

@liwii liwii left a comment

Choose a reason for hiding this comment

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

Nice! It worked well in my environment too!

Could you also edit the remaining metrics (e.g. Japanese toxicity) too?

@Alnusjaponica
Copy link
Contributor Author

Sure, I overlooked them. I updated the code.

@Alnusjaponica
Copy link
Contributor Author

@liwii I updated all the metric function which we can pass use_async options. PTAL.

Copy link
Contributor

@liwii liwii left a comment

Choose a reason for hiding this comment

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

I made a final check and added some missing use_async arguments. LGTM! Thanks for the work!!

The tests are failing due to the server that stores the Chinese tokenizers. Let me think about how to resolve that.

@liwii
Copy link
Contributor

liwii commented Mar 28, 2024

Seemingly the server is back, so let me merge this PR now. Thanks!!

@liwii liwii merged commit ccb33f3 into citadel-ai:main Mar 28, 2024
14 checks passed
@Alnusjaponica Alnusjaponica deleted the async-openai-api branch March 28, 2024 10:15
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.

None yet

2 participants