Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds retrieval logic #17

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/_google/docindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
from langchain_community.document_loaders import UnstructuredMarkdownLoader
from langchain_community.document_loaders import UnstructuredHTMLLoader
from langchain_pinecone import PineconeVectorStore
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from operator import itemgetter
from langchain_google_genai import ChatGoogleGenerativeAI


class GooglePineconeIndexer:
"""
Expand Down Expand Up @@ -234,3 +240,23 @@ def initialize_vectorstore(self, index_name):
)
vectorstore = PineconeVectorStore(index, embed, "text")
return vectorstore


def retrieve_and_generate(self, prompt: str ,query: str, top_k: int , index_name: str):
"""
Retrieve documents from the Pinecone index and generate a response.
"""
llm = ChatGoogleGenerativeAI(model = self.model_name, google_api_key=self.google_api_key)
rag_prompt = PromptTemplate(template = prompt, input_variables = ["query", "context"])
vector_store = self.initialize_vectorstore(index_name)
retriever = vector_store.as_retriver(search_kwargs = {"k": top_k})
rag_chain = (
{"context": itemgetter("query")| retriever,
"query": itemgetter("query"),
}
| rag_prompt
| llm
| StrOutputParser()
)

return rag_chain.invoke({"query": query})
34 changes: 34 additions & 0 deletions src/_openai/docindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from langchain_community.document_loaders import UnstructuredWordDocumentLoader
from langchain_community.document_loaders import UnstructuredMarkdownLoader
from langchain_community.document_loaders import UnstructuredHTMLLoader
from langchain_pinecone import PineconeVectorStore
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from operator import itemgetter
from langchain_openai import ChatOpenAI



class OpenaiPineconeIndexer:
"""
Expand Down Expand Up @@ -231,3 +238,30 @@ def initialize_vectorstore(self, index_name):
)
vectorstore = PineconeVectorStore(index, embed, "text")
return vectorstore


def retrieve_and_generate(self, prompt: str ,query: str, top_k: int , index_name: str):
"""
Retrieve documents from the Pinecone index and generate a response.
"""
llm = ChatOpenAI(temperature = 0, model = "gpt-3.5-turbo", openai_api_key = self.openai_api_key)
rag_prompt = PromptTemplate(template = prompt, input_variables = ["query", "context"])

vector_store = self.initialize_vectorstore(index_name)
retriever = vector_store.as_retriver(search_kwargs = {"k": top_k})
rag_chain = (
{"context": itemgetter("query")| retriever,
"query": itemgetter("query"),
}
| rag_prompt
| llm
| StrOutputParser()
)

return rag_chain.invoke({"query": query})






Loading