Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to parse OpenAI api response #212

Open
lucafirefox opened this issue Mar 11, 2024 · 2 comments
Open

Fail to parse OpenAI api response #212

lucafirefox opened this issue Mar 11, 2024 · 2 comments

Comments

@lucafirefox
Copy link
Contributor

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

@MaartenGr
Copy link
Owner

Thanks for sharing this. OpenAI migrated to a new major release a while ago, so it seems it still needs to be updated here. If you want, a PR would be highly appreciated.

@lucafirefox
Copy link
Contributor Author

PR done, feel free to close the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants