Skip to content

Commit

Permalink
++ .env & easy configuration for multiple variables
Browse files Browse the repository at this point in the history
	new file:   LangChain/Retrieval-Augmented-Generation/.env.template
	modified:   LangChain/Retrieval-Augmented-Generation/qa_local_docs.py
	modified:   LangChain/Retrieval-Augmented-Generation/test.py
  • Loading branch information
Daethyra committed Oct 12, 2023
1 parent 9034a6b commit d60d308
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions LangChain/Retrieval-Augmented-Generation/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OPENAI_API_KEY=
SIMILARITY_THRESHOLD=0.7
CHUNK_SIZE=500
CHUNK_OVERLAP=0
LLM_CHAIN_PROMPT_URL=https://smith.langchain.com/hub/rlm/rag-prompt
3 changes: 2 additions & 1 deletion LangChain/Retrieval-Augmented-Generation/qa_local_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _load_env_vars(self):
self.OPENAI_API_KEY = os.getenv('OPENAI_API_KEY', 'sk-')
if not self.OPENAI_API_KEY:
raise ValueError("OPENAI_API_KEY is missing. Please set the environment variable.")
self.LLM_CHAIN_PROMPT_URL = os.getenv('LLM_CHAIN_PROMPT_URL', 'https://smith.langchain.com/hub/rlm/rag-prompt')
except ValueError as ve:
print(f"ValueError encountered: {ve}")
raise
Expand Down Expand Up @@ -101,7 +102,7 @@ def load_pdfs_from_directory(self, directory_path: str = 'data/') -> List[List[s
retriever=self.vectorstore.as_retriever(),
# Pull premade RAG prompt from
# https://smith.langchain.com/hub/rlm/rag-prompt
chain_type_kwargs={"prompt": hub.pull("rlm/rag-prompt")}
chain_type_kwargs={"prompt": hub.pull(self.LLM_CHAIN_PROMPT_URL)}
)
# Return all text splits from PDFs
return all_splits
Expand Down
10 changes: 8 additions & 2 deletions LangChain/Retrieval-Augmented-Generation/test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import unittest
from unittest.mock import patch, MagicMock
from qa_local_docs import PDFProcessor
from qa_local_docs import PDFProcessor, ChatOpenAI, Chroma, UniversalSentenceEncoder, RetrievalQA

# Assumes that 'data/' directory contains PDFs
class TestPDFProcessor(unittest.TestCase):
# Set up reusable objects
def setUp(self):
self.pdf_processor = PDFProcessor()
embeddings = UniversalSentenceEncoder()
llm = ChatOpenAI()
vectorstore = Chroma()
qa_chain = RetrievalQA()
# Tie reusable objects together
self.pdf_processor = PDFProcessor(embeddings, llm, vectorstore, qa_chain)

def test_load_pdfs_from_directory(self):
# Test that the method returns a non-empty list
Expand Down

0 comments on commit d60d308

Please sign in to comment.