configuration.mdx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. ## Examples
  6. ### Custom embedding function
  7. Here's the readme example with configuration options.
  8. ```python
  9. import os
  10. from embedchain import App
  11. from embedchain.config import AppConfig, AddConfig, QueryConfig, ChunkerConfig
  12. from chromadb.utils import embedding_functions
  13. # Example: use your own embedding function
  14. # Warning: We are currenty reworking the concept of custom apps, this might not be working.
  15. config = AppConfig(ef=embedding_functions.OpenAIEmbeddingFunction(
  16. api_key=os.getenv("OPENAI_API_KEY"),
  17. organization_id=os.getenv("OPENAI_ORGANIZATION"),
  18. model_name="text-embedding-ada-002"
  19. ))
  20. naval_chat_bot = App(config)
  21. # Example: define your own chunker config for `youtube_video`
  22. chunker_config = ChunkerConfig(chunk_size=1000, chunk_overlap=100, length_function=len)
  23. naval_chat_bot.add("youtube_video", "https://www.youtube.com/watch?v=3qHkcs3kG44", AddConfig(chunker=chunker_config))
  24. add_config = AddConfig()
  25. naval_chat_bot.add("pdf_file", "https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf", add_config)
  26. naval_chat_bot.add("web_page", "https://nav.al/feedback", add_config)
  27. naval_chat_bot.add("web_page", "https://nav.al/agi", add_config)
  28. naval_chat_bot.add_local("qna_pair", ("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."), add_config)
  29. query_config = QueryConfig() # Currently no options
  30. print(naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?", query_config))
  31. ```
  32. ### Custom prompt template
  33. Here's the example of using custom prompt template with `.query`
  34. ```python
  35. from embedchain.config import QueryConfig
  36. from embedchain.embedchain import App
  37. from string import Template
  38. import wikipedia
  39. einstein_chat_bot = App()
  40. # Embed Wikipedia page
  41. page = wikipedia.page("Albert Einstein")
  42. einstein_chat_bot.add("text", page.content)
  43. # Example: use your own custom template with `$context` and `$query`
  44. einstein_chat_template = Template("""
  45. You are Albert Einstein, a German-born theoretical physicist,
  46. widely ranked among the greatest and most influential scientists of all time.
  47. Use the following information about Albert Einstein to respond to
  48. the human's query acting as Albert Einstein.
  49. Context: $context
  50. 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.
  51. Human: $query
  52. Albert Einstein:""")
  53. query_config = QueryConfig(einstein_chat_template)
  54. queries = [
  55. "Where did you complete your studies?",
  56. "Why did you win nobel prize?",
  57. "Why did you divorce your first wife?",
  58. ]
  59. for query in queries:
  60. response = einstein_chat_bot.query(query, query_config)
  61. print("Query: ", query)
  62. print("Response: ", response)
  63. # Output
  64. # Query: Where did you complete your studies?
  65. # Response: I completed my secondary education at the Argovian cantonal school in Aarau, Switzerland.
  66. # Query: Why did you win nobel prize?
  67. # 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.
  68. # Query: Why did you divorce your first wife?
  69. # Response: We divorced due to living apart for five years.
  70. ```
  71. ## Other methods
  72. ### Reset
  73. Resets the database and deletes all embeddings. Irreversible. Requires reinitialization afterwards.
  74. ```python
  75. app.reset()
  76. ```
  77. ### Count
  78. Counts the number of embeddings (chunks) in the database.
  79. ```python
  80. print(app.count())
  81. # returns: 481
  82. ```