configuration.mdx 3.7 KB

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