From 323add2e67ca22ddca577cb05ac690bc9c0e1c8f Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 8 Jul 2024 10:16:41 -0700 Subject: [PATCH] docs(vertex.md): add doc on passing base64 pdf to vertex ai --- docs/my-website/docs/providers/vertex.md | 70 +++++++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/docs/my-website/docs/providers/vertex.md b/docs/my-website/docs/providers/vertex.md index ce9e73bab1..7872f42d0f 100644 --- a/docs/my-website/docs/providers/vertex.md +++ b/docs/my-website/docs/providers/vertex.md @@ -825,9 +825,11 @@ assert isinstance( Pass any file supported by Vertex AI, through LiteLLM. + +### **Using `gs://`** ```python from litellm import completion @@ -840,7 +842,7 @@ response = completion( {"type": "text", "text": "You are a very professional document summarization specialist. Please summarize the given document."}, { "type": "image_url", - "image_url": "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", + "image_url": "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", # 👈 PDF }, ], } @@ -849,7 +851,41 @@ response = completion( ) print(response.choices[0]) +``` +### **using base64** +```python +from litellm import completion +import base64 +import requests + +# URL of the file +url = "https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/2403.05530.pdf" + +# Download the file +response = requests.get(url) +file_data = response.content + +encoded_file = base64.b64encode(file_data).decode("utf-8") + +response = completion( + model="vertex_ai/gemini-1.5-flash", + messages=[ + { + "role": "user", + "content": [ + {"type": "text", "text": "You are a very professional document summarization specialist. Please summarize the given document."}, + { + "type": "image_url", + "image_url": f"data:application/pdf;base64,{encoded_file}", # 👈 PDF + }, + ], + } + ], + max_tokens=300, +) + +print(response.choices[0]) ``` @@ -871,6 +907,7 @@ litellm --config /path/to/config.yaml 3. Test it! +**Using `gs://`** ```bash curl http://0.0.0.0:4000/v1/chat/completions \ -H "Content-Type: application/json" \ @@ -887,8 +924,8 @@ curl http://0.0.0.0:4000/v1/chat/completions \ }, { "type": "image_url", - "image_url": "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf", - }, + "image_url": "gs://cloud-samples-data/generative-ai/pdf/2403.05530.pdf" # 👈 PDF + } } ] } @@ -898,6 +935,33 @@ curl http://0.0.0.0:4000/v1/chat/completions \ ``` + +```bash +curl http://0.0.0.0:4000/v1/chat/completions \ + -H "Content-Type: application/json" \ + -H "Authorization: Bearer " \ + -d '{ + "model": "gemini-1.5-flash", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": "You are a very professional document summarization specialist. Please summarize the given document" + }, + { + "type": "image_url", + "image_url": "data:application/pdf;base64,{encoded_file}" # 👈 PDF + } + } + ] + } + ], + "max_tokens": 300 + }' + +```