How to input images in a CryptoTalks chat
CryptoTalks supports image input for models with vision capabilities, such as GPT-4 Vision. This guide will show you how to include images in your chat messages.
Prerequisites
- An active CryptoTalks account with sufficient funds
- Access to a model that supports image input (e.g., openai/gpt-4-vision-preview)
Steps to include an image in your chat
- Encode your image to base64 format
- Prepare your chat message with the encoded image
- Send the request to the CryptoTalks API
Step 1: Encode your image to base64
First, you need to convert your image to a base64-encoded string. Here's a Python function to do this:
import base64
def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
encoded_image = encode_image_to_base64("/path/to/your/image.jpg")
Step 2: Prepare your chat message
When including an image, your message should have a specific format. Here's an example:
chat_data = {
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{encoded_image}",
},
],
}
],
"model": "openai/gpt-4-vision-preview",
"stream": False,
}
Step 3: Send the request
Now you can send this data to the CryptoTalks API. Here's an example using the requests library in Python:
import requests
url = "https://cryptotalks.ai/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer YOUR_API_KEY_HERE"
}
response = requests.post(url, json=chat_data, headers=headers)
print(response.json())
Or you can use the OpenAI Python client library to send the request:
import base64
from openai import OpenAI
def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
encoded_image = encode_image_to_base64("/path/to/your/image.jpg")
client = OpenAI(
base_url="https://cryptotalks.ai/v1",
api_key="YOUR_API_TOKEN"
)
completion = client.chat.completions.create(
model="openai/gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": f"data:image/jpeg;base64,{encoded_image}",
},
],
}
],
)
print(completion.choices[0].message.content)