Skip to content

Commit 83d4199

Browse files
committed
Add max_tokens to user settings.
- Users can now add the maximum number of completion tokens via the Binary Ninja settings. - No longer substract the prompt tokens from the completion tokens. We leave this up to the user to decide. Otherwise, it's too confusing. Subtracting leads to dynamically changing token count. This could cause the model to work for one function and not the other without the user realizing why. I think OpenAI's error handling is sufficiently descriptive to allow the user to modify this parameter as they see fit. Implement #16.
1 parent ef08273 commit 83d4199

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/agent.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,16 @@ def get_model(self) -> str:
105105
assert self.is_valid_model('text-davinci-003')
106106
return 'text-davinci-003'
107107

108-
def max_token_count(self, model: str) -> int:
109-
'''Returns the maximum number of tokens that can be generated by the
110-
model. Returns a default of 2,048 if the model is not found. '''
111-
# TODO: This should be somewhere else, as it's also shared by Settings.
112-
models: dict[str, int] = {
113-
'text-davinci-003': 4_000,
114-
'text-curie-001': 2_048,
115-
'text-babbage-001': 2_048,
116-
'text-ada-001': 2_048,
117-
'code-davinci-002': 8_000,
118-
'code-cushman-001': 2_048
119-
}
120-
return models.get(model, 2_048)
108+
def get_token_count(self) -> int:
109+
'''Returns the maximum token count specified by the user. If no value is
110+
set, for whatever reason, returns 1,024.'''
111+
settings: Settings = Settings()
112+
# Check that the key exists.
113+
if settings.contains('openai.max_tokens'):
114+
# Check that the value is not None.
115+
if (max_tokens := settings.get_integer('openai.max_tokens')) is not None:
116+
return max_tokens
117+
return 1_024
121118

122119
def instruction_list(self, function: Union[LowLevelILFunction,
123120
MediumLevelILFunction,
@@ -150,6 +147,6 @@ def send_query(self, query: str) -> str:
150147
response: str = openai.Completion.create(
151148
model=self.model,
152149
prompt=query,
153-
max_tokens=self.max_token_count(self.model) - len(query),
150+
max_tokens=self.get_token_count(),
154151
)
155152
return response.choices[0].text

src/settings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def __init__(self) -> None:
2222
raise RegisterSettingsKeyException('Failed to register OpenAI '
2323
'model settings.')
2424

25+
# Register the setting for the max tokens used for both the prompt and
26+
# completion.
27+
if not self.register_max_tokens():
28+
raise RegisterSettingsKeyException('Failed to register OpenAI '
29+
'max tokens settings.')
30+
2531
def register_api_key_settings(self) -> bool:
2632
'''Register the OpenAI API key settings in Binary Ninja.'''
2733
# Set the attributes of the settings. Refer to:
@@ -64,3 +70,22 @@ def register_model_settings(self) -> bool:
6470
'default': 'text-davinci-003'
6571
}
6672
return self.register_setting('openai.model', json.dumps(properties))
73+
74+
def register_max_tokens(self) -> bool:
75+
'''Register the OpenAI max tokens used for both the prompt and
76+
completion. Defaults to 2,048. The Davinci model can use 4,000 or 8,000
77+
tokens for GPT and Codex respectively. Check out the documentation here:
78+
https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
79+
'''
80+
81+
properties = {
82+
'title': 'OpenAI Max Completion Tokens',
83+
'type': 'number',
84+
'description': 'The maximum number of tokens used for completion. Tokens do not necessarily align with word or instruction count. Typically, each token is four characters. If your function is very large, you may need to decrease this value, as the number of tokens in your prompt counts against the total number of tokens supported by the model. Not all models support the same number of maximum tokens; most support 2,048 tokens. For larger functions, check out text-davinci-003 and code-davinci-002 which support 4,000 and 8,000 respectively.',
85+
'default': 1_024,
86+
'minValue': 1,
87+
'maxValue': 8_000,
88+
'message': "Min: 1, Max: 8,000"
89+
}
90+
return self.register_setting('openai.max_tokens',
91+
json.dumps(properties))

0 commit comments

Comments
 (0)