mysql.mdx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. from embedchain.pipeline import Pipeline as App
  20. app = App()
  21. app.add("SELECT * FROM table_name;", data_type='mysql', loader=mysql_loader)
  22. # Adds `(1, 'What is your net worth, Elon Musk?', "As of October 2023, Elon Musk's net worth is $255.2 billion.")`
  23. response = app.query(question)
  24. # Answer: As of October 2023, Elon Musk's net worth is $255.2 billion.
  25. ```
  26. 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.
  27. 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:
  28. ``Python
  29. from embedchain.chunkers.mysql import MySQLChunker
  30. from embedchain.config.add_config import ChunkerConfig
  31. mysql_chunker_config = ChunkerConfig(chunk_size=1000, chunk_overlap=0, length_function=len)
  32. mysql_chunker = MySQLChunker(config=mysql_chunker_config)
  33. app.add("SELECT * FROM table_name;", data_type='mysql', loader=mysql_loader, chunker=mysql_chunker)
  34. ```