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

Fix: Add rate limit to OpenAIAnswerGenerator, change token computation #3078

Closed
wants to merge 1 commit into from

Conversation

Timoeller
Copy link
Contributor

@Timoeller Timoeller commented Aug 19, 2022

Related Issues

Proposed Changes:

  1. Added a rate limit since otherwise the API will crash eventually.
  2. Added a try catch around the API call since OpenAIs API sometimes returns errors
  3. Changed prompt length computation, since one has to take the output length into account as well

How did you test it?

manual verification

Checklist

@Timoeller Timoeller requested a review from a team as a code owner August 19, 2022 18:49
@Timoeller Timoeller requested review from bogdankostic and removed request for a team August 19, 2022 18:49
presence_penalty: float = -2.0,
frequency_penalty: float = -2.0,
max_calls_per_minute: int = 60,
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we make the default here 3000? I'd prefer having here the "production" setting for a paid OpenAI account. For people on the free trial we can make the error message that you added further down more descriptive.

try:
response = requests.request("POST", url, headers=headers, data=json.dumps(payload))
time.sleep(60 / self.max_calls_per_minute)
except Exception as e:
Copy link
Member

Choose a reason for hiding this comment

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

Can we catch the exception type / API response code for rate-limits? If yes, we can make the error message here more actionable like "Seems like the OpenAI API rate-limited your requests. See limits of plans here (link) and consider setting max_calls_per_minute to a lower value (currently: XX) when initializing the OpenAIAnswerGenerator node in haystack`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, this try catch is was not meant for catching the rate limit.
But I see the value of catching the rate limit as you suggest.

I think there is the need to catch more general problems, since when I was running eval after adjusting the rate limit, the API also ran into errors from time to time. That's why here is a very general try-catch block.

Copy link
Contributor

@bogdankostic bogdankostic left a comment

Choose a reason for hiding this comment

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

I agree with @tholor's comments. Also, we need to fix the import order as requested by pylint and generate new schemas.

@@ -191,7 +209,7 @@ def _build_prompt(self, query: str, documents: List[Document]) -> Tuple[str, Lis
)

# Top ranked documents should go at the end
context = " ".join(reversed(input_docs_content))
context = "\n".join(reversed(input_docs_content))
Copy link
Contributor

Choose a reason for hiding this comment

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

We use " " here as this is what's used in the transition guide for transition from answers endpoint to completions endpoint (see here for the transition guide and here for the place where " " is used).

What was your motivation for changing this to "\n"? Anyway, I assume that this change won't affect the the way the answers are produced, as tokenization should treat both " " and "\n" the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My motivation was to make it more readable. With the newline you can see very quickly the different articles in the context.

@Timoeller
Copy link
Contributor Author

Sorry, I realize I won't be able to work on this the next week. I can either look into it afterward or you start taking over?

@Timoeller
Copy link
Contributor Author

Closing this in favor of #3398

@Timoeller Timoeller closed this Oct 21, 2022
@masci masci deleted the openai_fixes branch September 13, 2023 08:55
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.

running into rate limit with OpenAI's GPT-3 API when used during evaluation
3 participants