Chat with famous authors

1 minute read

Published:

This is a project that let you chat with any characters. For example, you can chat with Karl Marx (the author of Das Kapital), Siddhartha (the protagonist in Hermann Hesse’s book), etc.

Demo

unavailable   unavailable

  • Another use of this project is that it lets you fetch information very efficiently from any document. Below is an example chatting with the 2022 annual report of Apple. We can see that the chatbot manages to grasp the information correctly from the 80-page apple 2022 annual report.

unavailable unavailable

Implementation

  • The implementation of this chatbot is incredibly simple. With langchain and gradio, the whole stack can be built within 100 lines of code.
    • First use langchain to divide any text into chunks. For Marx chatbot, I used Das Kapital. For Siddhartha chatbot, I used the book Siddhartha. There should be some overlap between chunks to ensure the integrity of sentences. I set the chunk size and overlap to 600 and 100.
    • For each chunk, calculate the embedding, which represent the “meaning” of this chunk. This step can be easily done with langchain and a vector database like FAISS. Btw, the cost of calculating embedding is very small, since the API of openai embedding model is very cheap.
    • Then build the chatbot. We just make some variation to the traditional chatbot. When we pass in the question, we first calculate the embedding of the question and search through the vector database to find the most similar chunk. Here the similarity of “meaning” is measured by the distance of the two embeddings.
    • After we find the closest chunk, we plug it into our prompt. Pass the prompt to openai to fetch the answer.
    • Use gradio to build the interface.
  • Link to the repo:
    • https://github.com/scottyaohk/Chat_With_Author/tree/main