OpenAI Audio API Client

Source vllm-project/vllm.

  1. 1"""An example showing how to use vLLM to serve VLMs.
  2. 2
  3. 3Launch the vLLM server with the following command:
  4. 4vllm serve fixie-ai/ultravox-v0_3
  5. 5"""
  6. 6import base64
  7. 7
  8. 8import requests
  9. 9from openai import OpenAI
  10. 10
  11. 11from vllm.assets.audio import AudioAsset
  12. 12
  13. 13# Modify OpenAI's API key and API base to use vLLM's API server.
  14. 14openai_api_key = "EMPTY"
  15. 15openai_api_base = "http://localhost:8000/v1"
  16. 16
  17. 17client = OpenAI(
  18. 18 # defaults to os.environ.get("OPENAI_API_KEY")
  19. 19 api_key=openai_api_key,
  20. 20 base_url=openai_api_base,
  21. 21)
  22. 22
  23. 23models = client.models.list()
  24. 24model = models.data[0].id
  25. 25
  26. 26# Any format supported by librosa is supported
  27. 27audio_url = AudioAsset("winning_call").url
  28. 28
  29. 29# Use audio url in the payload
  30. 30chat_completion_from_url = client.chat.completions.create(
  31. 31 messages=[{
  32. 32 "role":
  33. 33 "user",
  34. 34 "content": [
  35. 35 {
  36. 36 "type": "text",
  37. 37 "text": "What's in this audio?"
  38. 38 },
  39. 39 {
  40. 40 "type": "audio_url",
  41. 41 "audio_url": {
  42. 42 "url": audio_url
  43. 43 },
  44. 44 },
  45. 45 ],
  46. 46 }],
  47. 47 model=model,
  48. 48 max_tokens=64,
  49. 49)
  50. 50
  51. 51result = chat_completion_from_url.choices[0].message.content
  52. 52print(f"Chat completion output:{result}")
  53. 53
  54. 54
  55. 55# Use base64 encoded audio in the payload
  56. 56def encode_audio_base64_from_url(audio_url: str) -> str:
  57. 57 """Encode an audio retrieved from a remote url to base64 format."""
  58. 58
  59. 59 with requests.get(audio_url) as response:
  60. 60 response.raise_for_status()
  61. 61 result = base64.b64encode(response.content).decode('utf-8')
  62. 62
  63. 63 return result
  64. 64
  65. 65
  66. 66audio_base64 = encode_audio_base64_from_url(audio_url=audio_url)
  67. 67chat_completion_from_base64 = client.chat.completions.create(
  68. 68 messages=[{
  69. 69 "role":
  70. 70 "user",
  71. 71 "content": [
  72. 72 {
  73. 73 "type": "text",
  74. 74 "text": "What's in this audio?"
  75. 75 },
  76. 76 {
  77. 77 "type": "audio_url",
  78. 78 "audio_url": {
  79. 79 # Any format supported by librosa is supported
  80. 80 "url": f"data:audio/ogg;base64,{audio_base64}"
  81. 81 },
  82. 82 },
  83. 83 ],
  84. 84 }],
  85. 85 model=model,
  86. 86 max_tokens=64,
  87. 87)
  88. 88
  89. 89result = chat_completion_from_base64.choices[0].message.content
  90. 90print(f"Chat completion output:{result}")