mysql.mdx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ---
  2. title: '🐬 MySQL'
  3. ---
  4. 1. Setup the MySQL loader by configuring the SQL db.
  5. ```Python
  6. from embedchain.loaders.mysql import MySQLLoader
  7. config = {
  8. "host": "host",
  9. "port": "port",
  10. "database": "database",
  11. "user": "username",
  12. "password": "password",
  13. }
  14. mysql_loader = MySQLLoader(config=config)
  15. ```
  16. For more details on how to setup with valid config, check MySQL [documentation](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html).
  17. 2. Once you setup the loader, you can create an app and load data using the above MySQL loader
  18. ```Python
  19. import os
  20. from embedchain.pipeline import Pipeline as App
  21. app = App()
  22. app.add("SELECT * FROM table_name;", data_type='mysql', loader=mysql_loader)
  23. # Adds `(1, 'What is your net worth, Elon Musk?', "As of October 2023, Elon Musk's net worth is $255.2 billion.")`
  24. response = app.query(question)
  25. # Answer: As of October 2023, Elon Musk's net worth is $255.2 billion.
  26. ```
  27. NOTE: The `add` function of the app will accept any executable query to load data. DO NOT pass the `CREATE`, `INSERT` queries in `add` function.
  28. 3. We automatically create a chunker to chunk your SQL data, however if you wish to provide your own chunker class. Here is how you can do that:
  29. ``Python
  30. from embedchain.chunkers.mysql import MySQLChunker
  31. from embedchain.config.add_config import ChunkerConfig
  32. mysql_chunker_config = ChunkerConfig(chunk_size=1000, chunk_overlap=0, length_function=len)
  33. mysql_chunker = MySQLChunker(config=mysql_chunker_config)
  34. app.add("SELECT * FROM table_name;", data_type='mysql', loader=mysql_loader, chunker=mysql_chunker)
  35. ```