Spaces:
Build error
Build error
File size: 3,312 Bytes
08606a2 9040eb5 14bade9 9040eb5 08606a2 9040eb5 14bade9 9040eb5 75bfa09 14bade9 75bfa09 9040eb5 75bfa09 08606a2 75bfa09 08606a2 14bade9 08606a2 14bade9 08606a2 75bfa09 08606a2 14bade9 08606a2 14bade9 08606a2 14bade9 9040eb5 14bade9 9040eb5 14bade9 9040eb5 14bade9 9040eb5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import json
import os
import streamlit as st
from llama_index import ServiceContext
from llama_index import SimpleDirectoryReader
from llama_index import VectorStoreIndex
from llama_index import set_global_service_context
from llama_index.embeddings import OpenAIEmbedding
from llama_index.llms import AzureOpenAI
# Initialize message history
st.header("Chat with André's research 💬 📚")
if "messages" not in st.session_state.keys(): # Initialize the chat message history
st.session_state.messages = [{"role": "assistant", "content": "Ask me a question about André's research!"}]
# Load config values
with open(r"config.json") as config_file:
config_details = json.load(config_file)
@st.cache_resource(show_spinner=False)
def load_data():
with st.spinner(text="Loading and indexing the provided dataset – hang tight! This may take a few seconds."):
documents = SimpleDirectoryReader(input_dir="./data", recursive=True).load_data()
llm = AzureOpenAI(
model="gpt-3.5-turbo",
engine=config_details["ENGINE"],
temperature=0.5,
api_key=os.getenv("OPENAI_API_KEY"),
api_base=config_details["OPENAI_API_BASE"],
api_type="azure",
api_version=config_details["OPENAI_API_VERSION"],
system_prompt="You are an expert on André's research and your job is to answer"
"technical questions. Assume that all questions are related to"
"André's research. Keep your answers technical and based on facts"
" – do not hallucinate features.",
)
# You need to deploy your own embedding model as well as your own chat completion model
embed_model = OpenAIEmbedding(
model="text-embedding-ada-002",
deployment_name=config_details["ENGINE_EMBEDDING"],
api_key=os.getenv("OPENAI_API_KEY"),
api_base=config_details["OPENAI_API_BASE"],
api_type="azure",
api_version=config_details["OPENAI_API_VERSION"],
)
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
set_global_service_context(service_context)
index = VectorStoreIndex.from_documents(documents) # , service_context=service_context)
return index
def main():
index = load_data()
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
for message in st.session_state.messages: # Display the prior chat messages
with st.chat_message(message["role"]):
st.write(message["content"])
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
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
if __name__ == "__main__":
main()
|