My code seems to re-index all URLs with every question. This means that all questions take about 60-90sec to be answered, which is obviously way too much.
How can I update the code to only index once, thereby suffering this delay once? Ideally I’d like to index on a schedule, e.g. once per day.
import openai
import gradio as gr
import requests
from bs4 import BeautifulSoup
openai.api_key = "<our-API-key>"
def search_manual():
urls = [
<list of URLs>
]
all_headings = []
for url in urls:
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
headings = [heading.text.strip() for heading in soup.find_all("h2")]
all_headings.extend(headings)
return all_headings
def chatbot(input):
headings = search_manual()
messages = [
{"role": "system", "content": f"You are a helpful and kind AI Assistant. Here are some headings from the manual: {', '.join(headings)}"}
]
if input:
messages.append({"role": "user", "content": input})
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
return reply
inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
outputs = gr.outputs.Textbox(label="Reply")
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
description="Ask anything you want",
theme="compact").launch(share=True)