-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Pictogram #52
Changes from 3 commits
a1bd7f8
d59e86e
f749be3
0d042fd
139af07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}; | ||
|
@@ -149,6 +148,21 @@ async function fetchPictogramsURLs({ | |
}); | ||
} | ||
|
||
async function checkWordAvailability( | ||
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, | ||
|
@@ -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, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 These adjustments should lead to better performance and make the function more efficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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