123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- ---
- title: '⚙️ Custom configurations'
- ---
- 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.
- ## Examples
- ### Custom embedding function
- Here's the readme example with configuration options.
- ```python
- import os
- from embedchain import App
- from embedchain.config import InitConfig, AddConfig, QueryConfig
- from chromadb.utils import embedding_functions
- # Example: use your own embedding function
- config = InitConfig(ef=embedding_functions.OpenAIEmbeddingFunction(
- api_key=os.getenv("OPENAI_API_KEY"),
- organization_id=os.getenv("OPENAI_ORGANIZATION"),
- model_name="text-embedding-ada-002"
- ))
- naval_chat_bot = App(config)
- # Example: define your own chunker config for `youtube_video`
- youtube_add_config = {
- "chunker": {
- "chunk_size": 1000,
- "chunk_overlap": 100,
- "length_function": len,
- }
- }
- naval_chat_bot.add("youtube_video", "https://www.youtube.com/watch?v=3qHkcs3kG44", AddConfig(**youtube_add_config))
- add_config = AddConfig()
- naval_chat_bot.add("pdf_file", "https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf", add_config)
- naval_chat_bot.add("web_page", "https://nav.al/feedback", add_config)
- naval_chat_bot.add("web_page", "https://nav.al/agi", add_config)
- naval_chat_bot.add_local("qna_pair", ("Who is Naval Ravikant?", "Naval Ravikant is an Indian-American entrepreneur and investor."), add_config)
- query_config = QueryConfig() # Currently no options
- print(naval_chat_bot.query("What unique capacity does Naval argue humans possess when it comes to understanding explanations or concepts?", query_config))
- ```
- ### Custom prompt template
- Here's the example of using custom prompt template with `.query`
- ```python
- from embedchain.config import QueryConfig
- from embedchain.embedchain import App
- from string import Template
- import wikipedia
- einstein_chat_bot = App()
- # Embed Wikipedia page
- page = wikipedia.page("Albert Einstein")
- einstein_chat_bot.add("text", page.content)
- # Example: use your own custom template with `$context` and `$query`
- einstein_chat_template = Template("""
- You are Albert Einstein, a German-born theoretical physicist,
- widely ranked among the greatest and most influential scientists of all time.
- Use the following information about Albert Einstein to respond to
- the human's query acting as Albert Einstein.
- Context: $context
- 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.
- Human: $query
- Albert Einstein:""")
- query_config = QueryConfig(einstein_chat_template)
- queries = [
- "Where did you complete your studies?",
- "Why did you win nobel prize?",
- "Why did you divorce your first wife?",
- ]
- for query in queries:
- response = einstein_chat_bot.query(query, query_config)
- print("Query: ", query)
- print("Response: ", response)
- # Output
- # Query: Where did you complete your studies?
- # Response: I completed my secondary education at the Argovian cantonal school in Aarau, Switzerland.
- # Query: Why did you win nobel prize?
- # 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.
- # Query: Why did you divorce your first wife?
- # Response: We divorced due to living apart for five years.
- ```
- ## Other methods
- ### Reset
- Resets the database and deletes all embeddings. Irreversible. Requires reinitialization afterwards.
- ```python
- app.reset()
- ```
- ### Count
- Counts the number of embeddings (chunks) in the database.
- ```python
- print(app.count())
- # returns: 481
- ```
|