test_run_batch.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import subprocess
  2. import sys
  3. import tempfile
  4. from aphrodite.endpoints.openai.protocol import BatchRequestOutput
  5. # ruff: noqa: E501
  6. INPUT_BATCH = """{"custom_id": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "NousResearch/Meta-Llama-3-8B-Instruct", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
  7. {"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "NousResearch/Meta-Llama-3-8B-Instruct", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
  8. {"custom_id": "request-3", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "NonExistModel", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}"""
  9. INVALID_INPUT_BATCH = """{"invalid_field": "request-1", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "NousResearch/Meta-Llama-3-8B-Instruct", "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}
  10. {"custom_id": "request-2", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "NousResearch/Meta-Llama-3-8B-Instruct", "messages": [{"role": "system", "content": "You are an unhelpful assistant."},{"role": "user", "content": "Hello world!"}],"max_tokens": 1000}}"""
  11. INPUT_EMBEDDING_BATCH = """{"custom_id": "request-1", "method": "POST", "url": "/v1/embeddings", "body": {"model": "intfloat/e5-mistral-7b-instruct", "input": "You are a helpful assistant."}}
  12. {"custom_id": "request-2", "method": "POST", "url": "/v1/embeddings", "body": {"model": "intfloat/e5-mistral-7b-instruct", "input": "You are an unhelpful assistant."}}
  13. {"custom_id": "request-3", "method": "POST", "url": "/v1/embeddings", "body": {"model": "intfloat/e5-mistral-7b-instruct", "input": "Hello world!"}}
  14. {"custom_id": "request-4", "method": "POST", "url": "/v1/embeddings", "body": {"model": "NonExistModel", "input": "Hello world!"}}"""
  15. def test_empty_file():
  16. with tempfile.NamedTemporaryFile(
  17. "w") as input_file, tempfile.NamedTemporaryFile(
  18. "r") as output_file:
  19. input_file.write("")
  20. input_file.flush()
  21. proc = subprocess.Popen([
  22. sys.executable, "-m", "aphrodite.endpoints.openai.run_batch", "-i",
  23. input_file.name, "-o", output_file.name, "--model",
  24. "intfloat/e5-mistral-7b-instruct"
  25. ], )
  26. proc.communicate()
  27. proc.wait()
  28. assert proc.returncode == 0, f"{proc=}"
  29. contents = output_file.read()
  30. assert contents.strip() == ""
  31. def test_completions():
  32. with tempfile.NamedTemporaryFile(
  33. "w") as input_file, tempfile.NamedTemporaryFile(
  34. "r") as output_file:
  35. input_file.write(INPUT_BATCH)
  36. input_file.flush()
  37. proc = subprocess.Popen([
  38. sys.executable, "-m", "aphrodite.endpoints.openai.run_batch", "-i",
  39. input_file.name, "-o", output_file.name, "--model",
  40. "NousResearch/Meta-Llama-3-8B-Instruct"
  41. ], )
  42. proc.communicate()
  43. proc.wait()
  44. assert proc.returncode == 0, f"{proc=}"
  45. contents = output_file.read()
  46. for line in contents.strip().split("\n"):
  47. # Ensure that the output format conforms to the openai api.
  48. # Validation should throw if the schema is wrong.
  49. BatchRequestOutput.model_validate_json(line)
  50. def test_completions_invalid_input():
  51. """
  52. Ensure that we fail when the input doesn't conform to the openai api.
  53. """
  54. with tempfile.NamedTemporaryFile(
  55. "w") as input_file, tempfile.NamedTemporaryFile(
  56. "r") as output_file:
  57. input_file.write(INVALID_INPUT_BATCH)
  58. input_file.flush()
  59. proc = subprocess.Popen([
  60. sys.executable, "-m", "aphrodite.endpoints.openai.run_batch", "-i",
  61. input_file.name, "-o", output_file.name, "--model",
  62. "NousResearch/Meta-Llama-3-8B-Instruct"
  63. ], )
  64. proc.communicate()
  65. proc.wait()
  66. assert proc.returncode != 0, f"{proc=}"
  67. def test_embeddings():
  68. with tempfile.NamedTemporaryFile(
  69. "w") as input_file, tempfile.NamedTemporaryFile(
  70. "r") as output_file:
  71. input_file.write(INPUT_EMBEDDING_BATCH)
  72. input_file.flush()
  73. proc = subprocess.Popen([
  74. sys.executable, "-m", "aphrodite.endpoints.openai.run_batch", "-i",
  75. input_file.name, "-o", output_file.name, "--model",
  76. "intfloat/e5-mistral-7b-instruct"
  77. ], )
  78. proc.communicate()
  79. proc.wait()
  80. assert proc.returncode == 0, f"{proc=}"
  81. contents = output_file.read()
  82. for line in contents.strip().split("\n"):
  83. # Ensure that the output format conforms to the openai api.
  84. # Validation should throw if the schema is wrong.
  85. BatchRequestOutput.model_validate_json(line)