OpenAI
¶
Bases: BaseLLM
Using the OpenAI API to extract keywords.
The default method is openai.Completion
if chat=False
.
The prompts will also need to follow a completion task. If you
are looking for a more interactive chats, use chat=True
with model=gpt-3.5-turbo
.
For an overview see: https://platform.openai.com/docs/models
NOTE: The resulting keywords are expected to be separated by commas so any changes to the prompt will have to make sure that the resulting keywords are comma-separated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
A |
required | |
model
|
str
|
Model to use within OpenAI, defaults to |
'gpt-3.5-turbo-instruct'
|
generator_kwargs
|
Mapping[str, Any]
|
Kwargs passed to |
{}
|
prompt
|
str
|
The prompt to be used in the model. If no prompt is given,
|
None
|
system_prompt
|
str
|
The message that sets the behavior of the assistant. It's typically used to provide high-level instructions for the conversation. |
'You are a helpful assistant.'
|
delay_in_seconds
|
float
|
The delay in seconds between consecutive prompts in order to prevent RateLimitErrors. |
None
|
exponential_backoff
|
bool
|
Retry requests with a random exponential backoff.
A short sleep is used when a rate limit error is hit,
then the requests is retried. Increase the sleep length
if errors are hit until 10 unsuccesfull requests.
If True, overrides |
False
|
chat
|
bool
|
Set this to True if a chat model is used. Generally, this GPT 3.5 or higher See: https://platform.openai.com/docs/models/gpt-3-5 |
False
|
verbose
|
bool
|
Set this to True if you want to see a progress bar for the keyword extraction. |
False
|
Usage:
To use this, you will need to install the openai package first:
pip install openai
Then, get yourself an API key and use OpenAI's API as follows:
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)
# Extract keywords
document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine."
keywords = kw_model.extract_keywords(document)
You can also use a custom prompt:
prompt = "I have the following document: [DOCUMENT] \nThis document contains the following keywords separated by commas: '"
llm = OpenAI(client, prompt=prompt, delay_in_seconds=5)
If you want to use OpenAI's ChatGPT model:
llm = OpenAI(client, model="gpt-3.5-turbo", delay_in_seconds=10, chat=True)
Source code in keybert\llm\_openai.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
extract_keywords(documents, candidate_keywords=None)
¶
Extract topics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
documents
|
List[str]
|
The documents to extract keywords from |
required |
candidate_keywords
|
List[List[str]]
|
A list of candidate keywords that the LLM will fine-tune For example, it will create a nicer representation of the candidate keywords, remove redundant keywords, or shorten them depending on the input prompt. |
None
|
Returns:
Name | Type | Description |
---|---|---|
all_keywords |
All keywords for each document |
Source code in keybert\llm\_openai.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|