1- using Microsoft . SemanticKernel ;
1+ using MemoryPack ;
2+ using Microsoft . Extensions . AI ;
3+ using Microsoft . SemanticKernel ;
4+ using Microsoft . SemanticKernel . Connectors . Ollama ;
25using Microsoft . SemanticKernel . Embeddings ;
36using OllamaSharp ;
47using OllamaSharp . Models ;
8+ using OpenAI . Chat ;
9+ using PdfReader ;
510using System ;
611using System . Collections . Generic ;
712using System . IO ;
13+ using System . Net . Http ;
814using System . Threading . Tasks ;
15+ using UglyToad . PdfPig . Graphics ;
916
1017#pragma warning disable CA1861 // Avoid constant arrays as arguments
1118#pragma warning disable SKEXP0070 // AddOllamaTextGeneration
19+ #pragma warning disable SKEXP0010
1220
1321namespace Agent_Llama ;
1422
@@ -49,7 +57,7 @@ public async Task SummarizeFileUsingPdfContentPlugin(string PDF_filename = @"C:\
4957 var builder = Kernel . CreateBuilder ( )
5058 . AddOllamaTextGeneration (
5159 endpoint : new Uri ( ollamaEndpoint ) , // Use named argument for 'endpoint'
52- modelId : ollamaModel // Use named argument for 'modelId'
60+ modelId : ollamaModel // Use named argument for 'modelId
5361 ) ;
5462
5563 // Build the kernel instance
@@ -62,6 +70,8 @@ public async Task SummarizeFileUsingPdfContentPlugin(string PDF_filename = @"C:\
6270 var pdfContentPlugin = kernel . CreatePluginFromObject ( new PdfContentPlugin ( ) ) ;
6371 Console . WriteLine ( "PdfContentPlugin loaded successfully." ) ;
6472
73+
74+
6575 // --- Define the path to the text file ---
6676 //string filePath = Path.GetFullPath(sampleFileName);
6777 //Console.WriteLine($"Attempting to summarize file: {filePath}");
@@ -91,6 +101,8 @@ public async Task SummarizeFileUsingPdfContentPlugin(string PDF_filename = @"C:\
91101 var gen = ollamaClient . AsTextEmbeddingGenerationService ( ) ;
92102 var embeds = await gen . GenerateEmbeddingsAsync ( Input ) ;
93103
104+ System . Console . WriteLine ( $ "Generated { embeds . Count } embeddings from Ollama for PDF: { PDF_filename_local } " ) ;
105+
94106 // Create the embedding service
95107 //var embeddingService = OllamaApiClient.AsEmbeddingGenerationService(ollamaClient, "nomic-embed-text");
96108
@@ -136,6 +148,122 @@ public async Task SummarizeFileUsingPdfContentPlugin(string PDF_filename = @"C:\
136148 //Console.Read();
137149 }
138150
151+
152+ public async Task SummarizeFileWithPdfContentPlugin ( string PDF_filename = @"C:\Users\risto\source\repos\PDF_Llama\PDFs\VN.pdf" )
153+ {
154+ // --- Configuration ---
155+ const string PDF_filename_local = @"VN.pdf" ;
156+
157+ var httpClient = new HttpClient ( )
158+ {
159+ BaseAddress = ModelEndpoint ,
160+ Timeout = TimeSpan . FromMinutes ( 20 )
161+ } ;
162+
163+ try
164+ {
165+ var builder = Kernel . CreateBuilder ( )
166+ . AddOllamaTextGeneration (
167+ endpoint : ModelEndpoint ,
168+ modelId : ModelName
169+ ) ;
170+
171+ var kernel = builder . Build ( ) ;
172+
173+ Console . WriteLine ( $ "Kernel initialized with Ollama model: { ModelName } at { ModelEndpoint } ") ;
174+
175+
176+ // --- Import your custom plugin ---
177+ // The KernelPluginFactory.CreateFromType<T>() method is used to discover kernel functions defined within the FileContentPlugin class.
178+ //var pdfContentPlugin = kernel.CreatePluginFromObject(new PdfContentPlugin());
179+ //Console.WriteLine("PdfContentPlugin loaded successfully.");
180+
181+
182+ builder . Services . AddOllamaTextGeneration (
183+ ModelName ,
184+ ModelEndpoint
185+ ) ;
186+
187+ var Input = new List < string > { "your text to embed" } ;
188+
189+ Reader reader = new Reader ( ) ;
190+ var pdftxtlist = reader . ReadPdfToList ( PDF_filename ) ;
191+
192+
193+ // Assuming you have an IHttpClientFactory and a properly configured OllamaApiClient
194+ //var ollamaClient = new OllamaApiClient(ModelEndpoint, ModelName);
195+
196+ var ollamaClient = new OllamaApiClient ( httpClient )
197+ {
198+ SelectedModel = ModelName
199+ } ;
200+
201+ //var gen = ollamaClient.AsTextEmbeddingGenerationService();
202+ var embeds = await ollamaClient . AsTextEmbeddingGenerationService ( ) . GenerateEmbeddingsAsync ( pdftxtlist ) ;
203+
204+ // Serialize
205+ byte [ ] bytearr = MemoryPackSerializer . Serialize ( embeds ) ;
206+ File . WriteAllBytes ( @"C:\tmp\embedding.bin" , bytearr ) ;
207+
208+ // Deserialize
209+ ReadOnlyMemory < float > embeds_from_file = MemoryPackSerializer . Deserialize < ReadOnlyMemory < float > > ( bytearr ) ;
210+
211+
212+ System . Console . WriteLine ( $ "Generated { embeds . Count } , seralised { embeds_from_file . Length } embeddings from Ollama for PDF: { PDF_filename_local } ") ;
213+
214+
215+
216+ //var prompt = $"Summarize the following text in one sentence: {documentText}";
217+ //var response = await chatClient.GetResponseAsync(prompt);
218+
219+ //Console.WriteLine($"\nSummary:\n{response.Message.Text}");
220+
221+ // Create the embedding service
222+ //var embeddingService = OllamaApiClient.AsEmbeddingGenerationService(ollamaClient, "nomic-embed-text");
223+
224+ // Generate embeddings for a text
225+ //var text = "This is a sample sentence.";
226+ //var embedding = await embeddingService.GenerateEmbeddingAsync(text);
227+
228+
229+ var embeddingRequest = new EmbedRequest
230+ {
231+ Input = Input ,
232+ } ;
233+
234+ var embeddingGenerator = new OllamaApiClient ( ModelEndpoint , ModelName )
235+ . EmbedAsync ( embeddingRequest ) ;
236+
237+ //OllamaApiClientExtensions
238+ // .AddOllamaTextEmbeddingGeneration(
239+ // builder.Services,
240+
241+ // modelId: ollamaModel,
242+ // endpoint: new Uri(ollamaEndpoint)
243+ // );
244+
245+ //var embeddingGenerator = new OllamaEmbeddingGenerator(
246+ // modelId: ollamaModel,
247+ // endpoint: new Uri(ollamaEndpoint)
248+ // );
249+
250+ //var embeddingGenerator = new AddOllamaTextEmbeddingGeneration(ollamaEndpoint, ollamaModel)
251+ // .GetEmbeddingClient("your chosen model")
252+ // .AsIEmbeddingGenerator();
253+
254+ }
255+ catch ( Exception ex )
256+ {
257+ Console . WriteLine ( $ "An error occurred: { ex . Message } ") ;
258+ Console . WriteLine ( "Please ensure Ollama is running and the specified model is downloaded." ) ;
259+ Console . WriteLine ( $ "Check your Ollama endpoint: { ModelEndpoint . AbsoluteUri } and model: { ModelName } ") ;
260+ }
261+
262+ Console . WriteLine ( "Press any key to exit." ) ;
263+ }
264+
265+
266+
139267 /// <summary>
140268 /// Creates a sample text file with some dummy content for testing.
141269 /// </summary>
0 commit comments