|
1 | 1 | { |
2 | 2 | "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<div>\n", |
| 8 | + " <img src=\"https://user-images.githubusercontent.com/1991296/230134379-7181e485-c521-4d23-a0d6-f7b3b61ba524.png\" width=\"500\"/>\n", |
| 9 | + "</div>" |
| 10 | + ] |
| 11 | + }, |
3 | 12 | { |
4 | 13 | "cell_type": "code", |
5 | 14 | "execution_count": 1, |
|
9 | 18 | "name": "stdout", |
10 | 19 | "output_type": "stream", |
11 | 20 | "text": [ |
12 | | - "ChatCompletion(id='chatcmpl-65a710ba-41d1-4d0a-a124-a44b2b4a0189', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=' The image reads \"LlamaC++.\"', role='assistant', function_call=None, tool_calls=None))], created=1699413274, model='gpt-4-vision-preview', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=10, prompt_tokens=624, total_tokens=634))\n" |
| 21 | + "{'text': 'Llama C++'}\n" |
13 | 22 | ] |
14 | 23 | } |
15 | 24 | ], |
16 | 25 | "source": [ |
17 | 26 | "from openai import OpenAI\n", |
18 | 27 | "\n", |
19 | | - "import urllib.request\n", |
20 | | - "import base64\n", |
21 | | - "\n", |
22 | | - "def get_data_url(url):\n", |
23 | | - " return \"data:image/png;base64,\" + base64.b64encode(urllib.request.urlopen(url).read()).decode(\"utf-8\")\n", |
24 | | - "\n", |
25 | 28 | "client = OpenAI(base_url=\"http://100.64.159.73:8000/v1\", api_key=\"sk-1234\")\n", |
26 | 29 | "response = client.chat.completions.create(\n", |
27 | 30 | " model=\"gpt-4-vision-preview\",\n", |
|
32 | 35 | " {\n", |
33 | 36 | " \"type\": \"image_url\",\n", |
34 | 37 | " \"image_url\": {\n", |
35 | | - " \"url\": get_data_url(\"https://user-images.githubusercontent.com/1991296/230134379-7181e485-c521-4d23-a0d6-f7b3b61ba524.png\"),\n", |
36 | | - " # \"url\": \"https://user-images.githubusercontent.com/1991296/230134379-7181e485-c521-4d23-a0d6-f7b3b61ba524.png\",\n", |
| 38 | + " \"url\": \"https://user-images.githubusercontent.com/1991296/230134379-7181e485-c521-4d23-a0d6-f7b3b61ba524.png\",\n", |
37 | 39 | " },\n", |
38 | 40 | " },\n", |
39 | | - " {\"type\": \"text\", \"text\": \"What does the image say\"},\n", |
| 41 | + " {\"type\": \"text\", \"text\": \"What does the image say. Format your response as a json object with a single 'text' key.\"},\n", |
40 | 42 | " ],\n", |
41 | 43 | " }\n", |
42 | 44 | " ],\n", |
| 45 | + " response_format={ \"type\": \"json_object\" }\n", |
43 | 46 | ")\n", |
44 | | - "print(response)" |
45 | | - ] |
46 | | - }, |
47 | | - { |
48 | | - "cell_type": "markdown", |
49 | | - "metadata": {}, |
50 | | - "source": [ |
51 | | - "" |
| 47 | + "import json\n", |
| 48 | + "print(json.loads(response.choices[0].message.content))" |
52 | 49 | ] |
53 | 50 | }, |
54 | 51 | { |
|
0 commit comments