Skip to content

Commit

Permalink
Adding retries to OpenAI calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbit committed Oct 16, 2023
1 parent 8b2c289 commit bb1fdf4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions giskard/scanner/llm/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import openai
from tenacity import retry, stop_after_attempt, wait_exponential

from ...core.errors import GiskardInstallationError

Expand All @@ -8,13 +9,15 @@ class LLMImportError(GiskardInstallationError):
functionality = "LLM"


@retry(stop=stop_after_attempt(3), wait=wait_exponential())
def llm(messages, model="gpt-4", temperature=0.5, n=1, **kwargs):
completion = openai.ChatCompletion.create(model=model, messages=messages, temperature=temperature, n=n, **kwargs)
if n == 1:
return completion.choices[0].message.content
return [c.message.content for c in completion.choices]


@retry(stop=stop_after_attempt(3), wait=wait_exponential())
def llm_fn_call(messages, functions, model="gpt-4", temperature=0.5, n=1, **kwargs):
completion = openai.ChatCompletion.create(
model=model, messages=messages, functions=functions, temperature=temperature, n=n, **kwargs
Expand Down

0 comments on commit bb1fdf4

Please sign in to comment.