Skip to content

Commit 1e704e3

Browse files
committed
feat(chat): generate suggestions in ai story
1 parent 190dbff commit 1e704e3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

stories/chat.stories.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import type {
1212
IgcMessageAttachment,
1313
} from '../src/components/chat/types.js';
1414

15+
const googleGenAIKey = import.meta.env.VITE_GOOGLE_GEN_AI_KEY;
1516
const ai = new GoogleGenAI({
16-
apiKey: 'googleGenAIKey',
17+
apiKey: googleGenAIKey,
1718
});
1819

1920
defineComponents(IgcChatComponent);
@@ -379,7 +380,35 @@ async function handleAIMessageSend(e: CustomEvent) {
379380
};
380381
chat.messages = [...chat.messages];
381382
}
382-
chat.options = { ...ai_chat_options, suggestions: ['Thank you!'] };
383+
384+
const messagesForSuggestions = [
385+
...chat.messages,
386+
`Based on all my previous prompts give me 3 strings that would act like a suggestions for my next prompt. Don't repeat my previous prompts, I want just the suggestions in the format "suggestion1: '...', suggestion2: '...', suggestion3: '...'`,
387+
];
388+
const responseWithSuggestions = await ai.models.generateContent({
389+
model: 'gemini-2.0-flash',
390+
contents: messagesForSuggestions,
391+
config: {
392+
responseModalities: [Modality.TEXT],
393+
},
394+
});
395+
response = responseWithSuggestions?.candidates?.[0]?.content?.parts;
396+
if (response && response.length === 1) {
397+
const responseText = response[0]?.text ?? '';
398+
const regex: RegExp = /'(.*?)'/g;
399+
400+
// Use String.prototype.matchAll() to get an iterator of all matches, including captured groups.
401+
const matches: IterableIterator<RegExpMatchArray> =
402+
responseText.matchAll(regex);
403+
404+
// Map the matches to an array, extracting the first capturing group (the content).
405+
const suggestions: string[] = Array.from(
406+
matches,
407+
(match: RegExpMatchArray) => match[1]
408+
);
409+
410+
chat.options = { ...ai_chat_options, suggestions: suggestions };
411+
}
383412
}
384413
}, 2000);
385414
}

0 commit comments

Comments
 (0)