configuration.mdx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ---
  2. title: '⚙️ Custom configurations'
  3. ---
  4. Embedchain is made to work out of the box. However, for advanced users we're also offering configuration options. All of these configuration options are optional and have sane defaults.
  5. ## Concept
  6. The main `App` class is available in the following varieties: `CustomApp`, `OpenSourceApp` and `Llama2App` and `App`. The first is fully configurable, the others are opinionated in some aspects.
  7. The `App` class has three subclasses: `llm`, `db` and `embedder`. These are the core ingredients that make up an EmbedChain app.
  8. App plus each one of the subclasses have a `config` attribute.
  9. You can pass a `Config` instance as an argument during initialization to persistently configure a class.
  10. These configs can be imported from `embedchain.config`
  11. There are `set` methods for some things that should not (only) be set at start-up, like `app.db.set_collection_name`.
  12. ## Examples
  13. ### General
  14. Here's the readme example with configuration options.
  15. ```python
  16. from embedchain import App
  17. from embedchain.config import AppConfig, AddConfig, LlmConfig, ChunkerConfig
  18. # Example: set the log level for debugging
  19. config = AppConfig(log_level="DEBUG")
  20. naval_chat_bot = App(config)
  21. # Example: specify a custom collection name
  22. naval_chat_bot.db.set_collection_name("naval_chat_bot")
  23. # Example: define your own chunker config for `youtube_video`
  24. chunker_config = ChunkerConfig(chunk_size=1000, chunk_overlap=100, length_function=len)
  25. # Example: Add your chunker config to an AddConfig to actually use it
  26. add_config = AddConfig(chunker=chunker_config)
  27. naval_chat_bot.add("https://www.youtube.com/watch?v=3qHkcs3kG44", config=add_config)
  28. # Example: Reset to default
  29. add_config = AddConfig()
  30. naval_chat_bot.add("https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf", config=add_config)
  31. naval_chat_bot.add("https://nav.al/feedback", config=add_config)
  32. naval_chat_bot.add("https://nav.al/agi", config=add_config)
  33. naval_chat_bot.add(("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."), config=add_config)
  34. # Change the number of documents.
  35. query_config = LlmConfig(number_documents=5)
  36. print(naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?", config=query_config))
  37. ```
  38. ### Custom prompt template
  39. Here's the example of using custom prompt template with `.query`
  40. ```python
  41. from string import Template
  42. import wikipedia
  43. from embedchain import App
  44. from embedchain.config import LlmConfig
  45. einstein_chat_bot = App()
  46. # Embed Wikipedia page
  47. page = wikipedia.page("Albert Einstein")
  48. einstein_chat_bot.add(page.content)
  49. # Example: use your own custom template with `$context` and `$query`
  50. einstein_chat_template = Template(
  51. """
  52. You are Albert Einstein, a German-born theoretical physicist,
  53. widely ranked among the greatest and most influential scientists of all time.
  54. Use the following information about Albert Einstein to respond to
  55. the human's query acting as Albert Einstein.
  56. Context: $context
  57. Keep the response brief. If you don't know the answer, just say that you don't know, don't try to make up an answer.
  58. Human: $query
  59. Albert Einstein:"""
  60. )
  61. # Example: Use the template, also add a system prompt.
  62. llm_config = LlmConfig(template=einstein_chat_template, system_prompt="You are Albert Einstein.")
  63. queries = [
  64. "Where did you complete your studies?",
  65. "Why did you win nobel prize?",
  66. "Why did you divorce your first wife?",
  67. ]
  68. for query in queries:
  69. response = einstein_chat_bot.query(query, config=llm_config)
  70. print("Query: ", query)
  71. print("Response: ", response)
  72. # Output
  73. # Query: Where did you complete your studies?
  74. # Response: I completed my secondary education at the Argovian cantonal school in Aarau, Switzerland.
  75. # Query: Why did you win nobel prize?
  76. # Response: I won the Nobel Prize in Physics in 1921 for my services to Theoretical Physics, particularly for my discovery of the law of the photoelectric effect.
  77. # Query: Why did you divorce your first wife?
  78. # Response: We divorced due to living apart for five years.
  79. ```