Skip to content

Commit 05e5ba4

Browse files
niklubnik
andauthored
Add openai API retries (#44)
Co-authored-by: nik <[email protected]>
1 parent b6fd1ce commit 05e5ba4

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

adala/runtimes/_openai.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ def check_if_new_openai_version():
2929
from adala.utils.logs import print_error
3030
from adala.utils.internal_data import InternalDataFrame
3131
from adala.utils.parse import parse_template, partial_str_format
32+
from tenacity import retry, stop_after_attempt, wait_random
33+
34+
35+
@retry(wait=wait_random(min=5, max=10), stop=stop_after_attempt(6))
36+
def chat_completion_call(model, messages):
37+
return openai.ChatCompletion.create(model=model, messages=messages)
3238

3339

3440
class OpenAIChatRuntime(Runtime):
@@ -92,9 +98,7 @@ def execute(self, messages: List):
9298
completion_text = completion.choices[0].message.content
9399
else:
94100
# deprecated
95-
completion = openai.ChatCompletion.create(
96-
model=self.openai_model, messages=messages
97-
)
101+
completion = chat_completion_call(self.openai_model, messages)
98102
completion_text = completion.choices[0]["message"]["content"]
99103
return completion_text
100104

pdm.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies = [
1616
"aiosqlite>=0.19.0",
1717
"ipython>=8.12.3",
1818
"chromadb>=0.4.18",
19+
"tenacity>=8.2.3",
1920
]
2021
requires-python = ">=3.8.8,<3.12"
2122
readme = "README.md"

0 commit comments

Comments
 (0)