I have a Pyscript page that searches a news archive and returns a list of news summaries. I want to then feed this list of summaries into gpt-4o and get back a brief summary of the material. The problem I’m encountering is that my request keeps timing out before anything can happen. Using the exact same request in a Jupyter notebook produced the desired results, so I’m thinking the problem might have something to do with Pyscript. After reading OpenAI forums, i tried setting timeout, but this had no effect. I suspect that OpenAI’s 600ms default timeout is being applied but I don’t know how to change this when importing the package. Notably, importing openai to my page required also importing ssl, and I think pyscript imports openssl, so maybe there’s a conflict? If you can see what I’m doing wrong or if there’s a better way to do my OpenAI request in Pyscript, I’d love to hear about it. Here’s the function doing the request:
@when('click', '#summary-btn')
async def send(event=None):
Element('summarybrief').write("Send function successfully called.")
userElement = js.document.getElementById('brief')
OPENAI_API_KEY = userElement.value.strip()
client = OpenAI(api_key=OPENAI_API_KEY, timeout=60.0, max_retries=3)
# Convert similar_items to string format
global similar_items
similar_items_str = ""
for index, row in similar_items.iterrows():
similar_items_str += f"Title: {row['Title']} Summary: {row['Summary']} Note: {row['Note']}"
payload = f"Using br html tags instead of /n for line breaks, please generate a 500 word narrative summary of the following information: '''{similar_items_str}'''"
#confirm payload is correctly formed
#Element('summarybrief').element.innerHTML = payload
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": payload }
],
model="gpt-4o",
)
# Log the request data for debugging
console.log(f"Data: {messages}")
# Display the response in the summarybrief div
Element('summarybrief').element.innerHTML = chat_completion
# Debugging log
console.log(reply)
I tried making an OpenAI API request from a Pyscript page. I was expecting the request to work as it did in a Jupyter notebook. Instead, the request keeps failing due to timeout.