diff --git a/labs/04-deploy-ai/04-streamlit/app.py b/labs/04-deploy-ai/04-streamlit/app.py index a4ebd4b..8f51a6c 100644 --- a/labs/04-deploy-ai/04-streamlit/app.py +++ b/labs/04-deploy-ai/04-streamlit/app.py @@ -2,7 +2,7 @@ from langchain.callbacks.base import BaseCallbackHandler from langchain.chains import RetrievalQAWithSourcesChain from langchain.chains import RetrievalQA -from langchain.chains import ConversationalRetrievalChain,LLMChain, RetrievalQA +from langchain.chains import ConversationalRetrievalChain, LLMChain, RetrievalQA import os from dotenv import load_dotenv @@ -10,53 +10,48 @@ load_dotenv() -openai_api_type = os.getenv("OPENAI_API_TYPE") -openai_api_key = os.getenv("OPENAI_API_KEY") -openai_api_base = os.getenv("OPENAI_API_BASE") openai_api_version = os.getenv("OPENAI_API_VERSION") deployment_name = os.getenv("OPENAI_COMPLETION_DEPLOYMENT_NAME") -completion_model = os.getenv("OPENAI_COMPLETION_MODEL") embedding_name = os.getenv("OPENAI_EMBEDDING_DEPLOYMENT_NAME") qdrant_url = os.getenv("QDRANT_URL") qdrant_collection = os.getenv("QDRANT_COLLECTION") st.set_page_config(page_title="Movie expert AI", page_icon="📺") + def settings(): # Vectorstore from langchain.vectorstores import Qdrant - from langchain.embeddings import OpenAIEmbeddings - embeddings_model = OpenAIEmbeddings( - deployment=embedding_name, + from langchain_openai import AzureOpenAIEmbeddings + + embeddings_model = AzureOpenAIEmbeddings( + azure_deployment=embedding_name, chunk_size=1 - ) + ) client = qdrant_client.QdrantClient( qdrant_url ) vector_store = Qdrant( - client=client, collection_name=qdrant_collection, + client=client, collection_name=qdrant_collection, embeddings=embeddings_model, ) # LLM - from langchain.llms import AzureOpenAI - from langchain.chat_models import AzureChatOpenAI + from langchain_openai import AzureChatOpenAI + llm = AzureChatOpenAI( - openai_api_type = openai_api_type, - openai_api_version = openai_api_version, - openai_api_base = openai_api_base, - openai_api_key = openai_api_key, - deployment_name = deployment_name, - model_name=completion_model + openai_api_version=openai_api_version, + deployment_name=deployment_name ) retriever = vector_store.as_retriever(search_type="mmr") return retriever, llm + class StreamHandler(BaseCallbackHandler): def __init__(self, container, initial_text=""): self.container = container @@ -92,15 +87,17 @@ def on_retriever_end(self, documents, **kwargs): retriever = st.session_state.retriever llm = st.session_state.llm -# User input +# User input question = st.text_input("`Ask a question:`") if question: - qa_sources_chain = RetrievalQAWithSourcesChain.from_chain_type(llm, retriever=retriever) + qa_sources_chain = RetrievalQAWithSourcesChain.from_chain_type( + llm, retriever=retriever) retrieval_streamer_cb = PrintRetrievalHandler(st.container()) answer = st.empty() stream_handler = StreamHandler(answer, initial_text="`Answer:`\n\n") - result = qa_sources_chain({"question": question},callbacks=[retrieval_streamer_cb, stream_handler]) + result = qa_sources_chain({"question": question}, callbacks=[ + retrieval_streamer_cb, stream_handler]) print(result) answer.info('`Answer:`\n\n' + result['answer']) - st.info('`Sources:`\n\n' + result['sources']) \ No newline at end of file + st.info('`Sources:`\n\n' + result['sources']) diff --git a/labs/04-deploy-ai/04-streamlit/requirements.txt b/labs/04-deploy-ai/04-streamlit/requirements.txt index c9ca71c..5a63e23 100644 --- a/labs/04-deploy-ai/04-streamlit/requirements.txt +++ b/labs/04-deploy-ai/04-streamlit/requirements.txt @@ -1,6 +1,6 @@ streamlit==1.28.2 -langchain==0.0.331 -qdrant-client==1.4.0 -openai==0.28.1 +langchain==0.1.10 +qdrant-client==1.7.3 html2text==2020.1.16 -tiktoken==0.2.0 \ No newline at end of file +tiktoken==0.6.0 +python-dotenv==1.0.1 \ No newline at end of file