query.mdx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ---
  2. title: '❓ query'
  3. ---
  4. `.query()` method empowers developers to ask questions and receive relevant answers through a user-friendly query API. Function signature is given 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 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.query("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. # {
  44. # 'url': 'https://www.forbes.com/profile/elon-musk',
  45. # 'score': 0.89,
  46. # ...
  47. # }
  48. # ),
  49. # (
  50. # '74% of the company, which is now called X.Wealth HistoryHOVER TO REVEAL NET WORTH BY YEARForbes ...',
  51. # {
  52. # 'url': 'https://www.forbes.com/profile/elon-musk',
  53. # 'score': 0.81,
  54. # ...
  55. # }
  56. # ),
  57. # (
  58. # 'founded in 2002, is worth nearly $150 billion after a $750 million tender offer in June 2023 ...',
  59. # {
  60. # 'url': 'https://www.forbes.com/profile/elon-musk',
  61. # 'score': 0.73,
  62. # ...
  63. # }
  64. # )
  65. # ]
  66. ```
  67. <Note>
  68. When `citations=True`, note that the returned `sources` are a list of tuples where each tuple has two elements (in the following order):
  69. 1. source chunk
  70. 2. dictionary with metadata about the source chunk
  71. - `url`: url of the source
  72. - `doc_id`: document id (used for book keeping purposes)
  73. - `score`: score of the source chunk with respect to the question
  74. - other metadata you might have added at the time of adding the source
  75. </Note>
  76. ### Without citations
  77. If you just want to return answers and don't want to return citations, you can use the following example:
  78. ```python Without Citations
  79. from embedchain import App
  80. # Initialize app
  81. app = App()
  82. # Add data source
  83. app.add("https://www.forbes.com/profile/elon-musk")
  84. # Get relevant answer for your query
  85. answer = app.query("What is the net worth of Elon?")
  86. print(answer)
  87. # Answer: The net worth of Elon Musk is $221.9 billion.
  88. ```