Skip to content

Commit e8155f4

Browse files
authored
Merge pull request #362 from alexrudall/add-vision
Add Vision example to README
2 parents 1ec0bb8 + 36fc682 commit e8155f4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ puts response.dig("choices", 0, "message", "content")
174174
# => "Hello! How may I assist you today?"
175175
```
176176

177-
### Streaming Chat
177+
#### Streaming Chat
178178

179179
[Quick guide to streaming Chat with Rails 7 and Hotwire](https://gist.github.com/alexrudall/cb5ee1e109353ef358adb4e66631799d)
180180

@@ -195,6 +195,28 @@ client.chat(
195195

196196
Note: OpenAPI currently does not report token usage for streaming responses. To count tokens while streaming, try `OpenAI.rough_token_count` or [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby). We think that each call to the stream proc corresponds to a single token, so you can also try counting the number of calls to the proc to get the completion token count.
197197

198+
#### Vision
199+
200+
You can use the GPT-4 Vision model to generate a description of an image:
201+
202+
```ruby
203+
messages = [
204+
{ "type": "text", "text": "What’s in this image?"},
205+
{ "type": "image_url",
206+
"image_url": {
207+
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
208+
},
209+
}
210+
]
211+
response = client.chat(
212+
parameters: {
213+
model: "gpt-4-vision-preview", # Required.
214+
messages: [{ role: "user", content: messages}], # Required.
215+
})
216+
puts response.dig("choices", 0, "message", "content")
217+
# => "The image depicts a serene natural landscape featuring a long wooden boardwalk extending straight ahead"
218+
```
219+
198220
### Functions
199221

200222
You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call those them. For example, if you want the model to use your method `get_current_weather` to get the current weather in a given location:
@@ -438,6 +460,8 @@ puts response["text"]
438460
# => "Transcription of the text"
439461
```
440462

463+
#### Vision
464+
441465
#### Errors
442466

443467
HTTP errors can be caught like this:

0 commit comments

Comments
 (0)