Skip to content

Commit 218a681

Browse files
committed
Add keys to the OpenAI settings.
Relate #13, #14.
1 parent 53080f5 commit 218a681

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from binaryninja import PluginCommand
2+
from . src.settings import OpenAISettings
23
from . src.entry import check_function
34

5+
# Register the settings group in Binary Ninja to store the API key and model.
6+
OpenAISettings()
7+
48
PluginCommand.register_for_high_level_il_function("OpenAI\What Does this Function Do (HLIL)?",
59
"Checks OpenAI to see what this HLIL function does." \
610
"Requires an internet connection and an API key "

src/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
class InvalidEngineException(OpenAIError):
44
pass
5+
6+
class RegisterSettingsGroupException(Exception):
7+
pass
8+
9+
class RegisterSettingsKeyException(Exception):
10+
pass

src/settings.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import json
2+
from binaryninja.settings import Settings
3+
from . exceptions import RegisterSettingsGroupException, \
4+
RegisterSettingsKeyException
5+
6+
class OpenAISettings(Settings):
7+
8+
def __init__(self) -> None:
9+
# Initialize the settings with the default instance ID.
10+
super().__init__(instance_id='default')
11+
# Register the OpenAI group.
12+
if not self.register_group('openai', 'OpenAI'):
13+
raise RegisterSettingsGroupException('Failed to register OpenAI '
14+
'settings group.')
15+
# Register the setting for the API key.
16+
if not self.register_api_key_settings():
17+
raise RegisterSettingsKeyException('Failed to register OpenAI API '
18+
'key settings.')
19+
20+
# Register the setting for the model used to query.
21+
if not self.register_model_settings():
22+
raise RegisterSettingsKeyException('Failed to register OpenAI '
23+
'model settings.')
24+
25+
def register_api_key_settings(self) -> bool:
26+
'''Register the OpenAI API key settings in Binary Ninja.'''
27+
# Set the attributes of the settings. Refer to:
28+
# https://api.binary.ninja/binaryninja.settings-module.html
29+
properties = {
30+
'title': 'OpenAI API Key',
31+
'type': 'string',
32+
'description': 'The user\'s OpenAI API key used to make requests '
33+
'the server.'
34+
}
35+
return self.register_setting('openai.api_key', json.dumps(properties))
36+
37+
def register_model_settings(self) -> bool:
38+
'''Register the OpenAI model settings in Binary Ninja.
39+
Defaults to text-davinci-003.
40+
'''
41+
# Set the attributes of the settings. Refer to:
42+
# https://api.binary.ninja/binaryninja.settings-module.html
43+
properties = {
44+
'title': 'OpenAI Model',
45+
'type': 'string',
46+
'description': 'The OpenAI model used to generate the response.',
47+
'default': 'text-davinci-003'
48+
}
49+
return self.register_setting('openai.model', json.dumps(properties))

0 commit comments

Comments
 (0)