query.mdx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 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.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. # {'url': 'https://www.forbes.com/profile/elon-musk', ...}
  44. # ),
  45. # (
  46. # '74% of the company, which is now called X.Wealth HistoryHOVER TO REVEAL NET WORTH BY YEARForbes ...',
  47. # {'url': 'https://www.forbes.com/profile/elon-musk', ...}
  48. # ),
  49. # (
  50. # 'founded in 2002, is worth nearly $150 billion after a $750 million tender offer in June 2023 ...',
  51. # {'url': 'https://www.forbes.com/profile/elon-musk', ...}
  52. # )
  53. # ]
  54. ```
  55. <Note>
  56. When `citations=True`, note that the returned `sources` are a list of tuples where each tuple has three elements (in the following order):
  57. 1. source chunk
  58. 2. link of the source document
  59. 3. document id (used for book keeping purposes)
  60. </Note>
  61. ### Without citations
  62. If you just want to return answers and don't want to return citations, you can use the following example:
  63. ```python Without Citations
  64. from embedchain import Pipeline as App
  65. # Initialize app
  66. app = App()
  67. # Add data source
  68. app.add("https://www.forbes.com/profile/elon-musk")
  69. # Get relevant answer for your query
  70. answer = app.query("What is the net worth of Elon?")
  71. print(answer)
  72. # Answer: The net worth of Elon Musk is $221.9 billion.
  73. ```