vision.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """An example showing how to use Aphrodite to serve VLMs.
  2. Launch the Aphrodite server with the following command:
  3. aphrodite run llava-hf/llava-1.5-7b-hf \
  4. --chat-template template_llava.jinja
  5. """
  6. import base64
  7. import os
  8. from openai import OpenAI
  9. # Modify OpenAI's API key and API base to use Aphrodite's API server.
  10. openai_api_key = "EMPTY"
  11. openai_api_base = "http://localhost:2242/v1"
  12. client = OpenAI(
  13. # defaults to os.environ.get("OPENAI_API_KEY")
  14. api_key=openai_api_key,
  15. base_url=openai_api_base,
  16. )
  17. models = client.models.list()
  18. model = models.data[0].id
  19. def encode_image_base64_from_file(image_path: str) -> str:
  20. """Encode an image from a local file to base64 format."""
  21. with open(image_path, 'rb') as image_file:
  22. result = base64.b64encode(image_file.read()).decode('utf-8')
  23. return result
  24. image_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
  25. "../vision/burg.jpg")
  26. image_base64 = encode_image_base64_from_file(image_path=image_path)
  27. chat_completion_from_base64 = client.chat.completions.create(
  28. messages=[{
  29. "role":
  30. "user",
  31. "content": [
  32. {
  33. "type": "text",
  34. "text": "What's in this image?"
  35. },
  36. {
  37. "type": "image_url",
  38. "image_url": {
  39. "url": f"data:image/jpeg;base64,{image_base64}"
  40. },
  41. },
  42. ],
  43. }],
  44. model=model,
  45. )
  46. result = chat_completion_from_base64.choices[0].message.content
  47. print(f"Chat completion output:{result}")