usage.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import dspy
  2. from prompt_migration.engine import PromptMigrationEngine, PromptTemplate
  3. from prompt_migration.evaluator import PromptEvaluator
  4. # Initialize LMs
  5. openai_lm = dspy.OpenAI(model="gpt-3.5-turbo")
  6. target_lm = dspy.HFModel(model="gpt2")
  7. # Create migration engine
  8. engine = PromptMigrationEngine(openai_lm, target_lm)
  9. # Define source prompt
  10. source_prompt = PromptTemplate(
  11. template="Summarize the following text: {text}",
  12. input_variables=["text"],
  13. model_type="openai"
  14. )
  15. # Example evaluation dataset
  16. eval_dataset = [
  17. {"text": "Example text 1", "expected_summary": "Summary 1"},
  18. {"text": "Example text 2", "expected_summary": "Summary 2"},
  19. ]
  20. # Migrate prompt
  21. migrated_prompt = engine.migrate_prompt(source_prompt, eval_dataset)
  22. # Evaluate migration
  23. evaluator = PromptEvaluator(openai_lm, target_lm)
  24. metrics = evaluator.evaluate(
  25. source_prompt.template,
  26. migrated_prompt.template,
  27. eval_dataset
  28. )
  29. print(f"Migrated prompt: {migrated_prompt.template}")
  30. print(f"Evaluation metrics: {metrics}")