Created
March 2, 2024 13:32
-
-
Save shresthakamal/8436130230e8fa044282701daad7d6ee to your computer and use it in GitHub Desktop.
Vector Store based retreivals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load docs | |
| from langchain.document_loaders import WebBaseLoader | |
| loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/") | |
| data = loader.load() | |
| # Split | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| text_splitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 0) | |
| all_splits = text_splitter.split_documents(data) | |
| # Store splits | |
| from langchain.embeddings import OpenAIEmbeddings | |
| from langchain.vectorstores import Chroma | |
| vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings()) | |
| # RAG prompt | |
| from langchain import hub | |
| prompt = hub.pull("rlm/rag-prompt") | |
| # LLM | |
| from langchain.chains import RetrievalQA | |
| from langchain.chat_models import ChatOpenAI | |
| llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) | |
| # RetrievalQA | |
| qa_chain = RetrievalQA.from_chain_type( | |
| llm, | |
| retriever=vectorstore.as_retriever(), | |
| chain_type_kwargs={"prompt": prompt} | |
| ) | |
| question = "What are the approaches to Task Decomposition?" | |
| result = qa_chain({"query": question}) | |
| result["result"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment