MaartenGr / BERTopic

Leveraging BERT and c-TF-IDF to create easily interpretable topics.
https://maartengr.github.io/BERTopic/
MIT License
5.97k stars 746 forks source link

Hitting rate limit when using hierarchical_topics() #1337

Closed mattwalter-cv closed 1 year ago

mattwalter-cv commented 1 year ago

I am working with a dataset of about 12,000 social media messages clustered into 100 topics using KMEANS (or Agglomorative) algorithm. I'm using openAI as a representative model which worked great for generating labels on the initial clustering. But when I try to generate hierarchical_topics, I hit the openAI rate limit. Is it possible to implement a delay on the 'hierarchical_topics' method or provide another work around?

CODE

....
representation_model = OpenAI(model="text-davinci-003", delay_in_seconds=20, chat=False)
....
hierarchical_topics = topic_model.hierarchical_topics(cleaned_messages)
topic_model.visualize_hierarchy(hierarchical_topics=hierarchical_topics, width=1000, height=10000) #,top_n_topics=50

ERROR

RateLimitError Traceback (most recent call last)

in () ----> 1 hierarchical_topics = topic_model.hierarchical_topics(cleaned_messages) 2 topic_model.visualize_hierarchy(hierarchical_topics=hierarchical_topics, width=1000, height=10000) #,top_n_topics=50 7 frames /usr/local/lib/python3.10/dist-packages/bertopic/_bertopic.py in hierarchical_topics(self, docs, linkage_function, distance_function) 986 selection = documents.loc[documents.Topic.isin(clustered_topics), :] 987 selection.Topic = 0 --> 988 words_per_topic = self._extract_words_per_topic(words, selection, c_tf_idf, calculate_aspects=False) 989 990 # Extract parent's name and ID /usr/local/lib/python3.10/dist-packages/bertopic/_bertopic.py in _extract_words_per_topic(self, words, documents, c_tf_idf, calculate_aspects) 3568 topics = tuner.extract_topics(self, documents, c_tf_idf, topics) 3569 elif isinstance(self.representation_model, BaseRepresentation): -> 3570 topics = self.representation_model.extract_topics(self, documents, c_tf_idf, topics) 3571 elif isinstance(self.representation_model, dict): 3572 if self.representation_model.get("Main"): /usr/local/lib/python3.10/dist-packages/bertopic/representation/_openai.py in extract_topics(self, topic_model, documents, c_tf_idf, topics) 194 response = completions_with_backoff(model=self.model, prompt=prompt, **self.generator_kwargs) 195 else: --> 196 response = openai.Completion.create(model=self.model, prompt=prompt, **self.generator_kwargs) 197 label = response["choices"][0]["text"].strip() 198 /usr/local/lib/python3.10/dist-packages/openai/api_resources/completion.py in create(cls, *args, **kwargs) 23 while True: 24 try: ---> 25 return super().create(*args, **kwargs) 26 except TryAgain as e: 27 if timeout is not None and time.time() > start + timeout: /usr/local/lib/python3.10/dist-packages/openai/api_resources/abstract/engine_api_resource.py in create(cls, api_key, api_base, api_type, request_id, api_version, organization, **params) 151 ) 152 --> 153 response, _, api_key = requestor.request( 154 "post", 155 url, /usr/local/lib/python3.10/dist-packages/openai/api_requestor.py in request(self, method, url, params, headers, files, stream, request_id, request_timeout) 296 request_timeout=request_timeout, 297 ) --> 298 resp, got_stream = self._interpret_response(result, stream) 299 return resp, got_stream, self.api_key 300 /usr/local/lib/python3.10/dist-packages/openai/api_requestor.py in _interpret_response(self, result, stream) 698 else: 699 return ( --> 700 self._interpret_response_line( 701 result.content.decode("utf-8"), 702 result.status_code, /usr/local/lib/python3.10/dist-packages/openai/api_requestor.py in _interpret_response_line(self, rbody, rcode, rheaders, stream) 761 stream_error = stream and "error" in resp.data 762 if stream_error or not 200 <= rcode < 300: --> 763 raise self.handle_error_response( 764 rbody, rcode, resp.data, rheaders, stream_error=stream_error 765 ) RateLimitError: You exceeded your current quota, please check your plan and billing details.
mattwalter-cv commented 1 year ago

Upon further investigation, I hit quota hard limit!