|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "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" |
| 13 | + ] |
| 14 | + } |
| 15 | + ], |
| 16 | + "source": [ |
| 17 | + "from openai import OpenAI\n", |
| 18 | + "\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 | + "client = OpenAI(base_url=\"http://100.64.159.73:8000/v1\", api_key=\"sk-1234\")\n", |
| 26 | + "response = client.chat.completions.create(\n", |
| 27 | + " model=\"gpt-4-vision-preview\",\n", |
| 28 | + " messages=[\n", |
| 29 | + " {\n", |
| 30 | + " \"role\": \"user\",\n", |
| 31 | + " \"content\": [\n", |
| 32 | + " {\n", |
| 33 | + " \"type\": \"image_url\",\n", |
| 34 | + " \"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", |
| 37 | + " },\n", |
| 38 | + " },\n", |
| 39 | + " {\"type\": \"text\", \"text\": \"What does the image say\"},\n", |
| 40 | + " ],\n", |
| 41 | + " }\n", |
| 42 | + " ],\n", |
| 43 | + ")\n", |
| 44 | + "print(response)" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [] |
| 60 | + } |
| 61 | + ], |
| 62 | + "metadata": { |
| 63 | + "kernelspec": { |
| 64 | + "display_name": ".venv", |
| 65 | + "language": "python", |
| 66 | + "name": "python3" |
| 67 | + }, |
| 68 | + "language_info": { |
| 69 | + "codemirror_mode": { |
| 70 | + "name": "ipython", |
| 71 | + "version": 3 |
| 72 | + }, |
| 73 | + "file_extension": ".py", |
| 74 | + "mimetype": "text/x-python", |
| 75 | + "name": "python", |
| 76 | + "nbconvert_exporter": "python", |
| 77 | + "pygments_lexer": "ipython3", |
| 78 | + "version": "3.11.5+" |
| 79 | + }, |
| 80 | + "orig_nbformat": 4 |
| 81 | + }, |
| 82 | + "nbformat": 4, |
| 83 | + "nbformat_minor": 2 |
| 84 | +} |
0 commit comments