Skip to content

Commit

Permalink
Add openai API retries (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: nik <nik@heartex.net>
  • Loading branch information
niklub and nik committed Nov 29, 2023
1 parent b6fd1ce commit 05e5ba4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
10 changes: 7 additions & 3 deletions adala/runtimes/_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def check_if_new_openai_version():
from adala.utils.logs import print_error
from adala.utils.internal_data import InternalDataFrame
from adala.utils.parse import parse_template, partial_str_format
from tenacity import retry, stop_after_attempt, wait_random


@retry(wait=wait_random(min=5, max=10), stop=stop_after_attempt(6))
def chat_completion_call(model, messages):
return openai.ChatCompletion.create(model=model, messages=messages)


class OpenAIChatRuntime(Runtime):
Expand Down Expand Up @@ -92,9 +98,7 @@ def execute(self, messages: List):
completion_text = completion.choices[0].message.content
else:
# deprecated
completion = openai.ChatCompletion.create(
model=self.openai_model, messages=messages
)
completion = chat_completion_call(self.openai_model, messages)
completion_text = completion.choices[0]["message"]["content"]
return completion_text

Expand Down
2 changes: 1 addition & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"aiosqlite>=0.19.0",
"ipython>=8.12.3",
"chromadb>=0.4.18",
"tenacity>=8.2.3",
]
requires-python = ">=3.8.8,<3.12"
readme = "README.md"
Expand Down

0 comments on commit 05e5ba4

Please sign in to comment.