|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def generate_content() -> str: |
| 17 | + # [START googlegenaisdk_tools_urlcontext_with_txt] |
| 18 | + from google import genai |
| 19 | + from google.genai.types import Tool, GenerateContentConfig, HttpOptions, UrlContext |
| 20 | + |
| 21 | + client = genai.Client(http_options=HttpOptions(api_version="v1")) |
| 22 | + model_id = "gemini-2.5-flash" |
| 23 | + |
| 24 | + url_context_tool = Tool( |
| 25 | + url_context=UrlContext |
| 26 | + ) |
| 27 | + |
| 28 | + # TODO(developer): Here put your URLs |
| 29 | + url1 = "https://cloud.google.com/vertex-ai/docs/generative-ai/start" |
| 30 | + url2 = "https://cloud.google.com/docs/overview" |
| 31 | + |
| 32 | + response = client.models.generate_content( |
| 33 | + model=model_id, |
| 34 | + contents=f"Compare the content, purpose, and audiences of {url1} and {url2}.", |
| 35 | + config=GenerateContentConfig( |
| 36 | + tools=[url_context_tool], |
| 37 | + response_modalities=["TEXT"], |
| 38 | + ) |
| 39 | + ) |
| 40 | + |
| 41 | + for each in response.candidates[0].content.parts: |
| 42 | + print(each.text) |
| 43 | + # Gemini 2.5 Pro and Gemini 2.5 Flash are both advanced models offered by Google AI, but they are optimized for different use cases. |
| 44 | + # |
| 45 | + # Here's a comparison: |
| 46 | + # |
| 47 | + # **Gemini 2.5 Pro** |
| 48 | + # * **Description**: This is Google's most advanced model, described as a "state-of-the-art thinking model". It excels at reasoning over complex problems in areas like code, mathematics, and STEM, and can analyze large datasets, codebases, and documents using a long context window. |
| 49 | + # * **Input Data Types**: It supports audio, images, video, text, and PDF inputs. |
| 50 | + # * **Output Data Types**: It produces text outputs. |
| 51 | + # * **Token Limits**: It has an input token limit of 1,048,576 and an output token limit of 65,536. |
| 52 | + # * **Supported Capabilities**: Gemini 2.5 Pro supports Batch API, Caching, Code execution, Function calling, Search grounding, Structured outputs, Thinking, and URL context. |
| 53 | + # * **Knowledge Cutoff**: January 2025. |
| 54 | + # |
| 55 | + # **Gemini 2.5 Flash** |
| 56 | + # * **Description**: Positioned as "fast and intelligent," Gemini 2.5 Flash is highlighted as Google's best model in terms of price-performance, offering well-rounded capabilities. It is ideal for large-scale processing, low-latency, high-volume tasks that require thinking, and agentic use cases. |
| 57 | + # * **Input Data Types**: It supports text, images, video, and audio inputs. |
| 58 | + # * **Output Data Types**: It produces text outputs. |
| 59 | + # * **Token Limits**: Similar to Pro, it has an input token limit of 1,048,576 and an output token limit of 65,536. |
| 60 | + # * **Supported Capabilities**: Gemini 2.5 Flash supports Batch API, Caching, Code execution, Function calling, Search grounding, Structured outputs, Thinking, and URL context. |
| 61 | + # * **Knowledge Cutoff**: January 2025. |
| 62 | + # |
| 63 | + # **Key Differences and Similarities:** |
| 64 | + # |
| 65 | + # * **Primary Focus**: Gemini 2.5 Pro is geared towards advanced reasoning and in-depth analysis of complex problems and large documents. Gemini 2.5 Flash, on the other hand, is optimized for efficiency, scale, and high-volume, low-latency applications, making it a strong choice for price-performance sensitive scenarios. |
| 66 | + # * **Input Modalities**: Both models handle various input types including text, images, video, and audio. Gemini 2.5 Pro explicitly lists PDF as an input type, while Gemini 2.5 Flash lists text, images, video, audio. |
| 67 | + # * **Technical Specifications (for primary stable versions)**: Both models share the same substantial input and output token limits (1,048,576 input and 65,536 output). They also support a very similar set of core capabilities, including code execution, function calling, and URL context. Neither model supports audio generation, image generation, or Live API in their standard stable versions. |
| 68 | + # * **Knowledge Cutoff**: Both models have a knowledge cutoff of January 2025. |
| 69 | + # |
| 70 | + # In essence, while both models are powerful and capable, Gemini 2.5 Pro is designed for maximum performance in complex reasoning tasks, whereas Gemini 2.5 Flash prioritizes cost-effectiveness and speed for broader, high-throughput applications. |
| 71 | + # get URLs retrieved for context |
| 72 | + print(response.candidates[0].url_context_metadata) |
| 73 | + # url_metadata=[UrlMetadata( |
| 74 | + # retrieved_url='https://ai.google.dev/gemini-api/docs/models#gemini-2.5-flash', |
| 75 | + # url_retrieval_status=<UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS'> |
| 76 | + # ), UrlMetadata( |
| 77 | + # retrieved_url='https://ai.google.dev/gemini-api/docs/models#gemini-2.5-pro', |
| 78 | + # url_retrieval_status=<UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: 'URL_RETRIEVAL_STATUS_SUCCESS'> |
| 79 | + # )] |
| 80 | + # [END googlegenaisdk_tools_urlcontext_with_txt] |
| 81 | + return response.text |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + generate_content() |
0 commit comments