Responsible AI:
A Hands-On Approach
San Jose
Allen Firstenberg
Image by Imagen 3
Who are you?
Software developer / Consultant
New York
Google Developer Expert
Google Cloud Champion Innovator
LangChain.js contributor
Co-host "Two Voice Devs"
http://spiders.com/
http://prisoner.com/
LinkedIn: Allen Firstenberg
WARNING
We are dealing with sensitive topic areas.
Follow Along
https://prisoner.com/gemini-safety
Gemini marks the next phase on our journey to making
AI more helpful for everyone
State-of-the-art, natively multimodal reasoning capabilities
Highly optimized while preserving choice
Built with responsibility and safety at the core
Responsible AI
Safety Attribute Definitions
Hate Speech | Negative or harmful comments targeting identity and/or protected attributes. |
Harassment | Malicious, intimidating, bullying, or abusive comments targeting another individual. |
Sexually Explicit | Contains references to sexual acts or other lewd content. |
Dangerous Content | Promotes or enables access to harmful goods, services, and activities. |
Safety Probabilities / Ratings
NEGLIGIBLE | Content has a negligible probability of being unsafe. |
LOW | Content has a low probability of being unsafe. |
MEDIUM | Content has a medium probability of being unsafe. |
HIGH | Content has a high probability of being unsafe. |
Safety Settings
| BLOCK_NONE | Always Show. (Restricted) |
Block Few | BLOCK_ONLY_HIGH | Block only when high probability of unsafe content |
Block Some | BLOCK_MEDIUM_AND_ABOVE | Block medium or high (Default) |
Block Most | BLOCK_HIGH_AND_ABOVE | Block all but negligible |
Contents
Settings/Configs
User: Text
Model: Text
User: Text
Tools
Tools
Safety Settings
Safety Settings
Generation Config
Content of role: "user" �containing multimodal Parts:
Content of role: "model"
"user" content: a text Part
Tools / Functions specified by the caller
Safety setting configured by caller
Generation Config includes: temperature, Top P, Top K, stop seq, max output tokens etc.
Generate Content Request
Candidate(s)
Feedback
Text
Block Reason
Safety Ratings
Candidate "Content"�note: only one candidate returned today
why the model stopped generating
feedback on the prompt
Finish Reason
Finish Message
set if finish reason is present
Safety Ratings
Safety Ratings
how safe is the response
Generate Content Response
…
Demo: AI Studio
https://aistudio.google.com/
Demo: Vertex AI
https://console.cloud.google.com/vertex-ai/generative/multimodal/create/text
Python Colab
Node.js
// Make sure we've installed libraries
// npm install @google/generative-ai
// Make sure we've set our API Key in the shell
// export API_KEY="yourKey"
const {
GoogleGenerativeAI,
HarmBlockThreshold,
HarmCategory,
} = require( "@google/generative-ai" );
async function run(safetySettings) {
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash-001",
safetySettings,
});
const prompt = "Write a story about two people kissing passionately.";
const result = await model.generateContent(prompt);
console.log(result.response.text());
}
const safetySettingsDefault = [
];
const safetySettingsNone = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
];
run(safetySettingsNone);
LangChain.js
const safetySettingsDefault = [
];
const safetySettingsNone = [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_HATE_SPEECH",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold: "BLOCK_NONE",
},
{
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold: "BLOCK_NONE",
},
];
run(safetySettingsDefault);
# Make sure you install the appropriate packages
# npm install langchain @langchain/core @langchain/google-gauth
const { ChatGoogle } = require( "@langchain/google-gauth" );
const { ChatPromptTemplate } = require( "@langchain/core/prompts" );
const { StringOutputParser } = require( "@langchain/core/output_parsers" );
const { MessageGeminiSafetyHandler } = require( "@langchain/google-common/utils" );
async function run(safetySettings){
// Setup the prompt template
const template = "Write a story about two people kissing passionately.";
const prompt = ChatPromptTemplate.fromMessages([
["human", template],
]);
// Setup the safety handler
const safetyHandler = new MessageGeminiSafetyHandler({
msg: "I'm sorry Dave, but I can't do that."
});
// Setup our model
const model = new ChatGoogle({
apiKey: process.env.API_KEY,
safetySettings,
safetyHandler,
});
// The output of a chat model contains a lot of additional information,
// so we want to convert it to a string for this particular use.
const outputParser = new StringOutputParser();
// Create (but don't run) the chain
const chain = prompt.pipe(model).pipe(outputParser);
// Run the chain, providing it the values to put into the template.
const response = await chain.invoke({});
console.log(response);
return response;
}
What then?
Once you have a prompt or reply that is unsafe - what do you do?
Image by Gemini Advanced
Some prompts to try
What to look for
Conclusion
Image by Imagen3
Questions?
https://cloud.google.com/vertex-ai/generative-ai/docs/learn/responsible-ai
https://aistudio.google.com/
https://console.cloud.google.com/vertex-ai/generative/multimodal/create/text
http://spiders.com/
http://prisoner.com/
LinkedIn: Allen Firstenberg