home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 2
General
AI-based search in (local) MediaItems
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="ge2301" data-source="post: 1295825" data-attributes="member: 145639"><p>So far I needed to limit the media items to respect the token limit of ChatGPT.</p><p>Now the complete media items can be used, because the movie list is splitted into chunks of a specified size and responses for each chunk are concentrated into the result list <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile :)" loading="lazy" data-shortname=":)" /></p><p></p><p>[CODE=csharp] private async Task<string> GenerateResponseWithGPT(string searchQuery, List<string> movieList)</p><p> {</p><p> // Chunk the movie list into smaller segments</p><p> int chunkSize = 500; // Adjust the chunk size based on your requirements</p><p> List<List<string>> chunks = SplitList(movieList, chunkSize);</p><p></p><p> // Generate responses for each chunk and concatenate the results</p><p> List<string> generatedResponses = new List<string>();</p><p> foreach (var chunk in chunks)</p><p> {</p><p> string movieListText = string.Join(";", chunk);</p><p> string prompt = $"Filter for only movies from <{movieListText}> if following is true for the movie: {searchQuery}. Use a simikolon as separator and do not write any summary comments. No result means the output is simply empty";</p><p></p><p> string responseChunk = await GenerateResponseChunk(prompt);</p><p> generatedResponses.Add(responseChunk);</p><p> }</p><p></p><p> // Concatenate the generated responses into a single string</p><p> string finalResponse = string.Concat(generatedResponses);</p><p></p><p> return finalResponse;</p><p> }</p><p></p><p> private async Task<string> GenerateResponseChunk(string prompt)</p><p> {</p><p> APIAuthentication aPIAuthentication = new APIAuthentication(OpenAIApiKey);</p><p> OpenAIAPI openAiApi = new OpenAIAPI(aPIAuthentication);</p><p> //string movieListText = string.Join(";", movieList);</p><p> try</p><p> {</p><p> string model = "text-davinci-003";</p><p> int maxTokens = 700;</p><p></p><p> var completionRequest = new CompletionRequest</p><p> {</p><p> Prompt = prompt,</p><p> Model = model,</p><p> MaxTokens = maxTokens</p><p> };</p><p></p><p> var completionResult = await openAiApi.Completions.CreateCompletionAsync(completionRequest);</p><p> var generatedText = completionResult.Completions[0].Text; //completionResult.Choices[0].Text.Trim();</p><p> MessageBox.Show($"Filtered Movies: {completionResult}");</p><p> return generatedText;</p><p> }</p><p> catch (Exception ex)</p><p> {</p><p> MessageBox.Show($"An error occurred: {ex.Message}");</p><p> return "error";</p><p> }</p><p> }</p><p></p><p> private void FilterResultListBox()</p><p> {</p><p> // Filter items in resultListBox based on items present in movieListBox</p><p> List<string> filteredResult = new List<string>();</p><p> foreach (var item in resultListBox.Items)</p><p> {</p><p> if (movieListBox.Items.Contains(item))</p><p> {</p><p> filteredResult.Add(item.ToString());</p><p> }</p><p> }</p><p></p><p> // Display the filtered items in resultListBox</p><p> DisplayMovieList(resultListBox, filteredResult);</p><p> }</p><p></p><p> private async Task<List<string>> FilterMoviesWithGPT(string searchQuery)</p><p> {</p><p> // Use ChatGPT to determine if each movie fits the search query</p><p> string generatedText = await GenerateResponseWithGPT(searchQuery, movieList);</p><p> //MessageBox.Show($"Filtered Movies: {generatedText}");</p><p> // Split the generated text into a list of matches</p><p> List<string> filteredMovies = new List<string>(generatedText.Split(';'));</p><p></p><p> return filteredMovies;</p><p></p><p> }</p><p> private void DisplayMovieList(ListBox listBox, List<string> movies)</p><p> {</p><p> listBox.ItemsSource = movies;</p><p> }</p><p></p><p> // Split a list into chunks of a specified size</p><p> private List<List<T>> SplitList<T>(List<T> source, int chunkSize)</p><p> {</p><p> return source</p><p> .Select((x, i) => new { Index = i, Value = x })</p><p> .GroupBy(x => x.Index / chunkSize)</p><p> .Select(x => x.Select(v => v.Value).ToList())</p><p> .ToList();</p><p> }[/CODE]</p></blockquote><p></p>
[QUOTE="ge2301, post: 1295825, member: 145639"] So far I needed to limit the media items to respect the token limit of ChatGPT. Now the complete media items can be used, because the movie list is splitted into chunks of a specified size and responses for each chunk are concentrated into the result list :) [CODE=csharp] private async Task<string> GenerateResponseWithGPT(string searchQuery, List<string> movieList) { // Chunk the movie list into smaller segments int chunkSize = 500; // Adjust the chunk size based on your requirements List<List<string>> chunks = SplitList(movieList, chunkSize); // Generate responses for each chunk and concatenate the results List<string> generatedResponses = new List<string>(); foreach (var chunk in chunks) { string movieListText = string.Join(";", chunk); string prompt = $"Filter for only movies from <{movieListText}> if following is true for the movie: {searchQuery}. Use a simikolon as separator and do not write any summary comments. No result means the output is simply empty"; string responseChunk = await GenerateResponseChunk(prompt); generatedResponses.Add(responseChunk); } // Concatenate the generated responses into a single string string finalResponse = string.Concat(generatedResponses); return finalResponse; } private async Task<string> GenerateResponseChunk(string prompt) { APIAuthentication aPIAuthentication = new APIAuthentication(OpenAIApiKey); OpenAIAPI openAiApi = new OpenAIAPI(aPIAuthentication); //string movieListText = string.Join(";", movieList); try { string model = "text-davinci-003"; int maxTokens = 700; var completionRequest = new CompletionRequest { Prompt = prompt, Model = model, MaxTokens = maxTokens }; var completionResult = await openAiApi.Completions.CreateCompletionAsync(completionRequest); var generatedText = completionResult.Completions[0].Text; //completionResult.Choices[0].Text.Trim(); MessageBox.Show($"Filtered Movies: {completionResult}"); return generatedText; } catch (Exception ex) { MessageBox.Show($"An error occurred: {ex.Message}"); return "error"; } } private void FilterResultListBox() { // Filter items in resultListBox based on items present in movieListBox List<string> filteredResult = new List<string>(); foreach (var item in resultListBox.Items) { if (movieListBox.Items.Contains(item)) { filteredResult.Add(item.ToString()); } } // Display the filtered items in resultListBox DisplayMovieList(resultListBox, filteredResult); } private async Task<List<string>> FilterMoviesWithGPT(string searchQuery) { // Use ChatGPT to determine if each movie fits the search query string generatedText = await GenerateResponseWithGPT(searchQuery, movieList); //MessageBox.Show($"Filtered Movies: {generatedText}"); // Split the generated text into a list of matches List<string> filteredMovies = new List<string>(generatedText.Split(';')); return filteredMovies; } private void DisplayMovieList(ListBox listBox, List<string> movies) { listBox.ItemsSource = movies; } // Split a list into chunks of a specified size private List<List<T>> SplitList<T>(List<T> source, int chunkSize) { return source .Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / chunkSize) .Select(x => x.Select(v => v.Value).ToList()) .ToList(); }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 2
General
AI-based search in (local) MediaItems
Contact us
RSS
Top
Bottom