Skip to content

Commit

Permalink
Support for OpenAI >= 1 (#189)
Browse files Browse the repository at this point in the history
* Now only support for openai >= 1.0
* Use the `client` parameter to access the API (see docstrings)
  • Loading branch information
MaartenGr authored Dec 13, 2023
1 parent f21062a commit 445e246
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 40 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand All @@ -265,8 +265,8 @@ model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(MY_DOCUMENTS, convert_to_tensor=True)

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down
20 changes: 20 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ hide:
- navigation
---

## **Version 0.8.3**
*Release date: 29 November, 2023*

* Fix support for openai>=1

You can now use it as follows:

```python
import openai
from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
```

## **Version 0.8.2**
*Release date: 29 September, 2023*

Expand Down
22 changes: 10 additions & 12 deletions docs/guides/keyllm.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down Expand Up @@ -85,8 +85,6 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
openai.api_key = "sk-..."

prompt = """
I have the following document:
[DOCUMENT]
Expand All @@ -96,7 +94,8 @@ Make sure to only extract keywords that appear in the text.
Use the following format separated by commas:
<keywords>
"""
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down Expand Up @@ -129,8 +128,6 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
openai.api_key = "sk-..."

prompt = """
I have the following document:
[DOCUMENT]
Expand All @@ -143,7 +140,8 @@ Based on the information above, improve the candidate keywords to best describe
Use the following format separated by commas:
<keywords>
"""
llm = OpenAI(model="gpt-3.5-turbo", prompt=prompt, chat=True)
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client,model="gpt-3.5-turbo", prompt=prompt, chat=True)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down Expand Up @@ -199,8 +197,8 @@ model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(documents, convert_to_tensor=True)

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down Expand Up @@ -245,8 +243,8 @@ from keybert.llm import OpenAI
from keybert import KeyLLM, KeyBERT

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyBERT(llm=llm)
Expand Down
8 changes: 4 additions & 4 deletions docs/guides/llms.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your OpenAI LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand All @@ -37,8 +37,8 @@ from keybert.llm import OpenAI
from keybert import KeyLLM

# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI(model="gpt-3.5-turbo", chat=True)
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client, model="gpt-3.5-turbo", chat=True)

# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down
4 changes: 2 additions & 2 deletions keybert/_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def extract_keywords(
from keybert import KeyLLM
# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)
# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand Down
37 changes: 19 additions & 18 deletions keybert/llm/_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OpenAI(BaseLLM):
keywords are comma-separated.
Arguments:
client: A `openai.OpenAI` client
model: Model to use within OpenAI, defaults to `"text-ada-001"`.
NOTE: If a `gpt-3.5-turbo` model is used, make sure to set
`chat` to True.
Expand Down Expand Up @@ -85,8 +86,8 @@ class OpenAI(BaseLLM):
from keybert import KeyLLM
# Create your LLM
openai.api_key = "sk-..."
llm = OpenAI()
client = openai.OpenAI(api_key=MY_API_KEY)
llm = OpenAI(client)
# Load it in KeyLLM
kw_model = KeyLLM(llm)
Expand All @@ -100,16 +101,17 @@ class OpenAI(BaseLLM):
```python
prompt = "I have the following document: [DOCUMENT] \nThis document contains the following keywords separated by commas: '"
llm = OpenAI(prompt=prompt, delay_in_seconds=5)
llm = OpenAI(client, prompt=prompt, delay_in_seconds=5)
```
If you want to use OpenAI's ChatGPT model:
```python
llm = OpenAI(model="gpt-3.5-turbo", delay_in_seconds=10, chat=True)
llm = OpenAI(client, model="gpt-3.5-turbo", delay_in_seconds=10, chat=True)
```
"""
def __init__(self,
client,
model: str = "gpt-3.5-turbo-instruct",
prompt: str = None,
generator_kwargs: Mapping[str, Any] = {},
Expand All @@ -118,6 +120,7 @@ def __init__(self,
chat: bool = False,
verbose: bool = False
):
self.client = client
self.model = model

if prompt is None:
Expand Down Expand Up @@ -172,39 +175,37 @@ def extract_keywords(self, documents: List[str], candidate_keywords: List[List[s
]
kwargs = {"model": self.model, "messages": messages, **self.generator_kwargs}
if self.exponential_backoff:
response = chat_completions_with_backoff(**kwargs)
response = chat_completions_with_backoff(self.client, **kwargs)
else:
response = openai.ChatCompletion.create(**kwargs)
keywords = response["choices"][0]["message"]["content"].strip()
response = self.client.chat.completions.create(**kwargs)
keywords = response.choices[0].message.content.strip()

# Use a non-chat model
else:
if self.exponential_backoff:
response = completions_with_backoff(model=self.model, prompt=prompt, **self.generator_kwargs)
response = completions_with_backoff(self.client, model=self.model, prompt=prompt, **self.generator_kwargs)
else:
response = openai.Completion.create(model=self.model, prompt=prompt, **self.generator_kwargs)
keywords = response["choices"][0]["text"].strip()
response = self.client.completions.create(model=self.model, prompt=prompt, **self.generator_kwargs)
keywords = response.choices[0].message.content.strip()
keywords = [keyword.strip() for keyword in keywords.split(",")]
all_keywords.append(keywords)

return all_keywords


def completions_with_backoff(**kwargs):
def completions_with_backoff(client, **kwargs):
return retry_with_exponential_backoff(
openai.Completion.create,
client.completions.create,
errors=(
openai.error.RateLimitError,
openai.error.ServiceUnavailableError,
openai.RateLimitError,
),
)(**kwargs)


def chat_completions_with_backoff(**kwargs):
def chat_completions_with_backoff(client, **kwargs):
return retry_with_exponential_backoff(
openai.ChatCompletion.create,
client.chat.completions.create,
errors=(
openai.error.RateLimitError,
openai.error.ServiceUnavailableError,
openai.RateLimitError,
),
)(**kwargs)

0 comments on commit 445e246

Please sign in to comment.