Reference

Chat Vision

You can get vision text completions from any of the vision enabled models using the /chat/completions REST API endpoint or any of the official SDKs (Python, Go, Rust, JS, or cURL).

Generate a Vision Text Completion

To generate a vision text completion, you can use the following code examples. Depending on your preference or requirements, select the appropriate method for your application. Streaming is not currently supported when generating a vision text completion. For image inputs, this endpoint supports image file, url, and base 64 encoded inputs in the python client, while the Go API only supports images that are base64 encoded represented by a data uri.

1import os
2import json
3
4from predictionguard import PredictionGuard
5
6# Set your Prediction Guard token as an environmental variable.
7os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"
8
9client = PredictionGuard()
10
11messages = [
12 {
13 "role": "user",
14 "content": [
15 {
16 "type": "text",
17 "text": "What's in this image?"
18 },
19 {
20 "type": "image_url",
21 "image_url": {
22 "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",
23 }
24 }
25 ]
26 },
27]
28
29result = client.chat.completions.create(
30 model="llava-1.5-7b-hf",
31 messages=messages
32)
33
34print(json.dumps(
35 result,
36 sort_keys=True,
37 indent=4,
38 separators=(',', ': ')
39))

The output will look something like:

1{
2 "id":"chat-xQNsuexunK7zeMjlW25g99hax9qo4",
3 "object":"chat_completion",
4 "created":1717784541,
5 "model":"llava-1.5-7b-hf",
6 "choices":[
7 {
8 "index":0,
9 "message":{
10 "role":"assistant",
11 "content":"The image features a peaceful scene of a wooden pathway traveling through an open grassy field. The pathway seems to be leading to an even more remote and expansive meadow, providing a serene and picturesque view. The surroundings consist of lush green grass and trees, creating a sense of tranquility in the landscape.",
12 "output":null
13 },
14 "status":"success"
15 }
16 ]
17}

This approach presents a straightforward way for readers to choose and apply the code example that best suits their needs for generating text completions using either Python, Go, Rust, JS, or cURL.