Skip to content

Commit b49b2ef

Browse files
committed
Implement query submission in background thread.
1 parent e771fd2 commit b49b2ef

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/agent.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from binaryninja.settings import Settings
1313
from binaryninja import log
1414

15+
from . query import Query
16+
1517

1618
class Agent:
1719

@@ -142,11 +144,9 @@ def generate_query(self, function: Union[LowLevelILFunction,
142144
prompt += '\n'.join(self.instruction_list(function))
143145
return prompt
144146

145-
def send_query(self, query: str) -> str:
146-
'''Sends a query to the engine and returns the response.'''
147-
response: str = openai.Completion.create(
148-
model=self.model,
149-
prompt=query,
150-
max_tokens=self.get_token_count(),
151-
)
152-
return response.choices[0].text
147+
def send_query(self, query: str) -> None:
148+
'''Sends a query to the engine and prints the response.'''
149+
query = Query(query_string=query,
150+
model=self.model,
151+
max_token_count=self.get_token_count())
152+
query.start()

src/query.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import openai
2+
from binaryninja.plugin import BackgroundTaskThread
3+
4+
5+
class Query(BackgroundTaskThread):
6+
7+
def __init__(self, query_string: str, model: str,
8+
max_token_count: int) -> None:
9+
BackgroundTaskThread.__init__(self,
10+
initial_progress_text="",
11+
can_cancel=False)
12+
self.query_string: str = query_string
13+
self.model: str = model
14+
self.max_token_count: int = max_token_count
15+
16+
def run(self) -> None:
17+
self.progress = "Submitting query to OpenAI."
18+
19+
response: str = openai.Completion.create(
20+
model=self.model,
21+
prompt=self.query_string,
22+
max_tokens=self.max_token_count,
23+
)
24+
# Notify the user.
25+
print(response.choices[0].text)

0 commit comments

Comments
 (0)