MaartenGr / MaartenGr/KeyBERT

Fail to parse OpenAI api response

Open
#212 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
385
PR merge metrics
No merged PRs in 30d

Description

I'm using keybert to run some topic modeling tasks and it's very powerful. However, I did have to make some changes to the source code to make the code working flawlessly. Here is my code:

```
import openai
from keybert.llm import OpenAI
from keybert import KeyLLM, KeyBERT

# Create your LLM
client = openai.OpenAI(api_key="sk-XXXXXXXXX")
llm = OpenAI(client, prompt=CUSTOM_PROMPT)

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

# Extract keywords
keywords = kw_model.extract_keywords(documents)
```

which throws the following error:

`AttributeError: 'CompletionChoice' object has no attribute 'message'`

Here below a snippet of the traceback:

Screenshot 2024-03-11 at 16 19 43

I believe OpenAI response changed over time and my working work-around is to replace:

`keywords = response.choices[0].message.content.strip()`

with

`keywords = response.choices[0].text.strip()`

in file keybert/llm/_openai.py at lines `181` and `189`

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading keybert/llm/_openai.py around lines 181 and 189, then reproduce the reported Python call with the OpenAI client and inspect the response choice type. The issue is done when the OpenAI response is parsed without the reported AttributeError and keyword extraction completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.