advanced_usage.mdx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ---
  2. title: '📱 App types'
  3. ---
  4. Creating a chatbot involves 3 steps:
  5. - ⚙️ Import the App instance
  6. - 🗃️ Add Dataset
  7. - 💬 Query or Chat on the dataset and get answers (Interface Types)
  8. ## App Types
  9. We have three types of App.
  10. ### App
  11. ```python
  12. from embedchain import App
  13. naval_chat_bot = App()
  14. ```
  15. - `App` uses OpenAI's model, so these are paid models. 💸 You will be charged for embedding model usage and LLM usage.
  16. - `App` uses OpenAI's embedding model to create embeddings for chunks and ChatGPT API as LLM to get answer given the relevant docs. Make sure that you have an OpenAI account and an API key. If you have don't have an API key, you can create one by visiting [this link](https://platform.openai.com/account/api-keys).
  17. - Once you have the API key, set it in an environment variable called `OPENAI_API_KEY`
  18. ```python
  19. import os
  20. os.environ["OPENAI_API_KEY"] = "sk-xxxx"
  21. ```
  22. ### OpenSourceApp
  23. ```python
  24. from embedchain import OpenSourceApp
  25. naval_chat_bot = OpenSourceApp()
  26. ```
  27. - `OpenSourceApp` uses open source embedding and LLM model. It uses `all-MiniLM-L6-v2` from Sentence Transformers library as the embedding model and `gpt4all` as the LLM.
  28. - Here there is no need to setup any api keys. You just need to install embedchain package and these will get automatically installed. 📦
  29. - Once you have imported and instantiated the app, every functionality from here onwards is the same for either type of app. 📚
  30. ### PersonApp
  31. ```python
  32. from embedchain import PersonApp
  33. naval_chat_bot = PersonApp("name_of_person_or_character") #Like "Yoda"
  34. ```
  35. - `PersonApp` uses OpenAI's model, so these are paid models. 💸 You will be charged for embedding model usage and LLM usage.
  36. - `PersonApp` uses OpenAI's embedding model to create embeddings for chunks and ChatGPT API as LLM to get answer given the relevant docs. Make sure that you have an OpenAI account and an API key. If you have don't have an API key, you can create one by visiting [this link](https://platform.openai.com/account/api-keys).
  37. - Once you have the API key, set it in an environment variable called `OPENAI_API_KEY`
  38. ```python
  39. import os
  40. os.environ["OPENAI_API_KEY"] = "sk-xxxx"
  41. ```
  42. ## Add Dataset
  43. - This step assumes that you have already created an `app` instance by either using `App` or `OpenSourceApp`. We are calling our app instance as `naval_chat_bot` 🤖
  44. - Now use `.add()` function to add any dataset.
  45. ```python
  46. # naval_chat_bot = App() or
  47. # naval_chat_bot = OpenSourceApp()
  48. # Embed Online Resources
  49. naval_chat_bot.add("youtube_video", "https://www.youtube.com/watch?v=3qHkcs3kG44")
  50. naval_chat_bot.add("pdf_file", "https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf")
  51. naval_chat_bot.add("web_page", "https://nav.al/feedback")
  52. naval_chat_bot.add("web_page", "https://nav.al/agi")
  53. # Embed Local Resources
  54. naval_chat_bot.add_local("qna_pair", ("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."))
  55. ```
  56. - If there is any other app instance in your script or app, you can change the import as
  57. ```python
  58. from embedchain import App as EmbedChainApp
  59. from embedchain import OpenSourceApp as EmbedChainOSApp
  60. from embedchain import PersonApp as EmbedChainPersonApp
  61. # or
  62. from embedchain import App as ECApp
  63. from embedchain import OpenSourceApp as ECOSApp
  64. from embedchain import PersonApp as ECPApp
  65. ```
  66. ## Interface Types
  67. ### Query Interface
  68. - This interface is like a question answering bot. It takes a question and gets the answer. It does not maintain context about the previous chats.❓
  69. - To use this, call `.query()` function to get the answer for any query.
  70. ```python
  71. print(naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?"))
  72. # answer: Naval argues that humans possess the unique capacity to understand explanations or concepts to the maximum extent possible in this physical reality.
  73. ```
  74. ### Chat Interface
  75. - This interface is chat interface where it remembers previous conversation. Right now it remembers 5 conversation by default. 💬
  76. - To use this, call `.chat` function to get the answer for any query.
  77. ```python
  78. print(naval_chat_bot.chat("How to be happy in life?"))
  79. # answer: The most important trick to being happy is to realize happiness is a skill you develop and a choice you make. You choose to be happy, and then you work at it. It's just like building muscles or succeeding at your job. It's about recognizing the abundance and gifts around you at all times.
  80. print(naval_chat_bot.chat("who is naval ravikant?"))
  81. # answer: Naval Ravikant is an Indian-American entrepreneur and investor.
  82. print(naval_chat_bot.chat("what did the author say about happiness?"))
  83. # answer: The author, Naval Ravikant, believes that happiness is a choice you make and a skill you develop. He compares the mind to the body, stating that just as the body can be molded and changed, so can the mind. He emphasizes the importance of being present in the moment and not getting caught up in regrets of the past or worries about the future. By being present and grateful for where you are, you can experience true happiness.
  84. ```
  85. ### Stream Response
  86. - You can add config to your query method to stream responses like ChatGPT does. You would require a downstream handler to render the chunk in your desirable format. Supports both OpenAI model and OpenSourceApp. 📊
  87. - To use this, instantiate a `QueryConfig` or `ChatConfig` object with `stream=True`. Then pass it to the `.chat()` or `.query()` method. The following example iterates through the chunks and prints them as they appear.
  88. ```python
  89. app = App()
  90. query_config = QueryConfig(stream = True)
  91. resp = app.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?", query_config)
  92. for chunk in resp:
  93. print(chunk, end="", flush=True)
  94. # answer: Naval argues that humans possess the unique capacity to understand explanations or concepts to the maximum extent possible in this physical reality.
  95. ```