Skip to content

Commit

Permalink
ChatGPT Constructor: Cache results
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Aug 25, 2023
1 parent a2b5d9b commit 2f0ae74
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions orangecontrib/prototypes/widgets/owchatgptconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class OWChatGPTConstructor(OWWidget):
text_var = ContextSetting(None)
prompt_start = Setting("")
prompt_end = Setting("")
cache = Setting({})
auto_apply = Setting(True)

want_main_area = False
Expand Down Expand Up @@ -176,12 +177,19 @@ def _get_answers(self) -> str:
texts = self.__data.get_column(self.text_var)
answers = []
for text in texts:
try:
answer = run_gpt(self.access_key, MODELS[self.model_index],
text, self.prompt_start, self.prompt_end)
except Exception as ex:
answer = ex
self.Error.unknown_error(ex)
args = (text.strip(),
self.prompt_start.strip(),
self.prompt_end.strip())
if args in self.cache:
answer = self.cache[args]
else:
try:
answer = run_gpt(self.access_key, MODELS[self.model_index],
*args)
self.cache[args] = answer
except Exception as ex:
answer = ex
self.Error.unknown_error(ex)
answers.append(answer)
return answers

Expand Down

0 comments on commit 2f0ae74

Please sign in to comment.