benchmark_prompts.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import time
  2. import ollama
  3. model = "llama3.2:3b"
  4. # Old prompt
  5. sys_prompt_old = """You are a helpful medical data analyst AI.
  6. Health profile: Condition: Pregnancy.
  7. Act as a specialized clinical dietitian. Provide a direct answer. Skip all thinking, reasoning, and pleasantries.
  8. Local Database Context: No database records found.
  9. """
  10. # New prompt
  11. sys_prompt_new = """You are a helpful medical data analyst AI.
  12. Health profile: Condition: Pregnancy.
  13. Act as a specialized clinical dietitian. Provide a direct answer. Use Chain of Thought reasoning, and skip pleasantries.
  14. Local Database Context: No database records found.
  15. """
  16. user_query = "Can I eat unpasteurized cheese during pregnancy?"
  17. print("--- RUNNING BENCHMARK ON SERVER ---")
  18. # Run old prompt
  19. print("\\n[Old Prompt] - Skip all thinking and reasoning...")
  20. t0 = time.time()
  21. resp_old = ollama.chat(model=model, messages=[
  22. {"role": "system", "content": sys_prompt_old},
  23. {"role": "user", "content": user_query}
  24. ])
  25. t1 = time.time()
  26. elapsed_old = t1 - t0
  27. content_old = resp_old['message']['content']
  28. token_count_old = resp_old.get('eval_count', len(content_old.split()))
  29. print(f"Time Taken: {elapsed_old:.4f} seconds")
  30. print(f"Response length (chars): {len(content_old)}")
  31. print(f"Generated tokens: {token_count_old}")
  32. print(f"Speed: {token_count_old / elapsed_old:.2f} tokens/sec")
  33. print("Response preview:")
  34. print(content_old[:300] + "...")
  35. # Run new prompt
  36. print("\\n[New Prompt] - Chain of Thought...")
  37. t2 = time.time()
  38. resp_new = ollama.chat(model=model, messages=[
  39. {"role": "system", "content": sys_prompt_new},
  40. {"role": "user", "content": user_query}
  41. ])
  42. t3 = time.time()
  43. elapsed_new = t3 - t2
  44. content_new = resp_new['message']['content']
  45. token_count_new = resp_new.get('eval_count', len(content_new.split()))
  46. print(f"Time Taken: {elapsed_new:.4f} seconds")
  47. print(f"Response length (chars): {len(content_new)}")
  48. print(f"Generated tokens: {token_count_new}")
  49. print(f"Speed: {token_count_new / elapsed_new:.2f} tokens/sec")
  50. print("Response preview:")
  51. print(content_new[:300] + "...")
  52. diff_time = elapsed_new - elapsed_old
  53. pct_change = (diff_time / elapsed_old) * 100
  54. print(f"\\n--- SUMMARY ---")
  55. print(f"Old Prompt time: {elapsed_old:.2f}s")
  56. print(f"New Prompt time: {elapsed_new:.2f}s")
  57. print(f"Difference: {diff_time:+.2f}s ({pct_change:+.1f}%)")