Skip to content

Commit beddc20

Browse files
committed
fix: resolve mypy type checking issues
1 parent b1d98ab commit beddc20

4 files changed

Lines changed: 29 additions & 25 deletions

File tree

conftest.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
import pytest
33
import tempfile
44
from unittest.mock import patch, MagicMock
5-
from typing import List, Dict, Any
5+
from typing import List, Dict, Any, Optional
66
import numpy as np
7+
from langchain.schema import Document, AIMessage
78

89
from document_processor import DocumentProcessor
910
from vector_store import VectorStore
1011
from rag import RAG
1112

12-
class MockDocument:
13+
class MockDocument(Document):
1314
"""Mock LangChain document for testing."""
1415

15-
def __init__(self, page_content: str, metadata: Dict[str, Any] = None):
16-
self.page_content = page_content
17-
self.metadata = metadata or {}
16+
def __init__(self, page_content: str, metadata: Optional[Dict[str, Any]] = None):
17+
super().__init__(page_content=page_content, metadata=metadata or {})
1818

1919

2020
@pytest.fixture
21-
def sample_documents() -> List[MockDocument]:
21+
def sample_documents() -> List[Document]:
2222
"""Create sample documents for testing."""
2323
return [
2424
MockDocument(
@@ -55,7 +55,7 @@ def mock_pdf_file():
5555

5656

5757
@pytest.fixture
58-
def document_processor():
58+
def document_processor() -> DocumentProcessor:
5959
"""Create a DocumentProcessor instance for testing."""
6060
return DocumentProcessor(chunk_size=100, chunk_overlap=20)
6161

@@ -77,7 +77,7 @@ def mock_sentence_transformer():
7777

7878

7979
@pytest.fixture
80-
def vector_store(mock_sentence_transformer):
80+
def vector_store(mock_sentence_transformer) -> VectorStore:
8181
"""Create a VectorStore instance with mocked embeddings for testing."""
8282
return VectorStore()
8383

@@ -89,15 +89,14 @@ def mock_openai():
8989
# Configure the mock response
9090
instance = MagicMock()
9191

92-
response = MagicMock()
93-
response.content = "This is a mock response from the language model."
92+
response = AIMessage(content="This is a mock response from the language model.")
9493

9594
instance.invoke = MagicMock(return_value=response)
9695
mock.return_value = instance
9796
yield mock
9897

9998

10099
@pytest.fixture
101-
def rag_instance(vector_store, mock_openai):
100+
def rag_instance(vector_store, mock_openai) -> RAG:
102101
"""Create a RAG instance with a mocked vector store and language model."""
103102
return RAG(vector_store)

document_processor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
2-
from typing import List, Dict, Any
2+
from typing import List
33
from langchain_community.document_loaders import PyPDFLoader
44
from langchain.text_splitter import RecursiveCharacterTextSplitter
5+
from langchain.schema import Document
56

67
class DocumentProcessor:
78
"""Handles document loading and chunking."""
@@ -22,7 +23,7 @@ def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 200):
2223
length_function=len,
2324
)
2425

25-
def load_pdf(self, file_path: str) -> List[Dict[str, Any]]:
26+
def load_pdf(self, file_path: str) -> List[Document]:
2627
"""
2728
Load a PDF document and split it into chunks.
2829
@@ -48,7 +49,7 @@ def load_pdf(self, file_path: str) -> List[Dict[str, Any]]:
4849
except Exception as e:
4950
raise Exception(f"Error loading PDF: {str(e)}")
5051

51-
def chunk_documents(self, documents: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
52+
def chunk_documents(self, documents: List[Document]) -> List[Document]:
5253
"""
5354
Split documents into smaller chunks.
5455
@@ -60,7 +61,7 @@ def chunk_documents(self, documents: List[Dict[str, Any]]) -> List[Dict[str, Any
6061
"""
6162
return self.text_splitter.split_documents(documents)
6263

63-
def process_document(self, file_path: str) -> List[Dict[str, Any]]:
64+
def process_document(self, file_path: str) -> List[Document]:
6465
"""
6566
Process a document: load and chunk it.
6667

rag.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import os
2-
from typing import List, Dict, Any
2+
from typing import List, Dict, Any, Optional, Union, cast
33
from dotenv import load_dotenv
44
from langchain_openai import ChatOpenAI
5-
from langchain.schema import HumanMessage, SystemMessage
5+
from langchain.schema import HumanMessage, SystemMessage, AIMessage, BaseMessage
6+
from langchain.schema import Document
67

78
load_dotenv()
89

910
class RAG:
1011
"""Retrieval Augmented Generation for question answering."""
1112

12-
def __init__(self, vector_store, model_name: str = "gpt-3.5-turbo"):
13+
def __init__(self, vector_store, temperature: float = 0.7):
1314
"""
1415
Initialize the RAG system.
1516
1617
Args:
1718
vector_store: Vector store instance
18-
model_name: OpenAI model name
19+
temperature: Model temperature for response generation
1920
"""
2021
self.vector_store = vector_store
21-
self.model = ChatOpenAI(model_name=model_name)
22+
self.model = ChatOpenAI(temperature=temperature)
2223

2324
def generate_answer(self, query: str, k: int = 4) -> str:
2425
"""
@@ -49,6 +50,8 @@ def generate_answer(self, query: str, k: int = 4) -> str:
4950
# Generate response
5051
try:
5152
response = self.model.invoke(messages)
52-
return response.content
53+
if isinstance(response, BaseMessage):
54+
return str(response.content)
55+
return str(response)
5356
except Exception as e:
5457
return f"Error generating response: {str(e)}"

vector_store.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
import faiss
33
import numpy as np
4-
from typing import List, Dict, Any, Tuple
4+
from typing import List, Dict, Any, Optional
5+
from langchain.schema import Document
56
from sentence_transformers import SentenceTransformer
67

78
class VectorStore:
@@ -18,11 +19,11 @@ def __init__(self, model_name: str = "all-MiniLM-L6-v2"):
1819
self.model = SentenceTransformer(model_name)
1920
self.dimension = self.model.get_sentence_embedding_dimension()
2021
self.index = faiss.IndexFlatL2(self.dimension)
21-
self.documents = []
22+
self.documents: List[Document] = []
2223
except Exception as e:
2324
raise Exception(f"Error initializing vector store: {str(e)}")
2425

25-
def add_documents(self, documents: List[Dict[str, Any]]) -> None:
26+
def add_documents(self, documents: List[Document]) -> None:
2627
"""
2728
Add documents to the vector store.
2829
@@ -45,7 +46,7 @@ def add_documents(self, documents: List[Dict[str, Any]]) -> None:
4546
except Exception as e:
4647
raise Exception(f"Error adding documents: {str(e)}")
4748

48-
def similarity_search(self, query: str, k: int = 4) -> List[Dict[str, Any]]:
49+
def similarity_search(self, query: str, k: int = 4) -> List[Document]:
4950
"""
5051
Perform a similarity search for the query.
5152

0 commit comments

Comments
 (0)