I create my ChatBot with Llama-Index and Streamlit and every time I run my program the following error is thrown:
Strangely enough, it works when I run the same code on another laptop.
I would be delighted if anyone who has already had experience or has a suggested solution could share it with me.
This is my code:
import streamlit as st
from llama_index import VectorStoreIndex, ServiceContext, Document, SimpleDirectoryReader
import openai
from llama_index.llms import OpenAI
import os
openai.api_key = "mein api_key"
# Set page title
st.header("❓ Natural Chatbot ❓")
if "messages" not in st.session_state.keys():
st.session_state.messages = [
{"role": "assistant", "content": "Hello, ask me questions about Natural. How can I help you?"}
]
@st.cache_resource(show_spinner=False)
def load_data():
with st.spinner(text="Loading data..."):
reader = SimpleDirectoryReader(input_dir="./data", recursive=True)
docs = reader.load_data()
service_context = ServiceContext.from_defaults(llm=OpenAI(model="gpt-3.5-turbo", temperature=0.1, system_prompt="You are an expert on the programming language Natural and your job is to answer technical questions. Assume that all questions are related to Natural. Keep your answers technical and based on facts - do not hallucinate features."))
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
return index
index = load_data()
print("Index loaded")
index.storage_context.persist(persist_dir="./storage/")
chat_engine = index.as_chat_engine(chat_mode="context", verbose=True)
if prompt := st.chat_input("You"):
st.session_state.messages.append({"role": "user", "content": prompt})
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# If last message is not from assistant, generate response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner(text="Generating response..."):
response = chat_engine.chat(prompt)
st.write(response.response)
message = {"role": "assistant", "content": response.response}
st.session_state.messages.append(message) # add response to message history