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: 1295771" data-attributes="member: 145639"><p>Yes, it was initially due to the token limitation, but then I changed it with manual limitation of media items. In the end a flexible solution, that counts the needed token and splits the query into jobs would be best. Also the merge into movieListText could be combined into the SQLite DB import function, it was just a trial to check if it'S generally doable <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink ;)" loading="lazy" data-shortname=";)" /></p><p></p><p></p><p>Current code is as below:</p><p></p><p>[CODE=csharp]using System;</p><p>using System.Collections.Generic;</p><p>using System.Data.SQLite;</p><p>using System.Text;</p><p>using System.Threading.Tasks;</p><p>using System.Windows;</p><p>using System.Windows.Controls;</p><p>using OpenAI_API.Completions;</p><p>using OpenAI_API;</p><p></p><p>namespace MovieSearchApp</p><p>{</p><p></p><p> public partial class MainWindow : Window</p><p> {</p><p> private const string ConnectionString = "Data Source=C:\\ProgramData\\Team MediaPortal\\MP2-Server\\Database\\Datastore.s3db;Version=3;";</p><p> private const string OpenAIApiKey = "*******************";</p><p></p><p> private string movieListText; // Variable to hold the concatenated movie titles</p><p></p><p></p><p> public MainWindow()</p><p> {</p><p> InitializeComponent();</p><p> PopulateMovieList();</p><p> }</p><p></p><p> private void PopulateMovieList()</p><p> {</p><p> using (SQLiteConnection connection = new SQLiteConnection(ConnectionString))</p><p> {</p><p> connection.Open();</p><p></p><p> string sql = "SELECT MOVIENAME FROM M_MOVIEITEM;";</p><p> using (SQLiteCommand command = new SQLiteCommand(sql, connection))</p><p> using (SQLiteDataReader reader = command.ExecuteReader())</p><p> {</p><p> while (reader.Read())</p><p> {</p><p> string movieTitle = reader["MOVIENAME"].ToString();</p><p> movieListBox.Items.Add(movieTitle);</p><p> }</p><p> }</p><p> }</p><p></p><p> // Call the method to join ListBox items into a string</p><p> movieListText = JoinListBoxItemsToString(movieListBox);</p><p> }</p><p></p><p> string JoinListBoxItemsToString(ListBox listBox)</p><p> {</p><p> // Create a StringBuilder to efficiently concatenate strings</p><p> StringBuilder resultBuilder = new StringBuilder();</p><p></p><p> // Iterate through each item in the ListBox</p><p> //foreach (object item in listBox.Items)</p><p> //{</p><p> // Append the item to the StringBuilder</p><p> // resultBuilder.Append(item.ToString());</p><p></p><p> // Add ";" as a separator for the next item</p><p> // resultBuilder.Append(";");</p><p> //}</p><p></p><p> // Remove the trailing ";" if there are items in the ListBox</p><p> //if (listBox.Items.Count > 0)</p><p> //</p><p> // resultBuilder.Length--; // Remove the last character</p><p> //}</p><p></p><p></p><p> // Limit the loop to the first 300 items</p><p> int itemsToConcatenate = Math.Min(listBox.Items.Count, 300);</p><p></p><p> // Iterate through each item in the ListBox</p><p> for (int i = 0; i < itemsToConcatenate; i++)</p><p> {</p><p> // Append the item to the StringBuilder</p><p> resultBuilder.Append(listBox.Items[i].ToString());</p><p></p><p> // Add ";" as a separator for the next item</p><p> resultBuilder.Append(";");</p><p> }</p><p></p><p> // Remove the trailing ";" if there are items in the ListBox</p><p> if (itemsToConcatenate > 0)</p><p> {</p><p> resultBuilder.Length--; // Remove the last character</p><p> }</p><p></p><p> // Return the final joined string</p><p> return resultBuilder.ToString();</p><p> }</p><p></p><p> private async void SearchButton_Click(object sender, RoutedEventArgs e)</p><p> {</p><p> try</p><p> {</p><p> string searchQuery = searchTextBox.Text.Trim();</p><p></p><p> if (string.IsNullOrEmpty(searchQuery))</p><p> {</p><p> MessageBox.Show("Please enter a search query.");</p><p> return;</p><p> }</p><p></p><p> // Use ChatGPT to generate responses based on the search question</p><p> List<string> bestMatches = await GetBestMovieMatches(searchQuery);</p><p></p><p> // Display the results</p><p> resultListBox.ItemsSource = bestMatches;</p><p> }</p><p> catch (Exception ex)</p><p> {</p><p> MessageBox.Show($"An error occurred: {ex.Message}");</p><p> }</p><p> }</p><p></p><p> private async Task<List<string>> GetBestMovieMatches(string searchQuery)</p><p> {</p><p> // Use ChatGPT to determine the matches</p><p> string generatedText = await GenerateResponseWithGPT(searchQuery, movieListText);</p><p></p><p> // Split the generated text into a list of matches</p><p> List<string> bestMatches = new List<string>(generatedText.Split(';'));</p><p></p><p> return bestMatches;</p><p> }</p><p></p><p> private async Task<string> GenerateResponseWithGPT(string searchQuery, string movieListText)</p><p> {</p><p> APIAuthentication aPIAuthentication = new APIAuthentication(OpenAIApiKey);</p><p> OpenAIAPI openAiApi = new OpenAIAPI(aPIAuthentication);</p><p> try</p><p> {</p><p> string prompt = $"List all movies from the list fitting to the search query: {searchQuery}. Movies: {movieListText}";</p><p> string model = "text-davinci-003";</p><p> int maxTokens = 50;</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></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>}[/CODE]</p></blockquote><p></p>
[QUOTE="ge2301, post: 1295771, member: 145639"] Yes, it was initially due to the token limitation, but then I changed it with manual limitation of media items. In the end a flexible solution, that counts the needed token and splits the query into jobs would be best. Also the merge into movieListText could be combined into the SQLite DB import function, it was just a trial to check if it'S generally doable ;) Current code is as below: [CODE=csharp]using System; using System.Collections.Generic; using System.Data.SQLite; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using OpenAI_API.Completions; using OpenAI_API; namespace MovieSearchApp { public partial class MainWindow : Window { private const string ConnectionString = "Data Source=C:\\ProgramData\\Team MediaPortal\\MP2-Server\\Database\\Datastore.s3db;Version=3;"; private const string OpenAIApiKey = "*******************"; private string movieListText; // Variable to hold the concatenated movie titles public MainWindow() { InitializeComponent(); PopulateMovieList(); } private void PopulateMovieList() { using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) { connection.Open(); string sql = "SELECT MOVIENAME FROM M_MOVIEITEM;"; using (SQLiteCommand command = new SQLiteCommand(sql, connection)) using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string movieTitle = reader["MOVIENAME"].ToString(); movieListBox.Items.Add(movieTitle); } } } // Call the method to join ListBox items into a string movieListText = JoinListBoxItemsToString(movieListBox); } string JoinListBoxItemsToString(ListBox listBox) { // Create a StringBuilder to efficiently concatenate strings StringBuilder resultBuilder = new StringBuilder(); // Iterate through each item in the ListBox //foreach (object item in listBox.Items) //{ // Append the item to the StringBuilder // resultBuilder.Append(item.ToString()); // Add ";" as a separator for the next item // resultBuilder.Append(";"); //} // Remove the trailing ";" if there are items in the ListBox //if (listBox.Items.Count > 0) // // resultBuilder.Length--; // Remove the last character //} // Limit the loop to the first 300 items int itemsToConcatenate = Math.Min(listBox.Items.Count, 300); // Iterate through each item in the ListBox for (int i = 0; i < itemsToConcatenate; i++) { // Append the item to the StringBuilder resultBuilder.Append(listBox.Items[i].ToString()); // Add ";" as a separator for the next item resultBuilder.Append(";"); } // Remove the trailing ";" if there are items in the ListBox if (itemsToConcatenate > 0) { resultBuilder.Length--; // Remove the last character } // Return the final joined string return resultBuilder.ToString(); } private async void SearchButton_Click(object sender, RoutedEventArgs e) { try { string searchQuery = searchTextBox.Text.Trim(); if (string.IsNullOrEmpty(searchQuery)) { MessageBox.Show("Please enter a search query."); return; } // Use ChatGPT to generate responses based on the search question List<string> bestMatches = await GetBestMovieMatches(searchQuery); // Display the results resultListBox.ItemsSource = bestMatches; } catch (Exception ex) { MessageBox.Show($"An error occurred: {ex.Message}"); } } private async Task<List<string>> GetBestMovieMatches(string searchQuery) { // Use ChatGPT to determine the matches string generatedText = await GenerateResponseWithGPT(searchQuery, movieListText); // Split the generated text into a list of matches List<string> bestMatches = new List<string>(generatedText.Split(';')); return bestMatches; } private async Task<string> GenerateResponseWithGPT(string searchQuery, string movieListText) { APIAuthentication aPIAuthentication = new APIAuthentication(OpenAIApiKey); OpenAIAPI openAiApi = new OpenAIAPI(aPIAuthentication); try { string prompt = $"List all movies from the list fitting to the search query: {searchQuery}. Movies: {movieListText}"; string model = "text-davinci-003"; int maxTokens = 50; 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(); return generatedText; } catch (Exception ex) { MessageBox.Show($"An error occurred: {ex.Message}"); return "error"; } } } }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 2
General
AI-based search in (local) MediaItems
Contact us
RSS
Top
Bottom