From e449bf062d610c851cbdd3f66e986294f17a96c0 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 29 Aug 2024 16:12:14 -0700 Subject: [PATCH] add docs on pass thtough --- .../my-website/docs/pass_through/vertex_ai.md | 252 +++++++++++++++++- docs/my-website/docs/proxy/reliability.md | 1 - 2 files changed, 247 insertions(+), 6 deletions(-) diff --git a/docs/my-website/docs/pass_through/vertex_ai.md b/docs/my-website/docs/pass_through/vertex_ai.md index 61f47aab6d..1bf5558230 100644 --- a/docs/my-website/docs/pass_through/vertex_ai.md +++ b/docs/my-website/docs/pass_through/vertex_ai.md @@ -22,8 +22,51 @@ Looking for the Unified API (OpenAI format) for VertexAI ? [Go here - using vert - Tuning API - CountTokens API +## Authentication to Vertex AI + +LiteLLM Proxy Server supports two methods of authentication to Vertex AI: + +1. Pass Vertex Credetials client side to proxy server + +2. Set Vertex AI credentials on proxy server + ## Quick Start Usage + + + + +#### 1. Start litellm proxy + +```shell +litellm --config /path/to/config.yaml +``` + +#### 2. Test it + +```python +import vertexai +from vertexai.preview.generative_models import GenerativeModel + +LITE_LLM_ENDPOINT = "http://localhost:4000" + +vertexai.init( + project="", # enter your project id + location="", # enter your region + api_endpoint=f"{LITE_LLM_ENDPOINT}/vertex-ai", # route on litellm + api_transport="rest", +) + +model = GenerativeModel(model_name="gemini-1.0-pro") +model.generate_content("hi") + +``` + + + + + + #### 1. Set `default_vertex_config` on your `config.yaml` @@ -95,12 +138,43 @@ response = model.generate_content( print(response.text) ``` + + + + ## Usage Examples ### Gemini API (Generate Content) - + + +```python +import vertexai +from vertexai.generative_models import GenerativeModel + +LITELLM_PROXY_API_KEY = "sk-1234" +LITELLM_PROXY_BASE = "http://0.0.0.0:4000/vertex-ai" + +vertexai.init( + project="adroit-crow-413218", + location="us-central1", + api_endpoint=LITELLM_PROXY_BASE, + api_transport="rest", + +) + +model = GenerativeModel("gemini-1.5-flash-001") + +response = model.generate_content( + "What's a good name for a flower shop that specializes in selling bouquets of dried flowers?" +) + +print(response.text) +``` + + + ```python import vertexai @@ -171,7 +245,45 @@ curl http://localhost:4000/vertex-ai/publishers/google/models/gemini-1.5-flash-0 ### Embeddings API - + + + +```python +from typing import List, Optional +from vertexai.language_models import TextEmbeddingInput, TextEmbeddingModel +import vertexai +from vertexai.generative_models import GenerativeModel + +LITELLM_PROXY_API_KEY = "sk-1234" +LITELLM_PROXY_BASE = "http://0.0.0.0:4000/vertex-ai" + +import datetime + +vertexai.init( + project="adroit-crow-413218", + location="us-central1", + api_endpoint=LITELLM_PROXY_BASE, + api_transport="rest", +) + + +def embed_text( + texts: List[str] = ["banana muffins? ", "banana bread? banana muffins?"], + task: str = "RETRIEVAL_DOCUMENT", + model_name: str = "text-embedding-004", + dimensionality: Optional[int] = 256, +) -> List[List[float]]: + """Embeds texts with a pre-trained, foundational model.""" + model = TextEmbeddingModel.from_pretrained(model_name) + inputs = [TextEmbeddingInput(text, task) for text in texts] + kwargs = dict(output_dimensionality=dimensionality) if dimensionality else {} + embeddings = model.get_embeddings(inputs, **kwargs) + return [embedding.values for embedding in embeddings] +``` + + + + ```python from typing import List, Optional @@ -249,7 +361,54 @@ curl http://localhost:4000/vertex-ai/publishers/google/models/textembedding-geck ### Imagen API - + + + + +```python +from typing import List, Optional +from vertexai.preview.vision_models import ImageGenerationModel +import vertexai +from google.auth.credentials import Credentials + +LITELLM_PROXY_API_KEY = "sk-1234" +LITELLM_PROXY_BASE = "http://0.0.0.0:4000/vertex-ai" + +import datetime + +vertexai.init( + project="adroit-crow-413218", + location="us-central1", + api_endpoint=LITELLM_PROXY_BASE, + api_transport="rest", +) + +model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-001") + +images = model.generate_images( + prompt=prompt, + # Optional parameters + number_of_images=1, + language="en", + # You can't use a seed value and watermark at the same time. + # add_watermark=False, + # seed=100, + aspect_ratio="1:1", + safety_filter_level="block_some", + person_generation="allow_adult", +) + +images[0].save(location=output_file, include_generation_parameters=False) + +# Optional. View the generated image in a notebook. +# images[0].show() + +print(f"Created output image using {len(images[0]._image_bytes)} bytes") + +``` + + + ```python from typing import List, Optional @@ -338,7 +497,50 @@ curl http://localhost:4000/vertex-ai/publishers/google/models/imagen-3.0-generat - + + + +```python +from typing import List, Optional +from vertexai.generative_models import GenerativeModel +import vertexai + +LITELLM_PROXY_API_KEY = "sk-1234" +LITELLM_PROXY_BASE = "http://0.0.0.0:4000/vertex-ai" + +import datetime + +vertexai.init( + project="adroit-crow-413218", + location="us-central1", + api_endpoint=LITELLM_PROXY_BASE, + api_transport="rest", +) + + +model = GenerativeModel("gemini-1.5-flash-001") + +prompt = "Why is the sky blue?" + +# Prompt tokens count +response = model.count_tokens(prompt) +print(f"Prompt Token Count: {response.total_tokens}") +print(f"Prompt Character Count: {response.total_billable_characters}") + +# Send text to Gemini +response = model.generate_content(prompt) + +# Response tokens count +usage_metadata = response.usage_metadata +print(f"Prompt Token Count: {usage_metadata.prompt_token_count}") +print(f"Candidates Token Count: {usage_metadata.candidates_token_count}") +print(f"Total Token Count: {usage_metadata.total_token_count}") +``` + + + + + ```python from typing import List, Optional @@ -425,7 +627,47 @@ Create Fine Tuning Job - + + +```python +from typing import List, Optional +from vertexai.preview.tuning import sft +import vertexai + +LITELLM_PROXY_API_KEY = "sk-1234" +LITELLM_PROXY_BASE = "http://0.0.0.0:4000/vertex-ai" + + +vertexai.init( + project="adroit-crow-413218", + location="us-central1", + api_endpoint=LITELLM_PROXY_BASE, + api_transport="rest", +) + + +# TODO(developer): Update project +vertexai.init(project=PROJECT_ID, location="us-central1") + +sft_tuning_job = sft.train( + source_model="gemini-1.0-pro-002", + train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/sft_train_data.jsonl", +) + +# Polling for job completion +while not sft_tuning_job.has_ended: + time.sleep(60) + sft_tuning_job.refresh() + +print(sft_tuning_job.tuned_model_name) +print(sft_tuning_job.tuned_model_endpoint_name) +print(sft_tuning_job.experiment) + +``` + + + + ```python from typing import List, Optional diff --git a/docs/my-website/docs/proxy/reliability.md b/docs/my-website/docs/proxy/reliability.md index 7a2c65a90c..c048c6ddd4 100644 --- a/docs/my-website/docs/proxy/reliability.md +++ b/docs/my-website/docs/proxy/reliability.md @@ -283,7 +283,6 @@ litellm_settings: **Covers all errors (429, 500, etc.)** -[**See Code**]() **Set via config** ```yaml