010_migrate_modelfiles_to_models.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. """Peewee migrations -- 009_add_models.py.
  2. Some examples (model - class or model name)::
  3. > Model = migrator.orm['table_name'] # Return model in current state by name
  4. > Model = migrator.ModelClass # Return model in current state by name
  5. > migrator.sql(sql) # Run custom SQL
  6. > migrator.run(func, *args, **kwargs) # Run python function with the given args
  7. > migrator.create_model(Model) # Create a model (could be used as decorator)
  8. > migrator.remove_model(model, cascade=True) # Remove a model
  9. > migrator.add_fields(model, **fields) # Add fields to a model
  10. > migrator.change_fields(model, **fields) # Change fields
  11. > migrator.remove_fields(model, *field_names, cascade=True)
  12. > migrator.rename_field(model, old_field_name, new_field_name)
  13. > migrator.rename_table(model, new_table_name)
  14. > migrator.add_index(model, *col_names, unique=False)
  15. > migrator.add_not_null(model, *field_names)
  16. > migrator.add_default(model, field_name, default)
  17. > migrator.add_constraint(model, name, sql)
  18. > migrator.drop_index(model, *col_names)
  19. > migrator.drop_not_null(model, *field_names)
  20. > migrator.drop_constraints(model, *constraints)
  21. """
  22. from contextlib import suppress
  23. import peewee as pw
  24. from peewee_migrate import Migrator
  25. import json
  26. from utils.misc import parse_ollama_modelfile
  27. with suppress(ImportError):
  28. import playhouse.postgres_ext as pw_pext
  29. def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
  30. """Write your migrations here."""
  31. # Fetch data from 'modelfile' table and insert into 'model' table
  32. migrate_modelfile_to_model(migrator, database)
  33. # Drop the 'modelfile' table
  34. migrator.remove_model("modelfile")
  35. def migrate_modelfile_to_model(migrator: Migrator, database: pw.Database):
  36. ModelFile = migrator.orm["modelfile"]
  37. Model = migrator.orm["model"]
  38. modelfiles = ModelFile.select()
  39. for modelfile in modelfiles:
  40. # Extract and transform data in Python
  41. modelfile.modelfile = json.loads(modelfile.modelfile)
  42. meta = json.dumps(
  43. {
  44. "description": modelfile.modelfile.get("desc"),
  45. "profile_image_url": modelfile.modelfile.get("imageUrl"),
  46. "ollama": {"modelfile": modelfile.modelfile.get("content")},
  47. "suggestion_prompts": modelfile.modelfile.get("suggestionPrompts"),
  48. "categories": modelfile.modelfile.get("categories"),
  49. "user": {**modelfile.modelfile.get("user", {}), "community": True},
  50. }
  51. )
  52. info = parse_ollama_modelfile(modelfile.modelfile.get("content"))
  53. # Insert the processed data into the 'model' table
  54. Model.create(
  55. id=f"ollama-{modelfile.tag_name}",
  56. user_id=modelfile.user_id,
  57. base_model_id=info.get("base_model_id"),
  58. name=modelfile.modelfile.get("title"),
  59. meta=meta,
  60. params=json.dumps(info.get("params", {})),
  61. created_at=modelfile.timestamp,
  62. updated_at=modelfile.timestamp,
  63. )
  64. def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
  65. """Write your rollback migrations here."""
  66. recreate_modelfile_table(migrator, database)
  67. move_data_back_to_modelfile(migrator, database)
  68. migrator.remove_model("model")
  69. def recreate_modelfile_table(migrator: Migrator, database: pw.Database):
  70. query = """
  71. CREATE TABLE IF NOT EXISTS modelfile (
  72. user_id TEXT,
  73. tag_name TEXT,
  74. modelfile JSON,
  75. timestamp BIGINT
  76. )
  77. """
  78. migrator.sql(query)
  79. def move_data_back_to_modelfile(migrator: Migrator, database: pw.Database):
  80. Model = migrator.orm["model"]
  81. Modelfile = migrator.orm["modelfile"]
  82. models = Model.select()
  83. for model in models:
  84. # Extract and transform data in Python
  85. meta = json.loads(model.meta)
  86. modelfile_data = {
  87. "title": model.name,
  88. "desc": meta.get("description"),
  89. "imageUrl": meta.get("profile_image_url"),
  90. "content": meta.get("ollama", {}).get("modelfile"),
  91. "suggestionPrompts": meta.get("suggestion_prompts"),
  92. "categories": meta.get("categories"),
  93. "user": {k: v for k, v in meta.get("user", {}).items() if k != "community"},
  94. }
  95. # Insert the processed data back into the 'modelfile' table
  96. Modelfile.create(
  97. user_id=model.user_id,
  98. tag_name=model.id,
  99. modelfile=modelfile_data,
  100. timestamp=model.created_at,
  101. )