Skip to content

Commit

Permalink
Merge pull request #301 from xmliszt/preview
Browse files Browse the repository at this point in the history
Fix error message and lift safety setting for conversation generation
  • Loading branch information
xmliszt authored Feb 20, 2024
2 parents 3f214e7 + 809d91e commit 74ba193
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
3 changes: 2 additions & 1 deletion app/ai/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default function AiPage() {
const googleError = tryParseErrorAsGoogleAIError(error, 'topic-generation');
setErrorMessage(googleError.message);
} catch (error) {
setErrorMessage(error.message ?? 'Something went wrong.');
console.error(error);
setErrorMessage('Something went wrong. Please try again!');
}
} finally {
setIsLoading(false);
Expand Down
32 changes: 10 additions & 22 deletions app/level/[id]/level-page-client-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Progress } from '@/components/ui/progress';
import { CONSTANTS } from '@/lib/constants';
import { tryParseErrorAsGoogleAIError } from '@/lib/errors/google-ai-error-parser';
import { HASH } from '@/lib/hash';
import { getPersistence, setPersistence } from '@/lib/persistence/persistence';
import { fetchWord } from '@/lib/services/wordService';
Expand Down Expand Up @@ -251,27 +250,16 @@ export function LevelPageClientWrapper(props: LevelWordsProviderProps) {
await generateConversationFromAI(inputConversation);
setConversation(newConversation);
} catch (error) {
try {
const googleError = tryParseErrorAsGoogleAIError(error, 'conversation');
setConversation([
...inputConversation,
{
role: 'error',
content: googleError.message,
timestamp: new Date().toISOString(),
},
]);
} catch (error) {
console.error(error);
setConversation([
...inputConversation,
{
role: 'error',
content: error.message ?? CONSTANTS.errors.overloaded,
timestamp: new Date().toISOString(),
},
]);
}
console.error(error);
setConversation([
...inputConversation,
{
role: 'error',
content:
"Oops! I encountered a technical issue and I'm unable to continue the conversation right now. Please try again later!",
timestamp: new Date().toISOString(),
},
]);
} finally {
setIsWaitingForAIResponse(false);
setIsLoading(false);
Expand Down
3 changes: 2 additions & 1 deletion app/level/[id]/server/generate-conversation-from-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'server-only';

import { cookies } from 'next/headers';

import { googleGeminiPro } from '@/lib/google-ai';
import { googleGeminiPro, SAFETY_SETTINGS } from '@/lib/google-ai';

/**
* Generate conversations from AI.
Expand Down Expand Up @@ -46,6 +46,7 @@ export async function generateConversationFromAI(
generationConfig: {
maxOutputTokens: 100,
},
safetySettings: SAFETY_SETTINGS,
});
if (!userMessage) throw new Error('Missing user message');
const completion = await chat.sendMessage(userMessage);
Expand Down
10 changes: 5 additions & 5 deletions lib/google-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ export const googleGeminiPro = googleAI.getGenerativeModel({
model: 'gemini-pro',
});

export const HIGH_SAFETY_SETTINGS: SafetySetting[] = [
export const SAFETY_SETTINGS: SafetySetting[] = [
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
];

0 comments on commit 74ba193

Please sign in to comment.