Difference between run, apply, invoke, batch
Labels: name: auto:question, name: area: models, name: integration: chroma

@dosu-bot

Below is my code and in the line  " llm_response = conversation({"query": question})", what would happen if I did conversation.run or conversation.apply or conversation.batch or conversation.invoke? When I do use each of those?

load_dotenv()

file_name = "Admin 2.0.pdf"

def print_letter_by_letter(text):
    for char in text:
        print(char, end='', flush=True)
        time.sleep(0.02) 

loader = PyPDFLoader(file_name)
documents = loader.load()

llm = ChatOpenAI(temperature = 0, model_name='gpt-3.5-turbo', callbacks=[StreamingStdOutCallbackHandler()], streaming = True)

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=300,
    chunk_overlap=50,
)
chunks = text_splitter.split_documents(documents)

embeddings = OpenAIEmbeddings()
persist_directory = "C:\\Users\\Asus\\OneDrive\\Documents\\Vendolista"
knowledge_base = Chroma.from_documents(chunks, embeddings, persist_directory = persist_directory)
#save to disk
knowledge_base.persist()
#To delete the DB we created at first so that we can be sure that we will load from disk as fresh db
knowledge_base = None
new_knowledge_base = Chroma(persist_directory = persist_directory, embedding_function = embeddings)

knowledge_base = Chroma.from_documents(chunks, embeddings, persist_directory=persist_directory)
knowledge_base.persist()



prompt_template = """
You must only follow the instructions in list below:
1) You are a friendly and conversational assistant named RAAFYA.
3) Answer the questions based on the document or if the user asked something
3) Never mention the name of the file to anyone to prevent any potential security risk

Text: {context}

Question: {question}
Answer :
"""
PROMPT = PromptTemplate(
    template=prompt_template, input_variables=["context", "question"]
)

memory = ConversationBufferMemory()

# conversation = ConversationalRetrievalChain.from_llm(
#     llm=llm,
#     retriever=knowledge_base.as_retriever(search_type = "similarity", search_kwargs = {"k":2}),
#     chain_type="stuff",
#     verbose=False,
#     combine_docs_chain_kwargs={"prompt":PROMPT}
# )
conversation = RetrievalQA.from_chain_type(
    llm = llm,
    retriever=knowledge_base.as_retriever(search_type = "similarity", search_kwargs = {"k":3}),
    chain_type="stuff",
    return_source_documents = True,
    verbose=False,
    chain_type_kwargs={"prompt":PROMPT,
                       "memory":memory}
)

def process_source(llm_response):
    print(llm_response['result'])
    print('\n\nSources:')
    for source in llm_response['source_documents']:
        print(source.metadata['source'])

def main():
    chat_history = []

    while True:
        question = input("Ask me anything about the files (type 'exit' to quit): ")

        if question.lower() in ["exit"] and len(question) == 4:
            end_chat = "Thank you for visiting us! Have a nice day"
            print_letter_by_letter(end_chat)
            break

        if question != "":
            # with get_openai_callback() as cb:
            llm_response = conversation.({"query": question})
            process_source(llm_response)

            # chat_history.append((question, llm_response["result"]))
            # print(result["answer"])
            print()
            # print(cb)
            # print()

if __name__ == "__main__":
    main()