Skip to content

Commit

Permalink
chat vision: url, local file, base64 encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
aramasethu committed Aug 2, 2024
1 parent ba9f531 commit ca4c9e5
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions fern/docs/pages/usingllms/chat_vision.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@

While sending a request to the Vision model PredictionGuard offers various options to upload your image. You can upload the image from using the url, a local image or base 64 encoded image.
Here is an example of how to use an image from a URL:

``` Python
import os
import json
from predictionguard import PredictionGuard

# Set your Prediction Guard token as an environmental variable.
os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"

client = PredictionGuard()

messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"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",
}
}
]
},
]

result = client.chat.completions.create(
model="llava-1.5-7b-hf",
messages=messages
)

print(json.dumps(
result,
sort_keys=True,
indent=4,
separators=(',', ': ')
))


```

This example shows how you can upload the image from a local file:

``` Python
import os
import json
from predictionguard import PredictionGuard

# Set your Prediction Guard token as an environmental variable.
os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>"

client = PredictionGuard()

messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "image_data/Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
}
}
]
},
]

result = client.chat.completions.create(
model="llava-1.5-7b-hf",
messages=messages
)

print(json.dumps(
result,
sort_keys=True,
indent=4,
separators=(',', ': ')
))


```

This example shows how you can chat with a base64 encoded image

Here is how you convert an image to base64 encoding
```Python
def encode_image_to_base64(image_path):
with open(image_path, 'rb') as image_file:
image_data = image_file.read()
base64_encoded_data = base64.b64encode(image_data)
base64_message = base64_encoded_data.decode('utf-8')
return base64_message

image_path = 'image_data/Gfp-wisconsin-madison-the-nature-boardwalk.jpg'
encoded_image = encode_image_to_base64(image_path)

```

and this is how you can use it with predictionguard:

```Python
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": encoded_image,
}
}
]
},
]

result = client.chat.completions.create(
model="llava-1.5-7b-hf",
messages=messages
)

print(json.dumps(
result,
sort_keys=True,
indent=4,
separators=(',', ': ')
))
```

The output of these will be similar to this:

```json
{
"choices": [
{
"index": 0,
"message": {
"content": "The image features a beautiful wooden path lined with green grass and a blue sky overhead. The pathway leads towards a body of water, creating a serene atmosphere. Along the path, there is a bench overlooking the pond, inviting to sit and relax. The scene also includes trees in the background, adding to the picturesque scenery.\nWith the combination of the peaceful atmosphere, the sunny blue sky, and the presence of water nearby, this image",
"output": null,
"role": "assistant"
},
"status": "success"
}
],
"created": 1722545890,
"id": "chat-xX9FDkWknG8G0ZHQjCgNdp47uBQZy",
"model": "llava-1.5-7b-hf",
"object": "chat_completion"
}
```

0 comments on commit ca4c9e5

Please sign in to comment.