Skip to content

Commit

Permalink
Add initial safety settings to generateText
Browse files Browse the repository at this point in the history
  • Loading branch information
gsans committed Oct 23, 2023
1 parent e111557 commit f12d1ce
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/app/generative-ai-palm/v1beta2/text.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export class TextServiceClient {
text = text + ". Be very brief in your response so they can be read out loud. Don't use Markdown tables and code fences.";
let prompt: TextRequest = createTextRequest(model, text, undefined, undefined, undefined, undefined, undefined,
[ { "category": HarmCategory.HARM_CATEGORY_DEROGATORY,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE },
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE },
{ "category": HarmCategory.HARM_CATEGORY_TOXICITY,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE },
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE },
{ "category": HarmCategory.HARM_CATEGORY_VIOLENCE,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE },
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE },
{ "category": HarmCategory.HARM_CATEGORY_SEXUAL,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE },
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE },
{ "category": HarmCategory.HARM_CATEGORY_MEDICAL,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE },
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE },
{ "category": HarmCategory.HARM_CATEGORY_DANGEROUS,
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_NONE }]
"threshold": SafetySetting.HarmBlockThreshold.BLOCK_LOW_AND_ABOVE }]
, undefined);

return firstValueFrom(
Expand Down
1 change: 1 addition & 0 deletions src/app/rich-text-editor/prompt-ideas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const promptIdeas = [
`Create a sinister, derogatory, sexual, scary Halloween tale including medical references.`, /* test safety */
//`Explain Quantum mechanics using 2 formulas with katex and a simple diagram using MermaidJS.`,
`Strip out the HTML elements and keep the original tweet and emojis: <Embed Tweet>.`,
//`Summarise each character in [show] with emojis. [show]=Friends`,
Expand Down
22 changes: 22 additions & 0 deletions src/app/rich-text-editor/quill-warning.blot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Quill from 'quill';
const BlockEmbed = Quill.import('blots/block/embed');

class Warning extends BlockEmbed {

static create(value: string) {
const node = super.create(value) as HTMLSpanElement;
node.innerText = value;
node.contentEditable = 'false';
return node;
}

static value(node: HTMLElement) {
return node.childNodes[0].textContent;
}
}

Warning['blotName'] = 'warning';
Warning['tagName'] = 'SPAN';
Warning['className'] = 'ql-warning';

Quill.register({ 'formats/label': Warning });
26 changes: 19 additions & 7 deletions src/app/rich-text-editor/rich-text-editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, EventEmitter, HostListener, Output } from '@angular/core';
import { ContentChange } from 'ngx-quill';
import { Quill } from 'quill';
import './quill-label.blot';
import './quill-warning.blot';
import './quill-text-only.clipboard';
import './quill-markdown.module';
import { promptIdeas as PROMPTS } from './prompt-ideas'
Expand Down Expand Up @@ -104,7 +105,7 @@ export class RichTextEditorComponent {
return text.trim().substring(0, 8196);
}

insertAndFormatMarkdown(text: string) {
insertAndFormatMarkdown(text: string, error: boolean = false) {
//remove language from markdown fences
/* const processedMarkdown = text.replace(/```(\w*)\n([\s\S]*?)```/g, (match, language, code) => {
return `\n\`\`\`\n${code}\`\`\`\n`;
Expand All @@ -123,9 +124,15 @@ export class RichTextEditorComponent {
this.quillInstance.insertText(index, text, 'api');
this.quillInstance.update('api');

this.quillInstance.formatText(index, length-1, {
'background-color': 'rgb(200, 200, 200)'
});
if (error) {
this.quillInstance.formatText(index, length - 1, {
'background-color': 'rgb(200, 0, 0)'
});
} else {
this.quillInstance.formatText(index, length - 1, {
'background-color': 'rgb(200, 200, 200)'
});
}
this.quillInstance.update('api');

this.quillInstance.insertText(index + length, '\n');
Expand All @@ -143,15 +150,20 @@ export class RichTextEditorComponent {
}
}

insertAndFormat(text:string) {
insertAndFormat(text: string, error: boolean = false) {
var range = this.quillInstance.getSelection();
if (range) {
if (range.length > 0) return; // range selected ignore
const index = range.index;
const length = text.length;

this.quillInstance.insertEmbed(index, 'label', text, 'user');
this.quillInstance.update('user');
if (error) {
this.quillInstance.insertEmbed(index, 'warning', text, 'user');
this.quillInstance.update('user');
} else {
this.quillInstance.insertEmbed(index, 'label', text, 'user');
this.quillInstance.update('user');
}

/* this.quillInstance.setText(text);
this.quillInstance.formatText(index, length, { // unbolds 'hello' and set its color to blue
Expand Down
3 changes: 3 additions & 0 deletions src/app/text/text.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ button.run .mat-icon {
::ng-deep .ql-label {
background-color: #e0e0e0;
}
::ng-deep .ql-warning {
background-color: #e0e0e0;
}

.options {
padding: 20px 0px 250px 0px;
Expand Down
57 changes: 37 additions & 20 deletions src/app/text/text.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, Inject, ViewChild } from '@angular/core';
import { PREDICTION_SERVICE_CLIENT_TOKEN } from '../generative-ai-vertex/vertex.module';
import { PredictionServiceClient } from '../generative-ai-vertex/v1/prediction.service';
import { TextResponse } from '../generative-ai-vertex/v1/vertex.types';
//import { TEXT_SERVICE_CLIENT_TOKEN } from '../generative-ai-palm/palm.module';
//import { TextServiceClient } from '../generative-ai-palm/v1beta2/text.service';
// import { PREDICTION_SERVICE_CLIENT_TOKEN } from '../generative-ai-vertex/vertex.module';
// import { PredictionServiceClient } from '../generative-ai-vertex/v1/prediction.service';
// import { TextResponse } from '../generative-ai-vertex/v1/vertex.types';
import { TEXT_SERVICE_CLIENT_TOKEN } from '../generative-ai-palm/palm.module';
import { TextServiceClient } from '../generative-ai-palm/v1beta2/text.service';
import { TextResponse } from '../generative-ai-palm/v1beta2/palm.types';
import { RichTextEditorComponent } from '../rich-text-editor/rich-text-editor.component';
import { AudioService } from '../read/audio.service';
const MAX_PHRASES = 10;
Expand All @@ -20,8 +21,8 @@ export class TextComponent {
playing: boolean = false;

constructor(
//@Inject(TEXT_SERVICE_CLIENT_TOKEN) public client: TextServiceClient,
@Inject(PREDICTION_SERVICE_CLIENT_TOKEN) public client: PredictionServiceClient,
@Inject(TEXT_SERVICE_CLIENT_TOKEN) public client: TextServiceClient,
//@Inject(PREDICTION_SERVICE_CLIENT_TOKEN) public client: PredictionServiceClient,
private audio: AudioService
) { }

Expand All @@ -34,8 +35,15 @@ export class TextComponent {
const prompt = this.editor.extractPrompt();

// PaLM
//const response = await this.client.generateText(prompt);
//const text = (response?.candidates?.[0].output || '').trim();
const response = await this.client.generateText(prompt);
debugger;
if (response.filters && response.filters.length > 0){
this.logBlockedResponse(prompt, response);
this.editor.insertAndFormat("Response was blocked. Try changing your prompt to avoid any derogatory, toxic, sexual, violent, dangerous or medical related content.", true);
} else {
const text = (response?.candidates?.[0].output || '').trim();
this.editor.insertAndFormat(text);
}

// Vertex AI
//const response: TextResponse = await this.client.predict(prompt);
Expand All @@ -45,17 +53,17 @@ export class TextComponent {
// }

// Vertex AI Stream
this.client.streamingPredict(prompt).subscribe({
next: (response: any) => {
console.log('stream-chunk');
response.forEach( (element: any) => {
const text = (element.outputs?.[0].structVal?.content?.stringVal?.[0]).trim();
this.editor.insertStream(text);
});
},
complete: () => { console.log('stream-end'); },
error: (error) => { console.log(error); }
})
// this.client.streamingPredict(prompt).subscribe({
// next: (response: any) => {
// console.log('stream-chunk');
// response.forEach( (element: any) => {
// const text = (element.outputs?.[0].structVal?.content?.stringVal?.[0]).trim();
// this.editor.insertStream(text);
// });
// },
// complete: () => { console.log('stream-end'); },
// error: (error) => { console.log(error); }
// })
}

clear() {
Expand Down Expand Up @@ -87,5 +95,14 @@ export class TextComponent {
});
return text;
}

private logBlockedResponse(prompt: string, response: TextResponse) {
if (!response.filters || response.filters.length == 0) return;

console.log("Response was blocked.");
console.log(`Original prompt: ${prompt}`);
console.log(`Filters applied:\n${JSON.stringify(response.filters, null, " ")}`);
console.log(`Safety settings and category ratings:\n${JSON.stringify(response.safetyFeedback, null, " ")}`);
}
}

0 comments on commit f12d1ce

Please sign in to comment.