Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Chat with YouTube videos using .NET and C#

This is a sample demonstrating how to ingest a transcript for a YouTube video and ask questions about it using OpenAI, or local AI models, and .NET.
This is a sample demonstrating how to ingest a transcript for a YouTube video and ask questions about it using either OpenAI, Gemini, or local AI models, and .NET.

## Running the sample
To run the sample:
1. Clone the app locally
2. Get an API key for OpenAI and set a local environment variable named OPENAI_API_KEY with that key
3. Run the project using your favorite editor, or calling `dotnet run` from a Terminal in the root of your project directory
3. Alternatively, get an API key for [Gemini](https://aistudio.google.com/app/apikey) and set a local environment variable named GOOGLE_API_KEY with that key
4. Run the project using your favorite editor, or calling `dotnet run` from a Terminal in the root of your project directory

You could also run this sample using a local AI model, downloaded using [Ollama](https://ollama.com):
1. Install [Ollama](https://ollama.com)
Expand All @@ -20,5 +21,6 @@ This sample is built using the following technology:
* [.NET 9](https://dotnet.microsoft.com/en-us/download/dotnet/9.0)
* [Microsoft.Extensions.AI](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/)
* The [OpenAI library for .NET](https://www.nuget.org/packages/OpenAI)
* The [Gemini SDK for .NET using Microsoft.Extension.AI](https://www.nuget.org/packages/Mscc.GenerativeAI.Microsoft/2.0.1-preview)
* [OllamaSharp](https://www.nuget.org/packages/OllamaSharp)
* A [YouTubeTranscriptAPI](https://www.nuget.org/packages/Lofcz.Forks.YoutubeTranscriptApi) package, which is a .NET implementation of the Python [youtube-transcript-api](https://github.com/jdepoix/youtube-transcript-api) module
21 changes: 19 additions & 2 deletions YouTubeChat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@
using Microsoft.Extensions.AI;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
using Mscc.GenerativeAI.Microsoft;
using OllamaSharp;
using OpenAI;
using YoutubeTranscriptApi;

AppContext.SetSwitch("System.Net.SocketsHttpHandler.Http3Support", false);

var usingOpenAI = true;
var usingGemini = true;
IChatClient chatClient = null;
IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator = null;

var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
if (apiKey is null) usingOpenAI = false;

apiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY");
if (apiKey is null) usingGemini = false;

// Setup the connection to OpenAI
if (usingOpenAI)
{
OpenAIClient client = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
OpenAIClient client = new OpenAIClient(apiKey);
chatClient = client.AsChatClient("gpt-4o-mini");
embeddingGenerator = client.AsEmbeddingGenerator("text-embedding-3-small");
}
// Setup the connection to Gemini
else if (usingGemini)
{
chatClient = new GeminiChatClient(apiKey,
Environment.GetEnvironmentVariable("GOOGLE_AI_MODEL") ?? "gemini-1.5-flash-latest");
embeddingGenerator = new GeminiEmbeddingGenerator(apiKey, "text-embedding-004");
}
else
{
var ollamaEndpoint = "http://localhost:11434/";
Expand Down Expand Up @@ -82,6 +99,7 @@

do
{
Console.WriteLine("Ask a question, or type 'exit' to quit.");
string prompt = Console.ReadLine();

if (prompt.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
Expand Down Expand Up @@ -122,7 +140,6 @@ You should also provide links to documentation and other resources that can help

Question: {prompt}";

Console.WriteLine("Ask a question, or type 'exit' to quit.");
Console.ForegroundColor = ConsoleColor.Green; // Set console text color to green
IAsyncEnumerable<StreamingChatCompletionUpdate> responseChunk = chatClient.CompleteStreamingAsync(systemPrompt);
await foreach (var update in responseChunk)
Expand Down
13 changes: 7 additions & 6 deletions YouTubeChat/YouTubeChat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

<ItemGroup>
<PackageReference Include="Lofcz.Forks.YoutubeTranscriptApi" Version="0.4.2" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.0-preview.9.24556.5" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.0-preview.9.24556.5" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.0.1-preview.1.24570.5" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.0.1-preview.1.24570.5" />
<PackageReference Include="Microsoft.ML.Tokenizers" Version="1.0.0" />
<PackageReference Include="Microsoft.SemanticKernel.Core" Version="1.29.0" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.26.0-preview" />
<PackageReference Include="OllamaSharp" Version="4.0.3" />
<PackageReference Include="OpenAI" Version="2.0.0" />
<PackageReference Include="Microsoft.SemanticKernel.Core" Version="1.32.0" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.32.0-preview" />
<PackageReference Include="OllamaSharp" Version="4.0.11" />
<PackageReference Include="OpenAI" Version="2.1.0" />
<PackageReference Include="Mscc.GenerativeAI.Microsoft" Version="2.0.1-preview" />
</ItemGroup>

</Project>
Loading