Skip to content

Commit 136a0e1

Browse files
authored
Merge pull request #30 from WhatTheFuzz/feature/openai-updates
Feature/openai updates
2 parents 4d520c0 + e7be587 commit 136a0e1

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
"openai"
2828
]
2929
},
30-
"version": "2.0.1",
30+
"version": "3.0.1",
3131
"minimumbinaryninjaversion": 3200
3232
}

src/agent.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from pathlib import Path
66

77
import openai
8-
from openai.api_resources.model import Model
9-
from openai.error import APIError
8+
from openai import APIError
109

1110
from binaryninja.function import Function
1211
from binaryninja.lowlevelil import LowLevelILFunction
@@ -18,6 +17,7 @@
1817

1918
from . query import Query
2019
from . c import Pseudo_C
20+
from . exceptions import NoAPIKeyException
2121

2222

2323
class Agent:
@@ -45,7 +45,7 @@ def __init__(self,
4545
path_to_api_key: Optional[Path]=None) -> None:
4646

4747
# Read the API key from the environment variable.
48-
openai.api_key = self.read_api_key(path_to_api_key)
48+
self.client = openai.OpenAI(api_key=self.read_api_key(filename=path_to_api_key))
4949

5050
assert bv is not None, 'BinaryView is None. Check how you called this function.'
5151
# Set instance attributes.
@@ -87,12 +87,12 @@ def read_api_key(self, filename: Optional[Path]=None) -> str:
8787
except FileNotFoundError:
8888
log.log_error(f'Could not find API key file at {filename}.')
8989

90-
raise APIError('No API key found. Refer to the documentation to add the '
90+
raise NoAPIKeyException('No API key found. Refer to the documentation to add the '
9191
'API key.')
9292

9393
def is_valid_model(self, model: str) -> bool:
9494
'''Checks if the model is valid by querying the OpenAI API.'''
95-
models: list[Model] = openai.Model.list().data
95+
models: list = self.client.models.list().data
9696
return model in [m.id for m in models]
9797

9898
def get_model(self) -> str:
@@ -206,7 +206,8 @@ def rename_variable(self, response: str) -> None:
206206

207207
def send_query(self, query: str, callback: Optional[Callable]=None) -> None:
208208
'''Sends a query to the engine and prints the response.'''
209-
query = Query(query_string=query,
209+
query = Query(client=self.client,
210+
query_string=query,
210211
model=self.model,
211212
max_token_count=self.get_token_count(),
212213
callback_function=callback)

src/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
class NoAPIKeyException(Exception):
2+
pass
3+
14
class RegisterSettingsGroupException(Exception):
25
pass
36

src/query.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from __future__ import annotations
22
from collections.abc import Callable
33
from typing import Optional
4-
import openai
4+
from openai import Client
55
from binaryninja.plugin import BackgroundTaskThread
66
from binaryninja.log import log_debug, log_info
77

88
class Query(BackgroundTaskThread):
99

10-
def __init__(self, query_string: str, model: str,
10+
def __init__(self, client: Client, query_string: str, model: str,
1111
max_token_count: int, callback_function: Optional[Callable]=None) -> None:
1212
BackgroundTaskThread.__init__(self,
1313
initial_progress_text="",
1414
can_cancel=False)
15+
self.client: Client = client
1516
self.query_string: str = query_string
1617
self.model: str = model
1718
self.max_token_count: int = max_token_count
@@ -23,15 +24,15 @@ def run(self) -> None:
2324
log_debug(f'Sending query: {self.query_string}')
2425

2526
if self.model in ["gpt-3.5-turbo","gpt-4","gpt-4-32k"]:
26-
response = openai.ChatCompletion.create(
27+
response = self.client.chat.completions.create(
2728
model=self.model,
2829
messages=[{"role":"user","content":self.query_string}],
2930
max_tokens=self.max_token_count,
3031
)
3132
# Get the response text.
3233
result: str = response.choices[0].message.content
3334
else:
34-
response = openai.Completion.create(
35+
response = self.client.chat.completions.create(
3536
model=self.model,
3637
prompt=self.query_string,
3738
max_tokens=self.max_token_count,

0 commit comments

Comments
 (0)