mirror of
https://github.com/run-llama/llamaindex.net.git
synced 2026-07-01 20:36:58 -04:00
Add DI extensions and fix sample inmemory issue
This commit is contained in:
@@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Llamaindex.ServiceDefaults"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LlamaParseAspire", "samples\Aspire\LlamaParseAspire\LlamaParseAspire.csproj", "{F721E5DD-3C0E-41A3-B030-913E4AE187F5}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.LlamaParse.Client", "src\Aspire.LlamaParse.Client\Aspire.LlamaParse.Client.csproj", "{545DC26A-0420-44FD-9391-B6E150E2CDEC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -57,6 +59,10 @@ Global
|
||||
{F721E5DD-3C0E-41A3-B030-913E4AE187F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F721E5DD-3C0E-41A3-B030-913E4AE187F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F721E5DD-3C0E-41A3-B030-913E4AE187F5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{545DC26A-0420-44FD-9391-B6E150E2CDEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{545DC26A-0420-44FD-9391-B6E150E2CDEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{545DC26A-0420-44FD-9391-B6E150E2CDEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{545DC26A-0420-44FD-9391-B6E150E2CDEC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -69,6 +75,7 @@ Global
|
||||
{E6CAE94F-626F-4348-A062-EADA9CABDB7A} = {D41C4A39-8A5E-488C-A2AE-5B164F79B07C}
|
||||
{D31D3C9F-5055-4914-90C6-566E0EA46877} = {D41C4A39-8A5E-488C-A2AE-5B164F79B07C}
|
||||
{F721E5DD-3C0E-41A3-B030-913E4AE187F5} = {D41C4A39-8A5E-488C-A2AE-5B164F79B07C}
|
||||
{545DC26A-0420-44FD-9391-B6E150E2CDEC} = {87989DE6-A7D3-4C9C-AE2D-555AB2D6E018}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A523EAFD-F1D2-429A-97E2-F86406625D67}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Aspire.LlamaParse.Client\Aspire.LlamaParse.Client.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\LlamaParse\LlamaParse.csproj" />
|
||||
<ProjectReference Include="..\Llamaindex.ServiceDefaults\Llamaindex.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using LlamaIndex.Core.Schema;
|
||||
using LlamaParse;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -11,12 +12,7 @@ builder.AddServiceDefaults();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddTransient<LlamaParseClient>(sp =>
|
||||
{
|
||||
var client = sp.GetRequiredService<HttpClient>();
|
||||
var llamaParseClient = new LlamaParseClient(client, apiKey: builder.Configuration.GetSection("LlamaParse")["ApiKey"]!);
|
||||
return llamaParseClient;
|
||||
});
|
||||
builder.AddLlamaParseClient(apiKey: builder.Configuration.GetSection("LlamaParse")["ApiKey"]!);
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -35,7 +31,11 @@ var fileUploadHandler = async (LlamaParseClient client, IFormFile file) =>
|
||||
{
|
||||
var fileName = file.FileName;
|
||||
|
||||
var inMemoryFile = new InMemoryFile(File.ReadAllBytes(fileName), fileName);
|
||||
// Read the file into a byte array
|
||||
using var ms = new MemoryStream();
|
||||
file.CopyTo(ms);
|
||||
|
||||
var inMemoryFile = new InMemoryFile(ms.ToArray(), fileName);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
await foreach (var doc in client.LoadDataAsync(inMemoryFile))
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LlamaParse\LlamaParse.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
using LlamaParse;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Microsoft.Extensions.Hosting;
|
||||
|
||||
public static class AspireLlamaParseExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a LlamaParseClient to the service collection
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
|
||||
/// <param name="apiKey">The LlamaCloud API key</param>
|
||||
public static void AddLlamaParseClient(this IHostApplicationBuilder builder, string apiKey)
|
||||
{
|
||||
builder.Services.AddSingleton(p => ConfigureClient(p,apiKey));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a keyed LlamaParseClient to the service collection
|
||||
/// </summary>
|
||||
/// <param name="builder">The <see cref="IHostApplicationBuilder" /> to read config from and add services to.</param>
|
||||
/// <param name="apiKey">The LlamaCloud API key</param>
|
||||
/// <param name="name">The service keyed name</param>
|
||||
public static void AddKeyedLlamaParseClient(this IHostApplicationBuilder builder, string apiKey, string name)
|
||||
{
|
||||
builder.Services.AddKeyedSingleton<LlamaParseClient>(name, (p,_) => ConfigureClient(p,apiKey));
|
||||
}
|
||||
|
||||
private static LlamaParseClient ConfigureClient(IServiceProvider provider, string apiKey)
|
||||
{
|
||||
var client = provider.GetRequiredService<HttpClient>();
|
||||
var llamaParseClient = new LlamaParseClient(client, apiKey: apiKey);
|
||||
return llamaParseClient;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user