repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | --- |
| #2 | title: '💬 chat' |
| #3 | --- |
| #4 | |
| #5 | `chat()` method allows you to chat over your data sources using a user-friendly chat API. You can find the signature below: |
| #6 | |
| #7 | ### Parameters |
| #8 | |
| #9 | <ParamField path="input_query" type="str"> |
| #10 | Question to ask |
| #11 | </ParamField> |
| #12 | <ParamField path="config" type="BaseLlmConfig" optional> |
| #13 | Configure different llm settings such as prompt, temprature, number_documents etc. |
| #14 | </ParamField> |
| #15 | <ParamField path="dry_run" type="bool" optional> |
| #16 | The purpose is to test the prompt structure without actually running LLM inference. Defaults to `False` |
| #17 | </ParamField> |
| #18 | <ParamField path="where" type="dict" optional> |
| #19 | A dictionary of key-value pairs to filter the chunks from the vector database. Defaults to `None` |
| #20 | </ParamField> |
| #21 | <ParamField path="session_id" type="str" optional> |
| #22 | Session ID of the chat. This can be used to maintain chat history of different user sessions. Default value: `default` |
| #23 | </ParamField> |
| #24 | <ParamField path="citations" type="bool" optional> |
| #25 | Return citations along with the LLM answer. Defaults to `False` |
| #26 | </ParamField> |
| #27 | |
| #28 | ### Returns |
| #29 | |
| #30 | <ResponseField name="answer" type="str | tuple"> |
| #31 | If `citations=False`, return a stringified answer to the question asked. <br /> |
| #32 | If `citations=True`, returns a tuple with answer and citations respectively. |
| #33 | </ResponseField> |
| #34 | |
| #35 | ## Usage |
| #36 | |
| #37 | ### With citations |
| #38 | |
| #39 | If you want to get the answer to question and return both answer and citations, use the following code snippet: |
| #40 | |
| #41 | ```python With Citations |
| #42 | from embedchain import App |
| #43 | |
| #44 | # Initialize app |
| #45 | app = App() |
| #46 | |
| #47 | # Add data source |
| #48 | app.add("https://www.forbes.com/profile/elon-musk") |
| #49 | |
| #50 | # Get relevant answer for your query |
| #51 | answer, sources = app.chat("What is the net worth of Elon?", citations=True) |
| #52 | print(answer) |
| #53 | # Answer: The net worth of Elon Musk is $221.9 billion. |
| #54 | |
| #55 | print(sources) |
| #56 | # [ |
| #57 | # ( |
| #58 | # 'Elon Musk PROFILEElon MuskCEO, Tesla$247.1B$2.3B (0.96%)Real Time Net Worthas of 12/7/23 ...', |
| #59 | # { |
| #60 | # 'url': 'https://www.forbes.com/profile/elon-musk', |
| #61 | # 'score': 0.89, |
| #62 | # ... |
| #63 | # } |
| #64 | # ), |
| #65 | # ( |
| #66 | # '74% of the company, which is now called X.Wealth HistoryHOVER TO REVEAL NET WORTH BY YEARForbes ...', |
| #67 | # { |
| #68 | # 'url': 'https://www.forbes.com/profile/elon-musk', |
| #69 | # 'score': 0.81, |
| #70 | # ... |
| #71 | # } |
| #72 | # ), |
| #73 | # ( |
| #74 | # 'founded in 2002, is worth nearly $150 billion after a $750 million tender offer in June 2023 ...', |
| #75 | # { |
| #76 | # 'url': 'https://www.forbes.com/profile/elon-musk', |
| #77 | # 'score': 0.73, |
| #78 | # ... |
| #79 | # } |
| #80 | # ) |
| #81 | # ] |
| #82 | ``` |
| #83 | |
| #84 | <Note> |
| #85 | When `citations=True`, note that the returned `sources` are a list of tuples where each tuple has two elements (in the following order): |
| #86 | 1. source chunk |
| #87 | 2. dictionary with metadata about the source chunk |
| #88 | - `url`: url of the source |
| #89 | - `doc_id`: document id (used for book keeping purposes) |
| #90 | - `score`: score of the source chunk with respect to the question |
| #91 | - other metadata you might have added at the time of adding the source |
| #92 | </Note> |
| #93 | |
| #94 | |
| #95 | ### Without citations |
| #96 | |
| #97 | If you just want to return answers and don't want to return citations, you can use the following example: |
| #98 | |
| #99 | ```python Without Citations |
| #100 | from embedchain import App |
| #101 | |
| #102 | # Initialize app |
| #103 | app = App() |
| #104 | |
| #105 | # Add data source |
| #106 | app.add("https://www.forbes.com/profile/elon-musk") |
| #107 | |
| #108 | # Chat on your data using `.chat()` |
| #109 | answer = app.chat("What is the net worth of Elon?") |
| #110 | print(answer) |
| #111 | # Answer: The net worth of Elon Musk is $221.9 billion. |
| #112 | ``` |
| #113 | |
| #114 | ### With session id |
| #115 | |
| #116 | If you want to maintain chat sessions for different users, you can simply pass the `session_id` keyword argument. See the example below: |
| #117 | |
| #118 | ```python With session id |
| #119 | from embedchain import App |
| #120 | |
| #121 | app = App() |
| #122 | app.add("https://www.forbes.com/profile/elon-musk") |
| #123 | |
| #124 | # Chat on your data using `.chat()` |
| #125 | app.chat("What is the net worth of Elon Musk?", session_id="user1") |
| #126 | # 'The net worth of Elon Musk is $250.8 billion.' |
| #127 | app.chat("What is the net worth of Bill Gates?", session_id="user2") |
| #128 | # "I don't know the current net worth of Bill Gates." |
| #129 | app.chat("What was my last question", session_id="user1") |
| #130 | # 'Your last question was "What is the net worth of Elon Musk?"' |
| #131 | ``` |
| #132 | |
| #133 | ### With custom context window |
| #134 | |
| #135 | If you want to customize the context window that you want to use during chat (default context window is 3 document chunks), you can do using the following code snippet: |
| #136 | |
| #137 | ```python with custom chunks size |
| #138 | from embedchain import App |
| #139 | from embedchain.config import BaseLlmConfig |
| #140 | |
| #141 | app = App() |
| #142 | app.add("https://www.forbes.com/profile/elon-musk") |
| #143 | |
| #144 | query_config = BaseLlmConfig(number_documents=5) |
| #145 | app.chat("What is the net worth of Elon Musk?", config=query_config) |
| #146 | ``` |
| #147 | |
| #148 | ### With Mem0 to store chat history |
| #149 | |
| #150 | Mem0 is a cutting-edge long-term memory for LLMs to enable personalization for the GenAI stack. It enables LLMs to remember past interactions and provide more personalized responses. |
| #151 | |
| #152 | In order to use Mem0 to enable memory for personalization in your apps: |
| #153 | - Install the [`mem0`](https://docs.mem0.ai/) package using `pip install mem0ai`. |
| #154 | - Prepare config for `memory`, refer [Configurations](docs/api-reference/advanced/configuration.mdx). |
| #155 | |
| #156 | ```python with mem0 |
| #157 | from embedchain import App |
| #158 | |
| #159 | config = { |
| #160 | "memory": { |
| #161 | "top_k": 5 |
| #162 | } |
| #163 | } |
| #164 | |
| #165 | app = App.from_config(config=config) |
| #166 | app.add("https://www.forbes.com/profile/elon-musk") |
| #167 | |
| #168 | app.chat("What is the net worth of Elon Musk?") |
| #169 | ``` |
| #170 | |
| #171 | ## How Mem0 works: |
| #172 | - Mem0 saves context derived from each user question into its memory. |
| #173 | - When a user poses a new question, Mem0 retrieves relevant previous memories. |
| #174 | - The `top_k` parameter in the memory configuration specifies the number of top memories to consider during retrieval. |
| #175 | - Mem0 generates the final response by integrating the user's question, context from the data source, and the relevant memories. |
| #176 |