alejandro-ao / ask-multiple-pdfs

A Langchain app that allows you to chat with multiple PDFs
1.66k stars 940 forks source link

Unable to use get_openai_callback for ChatOpenAI() and OpenAIEmbeddings() #15

Open siddesai80 opened 1 year ago

siddesai80 commented 1 year ago

I attempted to use the callback function from langchain.callbacks to monitor the cost of each OpenAI request in my application, as demonstrated in the langchain-ask-pdf project video. However, I discovered that it wasn't providing any data. I suspect that the current version of langchain doesn't support the functionality for ChatOpenAI and OpenAIEmbeddings, or perhaps I made an error in my implementation.

I have included the code I used below. Any assistance with this issue would be greatly appreciated.

Code for OpenAIEmbeddings()

def create_vectorstore(text_chunks):
    embeddings = OpenAIEmbeddings()
    # embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
    with get_openai_callback() as cost:
         vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
         print(cost)
    return vectorstore

Code for ChatOpenAI()

def create_conversation_chain(vector_store):
    llm = ChatOpenAI()
    # llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
    memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
    with get_openai_callback() as cost:
        conversation_chain = ConversationalRetrievalChain.from_llm(
            llm=llm,
            retriever=vector_store.as_retriever(),
            memory=memory
        )
        print(cost)
    return conversation_chain