Skip to content

Commit

Permalink
[OPTIMIZED API]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Mar 19, 2024
1 parent 4263ec3 commit b41fe30
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 31 deletions.
71 changes: 43 additions & 28 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
import requests
import asyncio
import base64
from PIL import Image
from io import BytesIO

import httpx
from httpx import Timeout
from PIL import Image


# Convert image to Base64
def image_to_base64(image_path):
async def image_to_base64(image_path):
with Image.open(image_path) as image:
buffered = BytesIO()
image.save(buffered, format="JPEG")
image.save(buffered, format="JPEG", quality=85)
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str


# Replace 'image.jpg' with the path to your image
base64_image = image_to_base64("test.jpg")
text_data = {"type": "text", "text": "Describe what is in the image"}
image_data = {
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
}

# Construct the request data
request_data = {
"model": "cogvlm-chat-17b",
"messages": [{"role": "user", "content": [text_data, image_data]}],
"temperature": 0.8,
"top_p": 0.9,
"max_tokens": 1024,
}

# Specify the URL of your FastAPI application
url = "https://api.swarms.world/v1/chat/completions"

# Send the request
response = requests.post(url, json=request_data)
# Print the response from the server
print(response.text)
async def send_request(base64_image):
text_data = {"type": "text", "text": "Describe what is in the image"}
image_data = {
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
}

# Construct the request data
request_data = {
"model": "cogvlm-chat-17b",
"messages": [{"role": "user", "content": [text_data, image_data]}],
"temperature": 0.8,
"top_p": 0.9,
"max_tokens": 1024,
}

# Specify the URL of your FastAPI application
url = "https://api.swarms.world/v1/chat/completions"

# Timeout
timeout = Timeout(10.0, read=30.0)

# Use httpx.AsyncClient for asynchronous requests
async with httpx.AsyncClient(timeout=timeout, http2=True) as client:
response = await client.post(url, json=request_data)
print(response.text)


async def main():
# Replace 'image.jpg' with the path to your image
base64_image = await image_to_base64("test.jpg")
await send_request(base64_image)


if __name__ == "__main__":
asyncio.run(main())
16 changes: 16 additions & 0 deletions openai_api_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import openai

# Set the API key
client = openai.OpenAI(
base_url="https://api.swarms.world", api_key="sk-1g3Z3y2c1f2z3d4b5p6w7c8b9v0n1m2"
)

# Create a client object to interact with the OpenAI API
# Make a chat completion request
chat_completion = client.chat.completions.create(
model="cogvlm-chat-17b",
messages=[{"role": "user", "content": "Write me a love song"}],
temperature=0.7,
)

# Send a message to the chat model and get a completion response
Binary file added test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions tests/profiling/test_cogvlm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import pytest
import requests
from PIL import Image
import os
import os


img = os.environ('TEST_IMG')

img = os.environ("TEST_IMG")


# Utility function to convert image to Base64
Expand Down

0 comments on commit b41fe30

Please sign in to comment.