|
import os |
|
|
|
import openai |
|
|
|
from langchain.chains import LLMChain |
|
from langchain.chat_models import ChatOpenAI |
|
|
|
from langchain.embeddings import OpenAIEmbeddings |
|
from langchain.prompts import PromptTemplate |
|
from langchain_pinecone import PineconeVectorStore |
|
|
|
from langchain.retrievers import ContextualCompressionRetriever |
|
from langchain.retrievers.document_compressors import CohereRerank |
|
from langchain_community.llms import Cohere |
|
|
|
prompt_template = """Answer the question using the given context to the best of your ability. |
|
If you don't know, answer I don't know. |
|
Context: {context} |
|
Topic: {topic}""" |
|
|
|
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "topic"]) |
|
|
|
|
|
class LangOpen: |
|
def __init__(self, model_name: str) -> None: |
|
self.index = self.initialize_index("langOpen") |
|
self.llm = ChatOpenAI(temperature=0.3, model=model_name) |
|
self.chain = LLMChain(llm=self.llm, prompt=PROMPT) |
|
|
|
def initialize_index(self, index_name): |
|
embeddings = OpenAIEmbeddings(model="text-embedding-3-large") |
|
index_name = "openai-embeddings" |
|
vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings) |
|
return vectorstore |
|
|
|
|
|
def get_response(self, query_str): |
|
print("query_str: ", query_str) |
|
print("model_name: ", self.llm.model_name) |
|
|
|
vectorstore_retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 10}) |
|
compressor = CohereRerank() |
|
compression_retriever = ContextualCompressionRetriever( |
|
base_compressor=compressor, base_retriever=vectorstore_retriever |
|
) |
|
docs = compression_retriever.get_relevant_documents(query_str) |
|
inputs = [{"context": doc.page_content, "topic": query_str} for doc in docs] |
|
result = self.chain.apply(inputs)[0]["text"] |
|
return result |
|
|