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 a :max_tokens param in OpenAIValidator.validate_max_tokens #388

Merged
merged 2 commits into from
Nov 12, 2023

Conversation

bricolage
Copy link
Contributor

@bricolage bricolage commented Nov 12, 2023

Motivation

Two of OpenAI's API endpoints (chat and completions) take a :max_tokens param, but these are ignored in the current token validation / calculation method BaseValidator.validate_max_tokens (that method name is slightly misleading as it does more than validate it, but that's for another PR).

Changes

Fortunately the changes were minimal. BaseValidator.validate_max_tokens already takes an options param, and I just worked on this method in #379. Like in #379 we want to take the lower of two token limits. In this case it's between the max calculated from the leftover tokens and options[:max_tokens]. I added two specs for when it's passed in (one for when it's lower, one for when it's higher than the calculated max).

Addresses #365

@@ -69,7 +69,7 @@ def complete(prompt:, **params)
return legacy_complete(prompt, parameters) if is_legacy_model?(parameters[:model])

parameters[:messages] = compose_chat_messages(prompt: prompt)
parameters[:max_tokens] = validate_max_tokens(parameters[:messages], parameters[:model])
parameters[:max_tokens] = validate_max_tokens(parameters[:messages], parameters[:model], parameters[:max_tokens])
Copy link
Collaborator

Choose a reason for hiding this comment

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

max_tokens = [max_tokens, options[:max_tokens]].min

Do you think this ^^ logic should actually live inside of this method and not inside the base_validator or the openai_validator?

For example we could have something like this:

parameters[:max_tokens] = [
  parameters[:max_tokens],
  validate_max_tokens(parameters[:messages], parameters[:model])
].min

I guess what's missing with this approach is raising an error?

Copy link
Contributor Author

@bricolage bricolage Nov 12, 2023

Choose a reason for hiding this comment

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

@andreibondarev oh yeah that's leaky, it goes by other key names in other LLMs. My vote is let the validator subclasses handle it (e.g. [options[:max_tokens], super(...)].min). If we did it right in the LLM classes, any individual method calls that use it would lead to some repetition. e.g. OpenAI#chat and #complete would both have similar code. This way the other LLM classes can all pass through their own param names correctly. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreibondarev also what error condition did you have in mind? e.g. if :max_tokens is passed in as a non-number value? Or possibly as a negative value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreibondarev check out the latest commit. I moved it into the OpenAIValidator subclass.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@andreibondarev also what error condition did you have in mind? e.g. if :max_tokens is passed in as a non-number value? Or possibly as a negative value?

This is the error that I was referring to: https://github.com/andreibondarev/langchainrb/pull/388/files#diff-79c9e673e647cc360e11c2946b3a4def6a5dfe52480860dc9ccd2101906902a9R30

@bricolage bricolage changed the title Support a :max_tokens param in BaseValidator.validate_max_tokens Support a :max_tokens param in OpenAIValidator.validate_max_tokens Nov 12, 2023
@andreibondarev
Copy link
Collaborator

@bricolage Thank you!

@andreibondarev andreibondarev merged commit 0751f4a into patterns-ai-core:main Nov 12, 2023
5 checks passed
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