From ca4c9e5ba95d70739f1edf617211e6fce4f36ffb Mon Sep 17 00:00:00 2001 From: aramasethu Date: Fri, 2 Aug 2024 12:49:31 -0400 Subject: [PATCH] chat vision: url, local file, base64 encoded --- fern/docs/pages/usingllms/chat_vision.mdx | 163 ++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 fern/docs/pages/usingllms/chat_vision.mdx diff --git a/fern/docs/pages/usingllms/chat_vision.mdx b/fern/docs/pages/usingllms/chat_vision.mdx new file mode 100644 index 0000000..25a7068 --- /dev/null +++ b/fern/docs/pages/usingllms/chat_vision.mdx @@ -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"] = "" + +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"] = "" + +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" +} +``` \ No newline at end of file