MaartenGr / MaartenGr/BERTopic
Read Timeout Error while Using BertTopic with OpenAI API
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.8k
- Forks
- 920
- Avg merge
- 22h 24m
- Merged PRs (30d)
- 5
Description
## Description
I am encountering a timeout issue when attempting to use the OpenAI API in conjunction with BertTopic. After initiating a request, it fails with a Read Timeout error after 600 seconds.
## Error Details
```
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600)
at .send (/opt/conda/lib/python3.10/site-packages/requests/adapters.py:532)
at .send (/opt/conda/lib/python3.10/site-packages/requests/sessions.py:703)
at .request (/opt/conda/lib/python3.10/site-packages/requests/sessions.py:589)
```
## Steps to Reproduce
Set up BertTopic and make sure it's configured properly.
Initiate a request to OpenAI API.
Wait for the process to continue or fail.
```
from fastapi import FastAPI, HTTPException, Request
import numpy as np
import openai
from bertopic import BERTopic
from bertopic.representation import OpenAI
import time
from hdbscan import HDBSCAN
import os
app = FastAPI()
# Set your OpenAI API key securely
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("Please set the OPENAI_API_KEY environment variable.")
summarization_prompt = """
I have a topic that contains the following documents:
[DOCUMENTS]
The topic is described by the following keywords: [KEYWORDS]
Based on the information above, extract a short topic label in the following format:
topic:
"""
representation_model = OpenAI(model="gpt-3.5-turbo", prompt=summarization_prompt, nr_docs=3, chat=True)
@app.post("/topic_clustering")
async def topic_clustering_post(request: Request):
start_time = time.time() # Start timing
content = await request.json()
if 'topic_messages' in content and 'embeddings' in content:
topic_messages = content['topic_messages']
embeddings = content['embeddings']
if not isinstance(topic_messages, list) or not isinstance(embeddings, list):
raise HTTPException(status_code=400, detail="Both 'topic_messages' and 'embeddings' must be lists.")
if len(topic_messages) != len(embeddings):
raise HTTPException(status_code=400, detail="'topic_messages' and 'embeddings' must have the same length.")
embeddings = np.asarray(embeddings)
hdbscan_model = HDBSCAN(min_cluster_size=3, min_samples=1, metric='euclidean', cluster_selection_method='eom', prediction_data=True)
topic_model = BERTopic(representation_model=representation_model, hdbscan_model=hdbscan_model)
try:
topic_model.fit(topic_messages, embeddings)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
document_df = topic_model.get_document_info(topic_messages)
document_df_dict = document_df.to_dict(orient="records")
topic_df = topic_model.get_topic_info()
topic_df_dict = topic_df.to_dict(orient="records")
response = {
"document": document_df_dict,
"topic": topic_df_dict
}
if len(topic_df) > 2:
hierarchical_topics = topic_model.hierarchical_topics(topic_messages)
hierarchical_topics_dict = hierarchical_topics.to_dict(orient="records")
response["tree"] = hierarchical_topics_dict
else:
response["tree"] = None
end_time = time.time() # End timing
execution_time = end_time - start_time # Calculate execution time
response['execution_time'] = execution_time # Add execution time to the response
return response
else:
raise HTTPException(status_code=400, detail="JSON must include 'topic_messages' and 'embeddings' fields.")
```
## Expected Behavior
The request to the OpenAI API should be processed and return results without timing out. If an timeout does occur there are retrying options.
## Actual Behavior
The request fails after 600 seconds with a Read Timeout error.
## Environment
Python version: 3.10
BertTopic version: 0.15.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file or test is named. Start by reproducing the timeout through the OpenAI representation_model during topic_model.fit with the supplied FastAPI example, then trace the request path to determine whether the project can handle or report the timeout; done means the cause and an actionable timeout or retry behavior are covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, python
- Domain
- api, machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100