chat.mdx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ---
  2. title: '💬 chat'
  3. ---
  4. `chat()` method allows you to chat over your data sources using a user-friendly chat API. You can find the signature below:
  5. ### Parameters
  6. <ParamField path="input_query" type="str">
  7. Question to ask
  8. </ParamField>
  9. <ParamField path="config" type="BaseLlmConfig" optional>
  10. Configure different llm settings such as prompt, temprature, number_documents etc.
  11. </ParamField>
  12. <ParamField path="dry_run" type="bool" optional>
  13. The purpose is to test the prompt structure without actually running LLM inference. Defaults to `False`
  14. </ParamField>
  15. <ParamField path="where" type="dict" optional>
  16. A dictionary of key-value pairs to filter the chunks from the vector database. Defaults to `None`
  17. </ParamField>
  18. <ParamField path="citations" type="bool" optional>
  19. Return citations along with the LLM answer. Defaults to `False`
  20. </ParamField>
  21. ### Returns
  22. <ResponseField name="answer" type="str | tuple">
  23. If `citations=False`, return a stringified answer to the question asked. <br />
  24. If `citations=True`, returns a tuple with answer and citations respectively.
  25. </ResponseField>
  26. ## Usage
  27. ### With citations
  28. If you want to get the answer to question and return both answer and citations, use the following code snippet:
  29. ```python With Citations
  30. from embedchain import Pipeline as App
  31. # Initialize app
  32. app = App()
  33. # Add data source
  34. app.add("https://www.forbes.com/profile/elon-musk")
  35. # Get relevant answer for your query
  36. answer, sources = app.chat("What is the net worth of Elon?", citations=True)
  37. print(answer)
  38. # Answer: The net worth of Elon Musk is $221.9 billion.
  39. print(sources)
  40. # [
  41. # (
  42. # 'Elon Musk PROFILEElon MuskCEO, Tesla$247.1B$2.3B (0.96%)Real Time Net Worthas of 12/7/23 ...',
  43. # 'https://www.forbes.com/profile/elon-musk',
  44. # '4651b266--4aa78839fe97'
  45. # ),
  46. # (
  47. # '74% of the company, which is now called X.Wealth HistoryHOVER TO REVEAL NET WORTH BY YEARForbes ...',
  48. # 'https://www.forbes.com/profile/elon-musk',
  49. # '4651b266--4aa78839fe97'
  50. # ),
  51. # (
  52. # 'founded in 2002, is worth nearly $150 billion after a $750 million tender offer in June 2023 ...',
  53. # 'https://www.forbes.com/profile/elon-musk',
  54. # '4651b266--4aa78839fe97'
  55. # )
  56. # ]
  57. ```
  58. <Note>
  59. When `citations=True`, note that the returned `sources` are a list of tuples where each tuple has three elements (in the following order):
  60. 1. source chunk
  61. 2. link of the source document
  62. 3. document id (used for book keeping purposes)
  63. </Note>
  64. ### Without citations
  65. If you just want to return answers and don't want to return citations, you can use the following example:
  66. ```python Without Citations
  67. from embedchain import Pipeline as App
  68. # Initialize app
  69. app = App()
  70. # Add data source
  71. app.add("https://www.forbes.com/profile/elon-musk")
  72. # Chat on your data using `.chat()`
  73. answer = app.chat("What is the net worth of Elon?")
  74. print(answer)
  75. # Answer: The net worth of Elon Musk is $221.9 billion.
  76. ```