Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pictogram #52

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ async function getWordSuggestions({
prompt: `act as a speech pathologist selecting pictograms in language ${languageName}
for a non verbal person about ${prompt}.
Here are mandatory instructions for the list:
-Ensure that the list contains precisely ${maxWords} words; it must not be shorter or longer.
-The words should be related to the topic.
-When using verbs, you must use the infinitive form. Do not use gerunds, conjugated forms, or any other variations of the verb.
-Do not repeat any words.
-Do not include any additional text, symbols, or characters beyond the words requested.
-The list should follow this exact format: {word1, word2, word3,..., wordN}.`,
-You must provide a list of ${maxWords} maximum.
-When using verbs you must use infinitive form. Do not use gerunds, conjugated forms, or any other variations of the verb.
-It is very important to not repeat words.
-Do not add any other text or characters to the list.
-Template for the list {word1, word2, word3,..., wordN}`,
temperature: 0,
max_tokens: max_tokens,
};
Expand Down Expand Up @@ -149,6 +148,21 @@ async function fetchPictogramsURLs({
});
}

async function checkWordAvailability(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this function, as it is no longer in use

word: string,
language: string,
symbolSet?: SymbolSet,
globalSymbolsSymbolSet?: string
): Promise<boolean> {
const urls = await fetchPictogramsURLs({
words: [word],
language,
symbolSet,
globalSymbolsSymbolSet,
});
return urls[0].pictogram.images.length > 0 && urls[0].pictogram.images[0].url !== "";
}

async function getSuggestions({
prompt,
maxSuggestions = DEFAULT_MAX_SUGGESTIONS,
Expand All @@ -164,19 +178,45 @@ async function getSuggestions({
}): Promise<Suggestion[]> {
const words: string[] = await getWordSuggestions({
prompt,
maxWords: maxSuggestions,
maxWords: maxSuggestions * 2,
language,
});
const suggestions: Suggestion[] = await fetchPictogramsURLs({
words,

const validatedWords: string[] = [];
const unavailableWords: string[] = [];

for (const word of words) {
if (validatedWords.length >= maxSuggestions) {
break;
}

const isAvailable = await checkWordAvailability(
word,
language,
symbolSet,
globalSymbolsSymbolSet
);

if (isAvailable) {
validatedWords.push(word);
} else {
unavailableWords.push(word);
}
}
//In case the number of validated words is less than the maxSuggestions, we add unavailable words to reach the maxSuggestions
while (validatedWords.length < maxSuggestions) {
validatedWords.push(unavailableWords.pop() || "");
}

return await fetchPictogramsURLs({
words: validatedWords,
language,
symbolSet,
globalSymbolsSymbolSet,
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the function makes sequential requests to retrieve each pictogram, filters them to check for availability, and then re-fetches to create a new list of validated suggestions. This results in redundant API calls and can impact performance.

Suggested Improvements:

Create the Suggestions Array While Fetching: Build the Suggestions array during the initial availability checks, avoiding unnecessary re-fetching of images. This will improve both performance and API efficiency.

Batch Fetch Requests with Limited Concurrency: Instead of fetching each image individually, you can use fetchPictogramsURLs to batch the requests for multiple words at once. By limiting the number of concurrent requests (e.g., fetching 5 words at a time), you can significantly reduce the number of sequential API calls, improving efficiency and reducing API load.

These adjustments should lead to better performance and make the function more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. Will implement!


return suggestions;
}


async function isContentSafe(textPrompt: string): Promise<boolean> {
try {
const contentSafetyConfig = globalConfiguration.contentSafety;
Expand Down