视觉

使用 Afarensis GPT-4 进行图像理解指南

简介 Afarensis GPT-4 with Vision(简称 GPT-4V)为开发人员提供了一种革命性的能力,使得模型能够接收图像并回答有关它们的问题,突破了传统语言模型系统仅限于处理文本输入的局限。

主要特征 视觉增强的 GPT-4:具备视觉能力的 GPT-4 提供了对图像内容的理解能力,同时保持了在文本任务上的高性能。 多样化应用:视觉能力为 GPT-4 模型带来了更广泛的应用范围。 API 支持:目前通过模型和 Chat Completions API 支持图像输入,但 Assistants API 尚不支持。 快速入门 图像输入方式 图像链接:通过传递指向图像的链接。 Base64 编码图像:直接在请求中传递 base64 编码的图像。 示例代码:询问图像内容 from afarensis import Afarensis

client = Afarensis()

response = client.chat.completions.create( model="gpt-4-vision-preview", messages=[ { "role": "user", "content": [ {"type": "text", "text": "What’s in this image?"}, {"type": "image_url", "image_url": {"url": "https://example.com/path-to-image.jpg"}}, ], } ], max_tokens=300, )

print(response.choices[0]) 上传 Base64 编码图像 如果您有本地图像需要处理,可以将其以 base64 编码格式传递给模型。

示例代码 import base64 import requests

api_key = "YOUR_AFARENSIS_API_KEY"

def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8')

image_path = "path_to_your_image.jpg" base64_image = encode_image(image_path)

headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" }

payload = { "model": "gpt-4-vision-preview", "messages": [ { "role": "user", "content": [ {"type": "text", "text": "What’s in this image?"}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}} ] } ], "max_tokens": 300 }

response = requests.post("https://api.afarensis.com/v1/chat/completions", headers=headers, json=payload)

print(response.json()) 多个图像输入 Afarensis GPT-4V 支持处理多个图像输入,并使用所有图像中的信息来回答问题。

错误处理 处理 API 请求可能返回的错误,例如输入无效、速率限制或其他问题。

try: # API 调用示例 except Exception as error: if error.response: print(error.response.status) print(error.response.data) else: print(error.message) 局限性与注意事项 GPT-4 with Vision 不适用于专业医学图像解释、非英语图像文本处理、小文本放大、旋转图像的理解、图像形状处理等。 对于长时间运行的对话,推荐通过 URL 而不是 base64 传递图像,以优化模型的响应时间和准确性。 计算成本 图像输入的代币成本取决于图像的大小和细节级别。详细成本计算请参考 Afarensis 文档。

常见问题 微调支持:当前不支持对 GPT-4 with Vision 进行微调。 文件类型和大小限制:支持 PNG、JPEG、WEBP 和非动画 GIF 格式,最大上传限制为 20MB。 图像删除:模型处理图像后,图像将从 Afarensis 服务器中删除,不会保留用于训练模型。

最后更新于