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: 1295754" data-attributes="member: 145639"><p>[ATTACH=full]212771[/ATTACH]</p><p></p><p>Hi,</p><p></p><p>I'm planning to write a plugin for MediaPortal 2, that supports users to find the right media items with help of AI frameworks.</p><p></p><p>Following is used:</p><ul> <li data-xf-list-type="ul"><a href="https://platform.openai.com/docs/overview" target="_blank">OpenAI Framework</a></li> <li data-xf-list-type="ul"><a href="https://chat.openai.com" target="_blank">ChatGPT3.5 Client</a></li> <li data-xf-list-type="ul">Model: text-davinci-003</li> <li data-xf-list-type="ul"><a href="https://www.nuget.org/packages/OpenAI/" target="_blank">OpenAI API</a></li> </ul><p></p><p>It makes mostly sense in combination of speech control which is not existing/working for MP2 as complex questions are uncomfortable to enter with remote control.</p><ol> <li data-xf-list-type="ol">In first step I want to only limit this to existing local media items in the MP2-DB</li> <li data-xf-list-type="ol">In second step (only if first one works well) it could be extended to all media items, also not in MP2-DB existent ones</li> </ol><p><strong>Target state for Step 1:</strong></p><ul> <li data-xf-list-type="ul">You can perform easy filter actions for year, gerne, actors, ... (E.g.: "Show me all movies from the genre Thriller", ...)</li> <li data-xf-list-type="ul">You can perform complex filter actions (E.g.: "Show me all movies, that contain cowboys", "Show me movies with blue-skinned aliens", ...)</li> </ul><p>I wrote the program at first independently from MP2 as this is easier especially for testing purposes. Once it works content-wise it can be integrated as plugin.</p><p></p><p><strong>What does the program do yet:</strong></p><ul> <li data-xf-list-type="ul">The C# WPF application reads movie titles from MP2 SQLite database table (M_MOVIEITEM) and displays the complete list of movie titles on the right side of the window (I first concentrate only on the movie title)</li> <li data-xf-list-type="ul">It then uses the OpenAI GPT-3 API to find the best movie match based on a search question entered by the user (Restiction: Learning date are from 12/2021, all titles after that can currently not be considered)</li> <li data-xf-list-type="ul">The result is now displayed in a ListBox named resultListBox, which allows for multiple matches to be shown. The GetBestMovieMatches method returns a list of best matches, and the resultListBox.ItemsSource is set to this list for display.</li> </ul><p><strong>MainWindow.xaml</strong></p><p>[CODE=xml]<Window x:Class="MovieSearchApp.MainWindow"</p><p> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</p><p> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"</p><p> Title="Movie Search" Height="350" Width="500"></p><p> <Grid></p><p> <Grid.ColumnDefinitions></p><p> <ColumnDefinition Width="*" /></p><p> <ColumnDefinition Width="*" /></p><p> </Grid.ColumnDefinitions></p><p></p><p> <!-- Left Column: Movie List --></p><p> <ListBox x:Name="movieListBox" Grid.Column="0" Margin="10"/></p><p></p><p> <!-- Right Column: Search and Result --></p><p> <StackPanel Grid.Column="1" Margin="10"></p><p> <Label Content="Enter search query:"/></p><p> <TextBox x:Name="searchTextBox" Width="200" Margin="0,0,0,10"/></p><p> <Button Content="OK" Click="SearchButton_Click"/></p><p> <Label Content="Best Movie Matches:" Margin="0,20,0,0"/></p><p> <ListBox x:Name="resultListBox" Width="200" Height="60"/></p><p> </StackPanel></p><p> </Grid></p><p></Window></p><p>[/CODE]</p><p></p><p><strong>MainWindow.xaml.cs</strong></p><p>[CODE=csharp]using System;</p><p>using System.Collections.Generic;</p><p>using System.Data.SQLite;</p><p>using System.Net.Http;</p><p>using System.Text;</p><p>using System.Threading.Tasks;</p><p>using System.Windows;</p><p>using System.Windows.Controls;</p><p></p><p>namespace MovieSearchApp</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 = "your_openai_api_key";</p><p> private const string OpenAIEndpoint = "https://api.openai.com/v1/engines/davinci-codex/completions";</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></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> List<string> bestMatches = new List<string>();</p><p></p><p> foreach (string movieTitle in movieListBox.Items)</p><p> {</p><p> // Use ChatGPT to determine the match</p><p> string generatedText = await GenerateResponseWithGPT(searchQuery, movieTitle);</p><p></p><p> // You can add your matching logic here based on the generated text</p><p> // For simplicity, we're using the whole generated text as the match</p><p> if (!string.IsNullOrEmpty(generatedText))</p><p> {</p><p> bestMatches.Add(generatedText);</p><p> }</p><p> }</p><p></p><p> return bestMatches;</p><p> }</p><p></p><p> private async Task<string> GenerateResponseWithGPT(string searchQuery, string movieTitle)</p><p> {</p><p> using (HttpClient client = new HttpClient())</p><p> {</p><p> client.DefaultRequestHeaders.Add("Authorization", $"Bearer {OpenAIApiKey}");</p><p></p><p> string prompt = $"Find the best movie match for the search query: {searchQuery}. Movie Title: {movieTitle}";</p><p></p><p> var requestData = new</p><p> {</p><p> prompt,</p><p> max_tokens = 50,</p><p> temperature = 0.7</p><p> };</p><p></p><p> string requestDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestData);</p><p> var content = new StringContent(requestDataJson, Encoding.UTF8, "application/json");</p><p></p><p> HttpResponseMessage response = await client.PostAsync(OpenAIEndpoint, content);</p><p></p><p> if (response.IsSuccessStatusCode)</p><p> {</p><p> string responseJson = await response.Content.ReadAsStringAsync();</p><p> dynamic responseData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseJson);</p><p> string generatedText = responseData.choices[0].text;</p><p></p><p> return generatedText;</p><p> }</p><p> else</p><p> {</p><p> throw new Exception($"ChatGPT API request failed. Status code: {response.StatusCode}");</p><p> }</p><p> }</p><p> }</p><p> }</p><p>}[/CODE]</p><p></p><p></p><p><s>Current problem is, that always the error "No Data" is thrown. </s><strong>Solved</strong></p><p>Does anyone have a <a href="https://openai.com/" target="_blank">OpenAI</a> API (1/user is free) and can support me writing/testing?</p><p>Make sure to replace "database.db" with the actual path to your SQLite database file (I kept mine in the code, as this will not change for most users) and "your_openai_api_key" with your OpenAI API key.</p></blockquote><p></p>
[QUOTE="ge2301, post: 1295754, member: 145639"] [ATTACH type="full" width="207px" alt="openai-icon-2021x2048-4rpe5x7n.png"]212771[/ATTACH] Hi, I'm planning to write a plugin for MediaPortal 2, that supports users to find the right media items with help of AI frameworks. Following is used: [LIST] [*][URL='https://platform.openai.com/docs/overview']OpenAI Framework[/URL] [*][URL='https://chat.openai.com']ChatGPT3.5 Client[/URL] [*]Model: text-davinci-003 [*][URL='https://www.nuget.org/packages/OpenAI/']OpenAI API[/URL] [/LIST] It makes mostly sense in combination of speech control which is not existing/working for MP2 as complex questions are uncomfortable to enter with remote control. [LIST=1] [*]In first step I want to only limit this to existing local media items in the MP2-DB [*]In second step (only if first one works well) it could be extended to all media items, also not in MP2-DB existent ones [/LIST] [B]Target state for Step 1:[/B] [LIST] [*]You can perform easy filter actions for year, gerne, actors, ... (E.g.: "Show me all movies from the genre Thriller", ...) [*]You can perform complex filter actions (E.g.: "Show me all movies, that contain cowboys", "Show me movies with blue-skinned aliens", ...) [/LIST] I wrote the program at first independently from MP2 as this is easier especially for testing purposes. Once it works content-wise it can be integrated as plugin. [B]What does the program do yet:[/B] [LIST] [*]The C# WPF application reads movie titles from MP2 SQLite database table (M_MOVIEITEM) and displays the complete list of movie titles on the right side of the window (I first concentrate only on the movie title) [*]It then uses the OpenAI GPT-3 API to find the best movie match based on a search question entered by the user (Restiction: Learning date are from 12/2021, all titles after that can currently not be considered) [*]The result is now displayed in a ListBox named resultListBox, which allows for multiple matches to be shown. The GetBestMovieMatches method returns a list of best matches, and the resultListBox.ItemsSource is set to this list for display. [/LIST] [B]MainWindow.xaml[/B] [CODE=xml]<Window x:Class="MovieSearchApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Movie Search" Height="350" Width="500"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!-- Left Column: Movie List --> <ListBox x:Name="movieListBox" Grid.Column="0" Margin="10"/> <!-- Right Column: Search and Result --> <StackPanel Grid.Column="1" Margin="10"> <Label Content="Enter search query:"/> <TextBox x:Name="searchTextBox" Width="200" Margin="0,0,0,10"/> <Button Content="OK" Click="SearchButton_Click"/> <Label Content="Best Movie Matches:" Margin="0,20,0,0"/> <ListBox x:Name="resultListBox" Width="200" Height="60"/> </StackPanel> </Grid> </Window> [/CODE] [B]MainWindow.xaml.cs[/B] [CODE=csharp]using System; using System.Collections.Generic; using System.Data.SQLite; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; 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 = "your_openai_api_key"; private const string OpenAIEndpoint = "https://api.openai.com/v1/engines/davinci-codex/completions"; 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); } } } } 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) { List<string> bestMatches = new List<string>(); foreach (string movieTitle in movieListBox.Items) { // Use ChatGPT to determine the match string generatedText = await GenerateResponseWithGPT(searchQuery, movieTitle); // You can add your matching logic here based on the generated text // For simplicity, we're using the whole generated text as the match if (!string.IsNullOrEmpty(generatedText)) { bestMatches.Add(generatedText); } } return bestMatches; } private async Task<string> GenerateResponseWithGPT(string searchQuery, string movieTitle) { using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {OpenAIApiKey}"); string prompt = $"Find the best movie match for the search query: {searchQuery}. Movie Title: {movieTitle}"; var requestData = new { prompt, max_tokens = 50, temperature = 0.7 }; string requestDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestData); var content = new StringContent(requestDataJson, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync(OpenAIEndpoint, content); if (response.IsSuccessStatusCode) { string responseJson = await response.Content.ReadAsStringAsync(); dynamic responseData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseJson); string generatedText = responseData.choices[0].text; return generatedText; } else { throw new Exception($"ChatGPT API request failed. Status code: {response.StatusCode}"); } } } } }[/CODE] [S]Current problem is, that always the error "No Data" is thrown. [/S][B]Solved[/B] Does anyone have a [URL='https://openai.com/']OpenAI[/URL] API (1/user is free) and can support me writing/testing? Make sure to replace "database.db" with the actual path to your SQLite database file (I kept mine in the code, as this will not change for most users) and "your_openai_api_key" with your OpenAI API key. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 2
General
AI-based search in (local) MediaItems
Contact us
RSS
Top
Bottom