Skip to content

Commit

Permalink
Merge pull request #88 from wuerzelchen/main
Browse files Browse the repository at this point in the history
refactor(04/04-streamlit): upgraded to use langchain 0.1.10
  • Loading branch information
denniszielke authored Mar 25, 2024
2 parents a795059 + 3418f58 commit d98b2c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
41 changes: 19 additions & 22 deletions labs/04-deploy-ai/04-streamlit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,56 @@
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
import qdrant_client

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
Expand Down Expand Up @@ -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'])
st.info('`Sources:`\n\n' + result['sources'])
8 changes: 4 additions & 4 deletions labs/04-deploy-ai/04-streamlit/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
tiktoken==0.6.0
python-dotenv==1.0.1

0 comments on commit d98b2c6

Please sign in to comment.