import streamlit as st import os import weaviate from dotenv import load_dotenv,find_dotenv from langchain.vectorstores import Weaviate from langchain_core.prompts import ChatPromptTemplate from langchain_community.chat_models import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough load_dotenv(find_dotenv()) weaviate_api_key = os.getenv('WEAVIATE_API_KEY') weaviate_cluster = os.getenv('WEAVIATE_CLUSTER') st.set_page_config(page_title="RAG-Roman Empire") st.image("public/images/banner.png") st.write( """

Salvete! I'm here to help you learn about the Roman Empire. Feel free to ask me anything about Roman history and culture!

""", unsafe_allow_html=True ) with st.expander("Sample questions you can ask"): st.markdown( """

Note: you can also ask irrelevant questions to test how the RAG framework is working.

""", unsafe_allow_html=True, ) st.sidebar.subheader("Your OpenAI Key Please") openai_api_key = st.sidebar.text_input('Put Your Key Here: ', type="password") with st.sidebar: if openai_api_key.startswith('sk-') and weaviate_api_key and weaviate_cluster: st.success('API keys and URL already provided!', icon='✅') else: st.warning('Please enter your credentials!', icon='⚠️') with st.sidebar.expander("ℹ️ Disclaimer"): st.caption( """We appreciate your engagement! Please note, This demo app can be shut down after the Weaviate cluster expires on 6/22/2024. """ ) # Adding custom HTML content to the sidebar with your name and heading st.sidebar.markdown( """

This RAG App is built using:

Weaviate LangChain

""", unsafe_allow_html=True, ) st.sidebar.markdown( """

App created by

Prasad Mahamulkar

GitHub Repository

Follow me for more!

""", unsafe_allow_html=True, ) def get_qa_chain(): auth_config = weaviate.auth.AuthApiKey(api_key=weaviate_api_key) client = weaviate.Client( url=weaviate_cluster, additional_headers={"X-OpenAI-Api-key": openai_api_key}, auth_client_secret=auth_config, startup_period=10 ) vectorstore = Weaviate(client, "RomanEmpire", "content", attributes=["source"]) retriever = vectorstore.as_retriever() template = """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If the answer is not in the context, just say that you I don't have access to this information. Use five sentences maximum and keep the answer concise. Question: {question} Context: {context} Answer: """ prompt = ChatPromptTemplate.from_template(template) llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0, openai_api_key=openai_api_key) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) return chain def generate_response(input_text): if openai_api_key and weaviate_api_key and weaviate_cluster: chain = get_qa_chain() answer = chain.invoke(input_text) st.info(answer) else: st.warning("Please provide all required API keys and URL!") with st.form('my_form'): text = st.text_input('Enter text:', 'Tell me interesting facts about Roman Empire') submitted = st.form_submit_button('Submit') if not openai_api_key.startswith('sk-'): st.warning('Please enter your OpenAI API key!', icon='⚠') if submitted and openai_api_key.startswith('sk-'): generate_response(text)