Add samples README

This commit is contained in:
luisquintanilla
2024-07-29 11:52:04 -04:00
committed by Diego Colombo
parent 143b84e981
commit c05354b2a4
3 changed files with 130 additions and 0 deletions
+65
View File
@@ -18,3 +18,68 @@ This sample shows how to add a [LlamaParse](https://docs.llamaindex.ai/en/stable
"ApiKey": "ADD-YOUR-KEY-HERE"
}
```
## Guide
1. In your Web API project `LlamaParseAspire`, add the following code to register the `LlamaParseClient`.
```csharp
builder.AddLlamaParseClient(builder.Configuration.GetSection("LlamaParse").Get<Configuration>()!);
```
1. Use the `LlamaParseClient` just like you would in any other application. In this case, the `/parse` endpoint handler takes a file as input, uses LlamaParse to extract the data, and returns the parsed results back to the user for further downstream processing.
```csharp
var fileUploadHandler = async (LlamaParseClient client, IFormFile file) =>
{
var fileName = file.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))
{
if(doc is ImageDocument)
{
continue;
}
else
{
sb.AppendLine(doc.Text);
}
}
return Results.Ok(sb.ToString());
};
```
## Enable telemetry
The LlamaParse .NET client SDK contains OpenTelemetry instrumentation to log traces and metrics related to LlamaParse jobs.
To enable it:
1. Add the following code to the `ConfigureOpenTelemetry` method in the `*.ServiceDefaults` project.
```csharp
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
//other metrics code...
// Add a meter for the LlamaParse namespace
metrics.AddMeter("LlamaParse");
})
.WithTracing(tracing =>
{
//other tracing code...
// Add a source for the LlamaParse namespace
tracing.AddSource("LlamaParse");
});
```
Now that this is configured, traces and metrics will begin to display in the Aspire dasboard. For more details, on [Aspire telemetry](https://learn.microsoft.com/dotnet/aspire/fundamentals/telemetry) and the [dashboard](https://learn.microsoft.com/dotnet/aspire/fundamentals/dashboard/overview?tabs=bash), see the documentation.
@@ -0,0 +1,56 @@
# ParseDocuments Sample
This samples shows off the basics you need to get started parsing documents in .NET using the LlamaParse .NET client SDK inside of a console application.
## Prerequisites
- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)
- [LlamaCloud API Key](https://docs.cloud.llamaindex.ai/llamacloud/getting_started/api_key)
- [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Visual Studio Code](https://code.visualstudio.com/Download)
## Guide
1. Configure your client
```csharp
var apiKey = Environment.GetEnvironmentVariable("LLAMACLOUD_API_KEY");
var parseConfig = new Configuration()
{
ApiKey = apiKey?? string.Empty
};
var llamaParseClient = new LlamaParseClient(new HttpClient(), parseConfig);
```
1. Use the client to parse your documents. In this case, we're using an `InMemoryFile`, which contains the document data `byte[]` from the paper [Attention is all you need](https://arxiv.org/pdf/1706.03762). For simplicity and further processing, we've opted to get the results in JSON format.
```csharp
var document = new InMemoryFile(documentData, "attention-is-all-you-need.pdf");
var parsedDocs = llamaParseClient.LoadDataRawAsync(document, ResultType.Json);
```
1. Extract parsed results and post-process. In this case, the code just takes the paginated results and prints them out to the console.
```csharp
await foreach (var parsedDoc in parsedDocs)
{
var serializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var result = JsonSerializer.Deserialize<ParseResult>(parsedDoc.Result, serializerOptions);
foreach(var page in result.Pages)
{
Console.WriteLine($"Page {page.Page}");
Console.WriteLine("-------------------");
Console.WriteLine(page.Text);
Console.WriteLine("-------------------");
}
}
public record ParseResult(PageContent[] Pages);
public record PageContent(int Page, string Text);
```
+9
View File
@@ -0,0 +1,9 @@
# LlamaIndex .NET Samples
This directory contains getting started for LlamaIndex
| Sample | Type | Description |
| --- | --- | --- |
| [ParseDocuments](../samples/GettingStarted/ParseDocuments/README.md) | Console | Console application that shows the basics of getting started with LlamaParse .NET client SDK |
| [LlamaParseAspire](../samples/Aspire/README.md) | Web (Aspire) | ASP.NET Core Web API with Aspire Orchestration enabled. Uses LlamaParse to parse documents and return response back to users. |