Skip to content

Commit

Permalink
openai v1 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesturk committed Nov 24, 2023
1 parent fe8249d commit 83499ef
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ scrapeghost = 'scrapeghost.cli:main'

[tool.poetry.dependencies]
python = "^3.11"
openai = "^0.27.1"
openai = "^1.3"
cssselect = "^1.2.0"
lxml = "^4.9.2"
structlog = ">22.3,<24.0"
Expand Down
13 changes: 7 additions & 6 deletions src/scrapeghost/apicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import time
from dataclasses import dataclass
import openai
from openai import OpenAI

client = OpenAI()
import openai.error
from typing import Callable

Expand All @@ -23,7 +26,7 @@


RETRY_ERRORS = (
openai.error.RateLimitError,
openai.RateLimitError,
openai.error.Timeout,
openai.error.APIConnectionError,
)
Expand Down Expand Up @@ -92,11 +95,9 @@ def _raw_api_request(
f"Total cost {self.total_cost:.2f} exceeds max cost {self.max_cost:.2f}"
)
start_t = time.time()
completion = openai.ChatCompletion.create(
model=model,
messages=messages,
**self.model_params,
)
completion = client.chat.completions.create(model=model,
messages=messages,
**self.model_params)
elapsed = time.time() - start_t
p_tokens = completion.usage.prompt_tokens
c_tokens = completion.usage.completion_tokens
Expand Down
4 changes: 2 additions & 2 deletions tests/test_apicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _timeout_once(**kwargs):
if _timeout_once.called:
return _mock_response()
_timeout_once.called = True
raise openai.error.Timeout()
raise openai.Timeout()

_timeout_once.called = False

Expand All @@ -93,7 +93,7 @@ def test_retry_failure():
retry=RetryRule(2, 0), # disable wait
)

with pytest.raises(openai.error.Timeout):
with pytest.raises(openai.Timeout):
with patch_create() as create:
# fail first request
create.side_effect = _timeout
Expand Down
2 changes: 1 addition & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _mock_response(**kwargs):


def _timeout(**kwargs):
raise openai.error.Timeout()
raise openai.Timeout()


def patch_create():
Expand Down

0 comments on commit 83499ef

Please sign in to comment.