title: [Udacity] Gemini API by Google Course Sharing
published: false
date: 2024-07-16 00:00:00 UTC
tags:
canonical_url: https://www.evanlin.com/gemma2-as-local-model-howto/
---

(Course link: https://www.udacity.com/enrollment/cd13416)
## Preface
This course, a collaboration between Google and Udacity, is highly recommended. Besides being free, it provides detailed information and usage instructions that are not available in many documents. If you want to better understand how to use each API, consider taking a look.
Here's a quick overview of the course outline, followed by a few concepts I think are very important:
## Course Outline
- Introduction to LLMs and Gemini
- Basic concept supplement.
- Introduction to prompting in Google AI Studio
- Mainly instructions on how to operate AI Studio.
- Developing with the Gemini API
- Developers are recommended to watch this, mostly API usage details.
- Advanced Applications
- Course summary and related small projects for students to practice.
## Key Points Summary:
### How to calculate output tokens
token_n_model = genai.GenerativeModel(model_name, generation_config={"temperature": 0.0})
poem_prompt = "Write me a poem about Berkeley's campus"
prompt_token_count = token_n_model.count_tokens(poem_prompt)
output_token_count = token_n_model.count_tokens(response.text)
print(f'Tokens in prompt: {prompt_token_count} \n Estimated tokens in output {output_token_count}')
Through
- prompt\_token\_count: The number of input tokens.
- output\_token\_count: The number of output tokens.
### Safety Setting
Due to Gemini's strict controls, some data will often be rejected due to safety factors. In this case, you need to do some processing:
Before proceeding with this demonstration, remember to handle AI responsibly and follow ethical guidelines.
Security is an important built-in feature of Gemini. Let's learn more about its functions:
### How to check if your prompt is blocked by the safety filter
- Which safety filters caused the block
- How to adjust settings to unblock
### Check if the prompt is blocked
Assuming we selected the following prompt, you can expect your response to be blocked under the current security settings:
model = genai.GenerativeModel("gemini-1.5-flash", generation_config={"temperature": 0})
unsafe_prompt = "Write a threat a video game villain might make"
response = model.generate_content(unsafe_prompt)
print(response.text)
Output: ValueError: Invalid operation: `response.text` quick accessor requires the response to contain a valid `Part`, but nothing was returned. Please check `candidate.safety_ratings` to determine if the response was blocked.
Now, you can print `response.candidates[0].finish_reason` to further investigate.
- If `finish_reason` is `FinishReason.STOP`, it means your generation request ran successfully.
- If `finish_reason` is `FinishReason.SAFETY`, it means your generation request was blocked for safety reasons, so the `response.text` structure will be empty.
### Check the safety filter
Printing `response.candidates[0].safety_ratings` will display the rating structure. We might see something like this:
[category: HARM_CATEGORY_SEXUALLY_EXPLICIT
probability: NEGLIGIBLE
, category: HARM_CATEGORY_HATE_SPEECH
probability: NEGLIGIBLE
, category: HARM_CATEGORY_HARASSMENT
probability: MEDIUM
, category: HARM_CATEGORY_DANGEROUS_CONTENT
probability: MEDIUM
]
Here, our prompt triggered the `HARM_CATEGORY_HARASSMENT` and `HARM_CATEGORY_DANGEROUS_CONTENT` categories with a medium probability, which is why we were blocked from seeing the output.
### How to remove filters
response = model.generate_content(
unsafe_prompt,
safety_settings={
'HATE': 'BLOCK_LOW_AND_ABOVE',
'HARASSMENT': 'BLOCK_NONE',
'SEXUAL' : 'BLOCK_LOW_AND_ABOVE',
'DANGEROUS' : 'BLOCK_NONE'
})
This way, even if you want to generate some text with swear words or offensive language, it is allowed.
### Code execution can reduce hallucinations
You can let Gemini generate Python code for you and actually execute it ([example](https://ai.google.dev/gemini-api/docs/code-execution?lang=python&utm_source=udacity&utm_medium=referral&utm_campaign=gemini-api-course&utm_content=embedding)), for some math (or difficult to calculate numbers) can be calculated by executing the code.
import os
import google.generativeai as genai
genai.configure(api_key=os.environ['API_KEY'])
model = genai.GenerativeModel(
model_name='gemini-1.5-pro',
tools='code_execution')
response = model.generate_content((
'What is the sum of the first 50 prime numbers? '
'Generate and run code for the calculation, and make sure you get all 50.'))
print(response.text)
Top comments (0)