Skip to content

Commit

Permalink
sample: Added Local RAG sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 13, 2024
1 parent d532028 commit 9ecb3f6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LangChain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Serve.OpenAI", "s
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Samples.Serve.OpenAI", "examples\LangChain.Samples.Serve.OpenAI\LangChain.Samples.Serve.OpenAI.csproj", "{B5D7CED5-EF1E-4539-9220-1ABBA2A70AE4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangChain.Samples.LocalRAG", "examples\LangChain.Samples.LocalRAG\LangChain.Samples.LocalRAG.csproj", "{3BCE1801-0354-4062-94E1-A3AE9CF82874}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -714,6 +716,10 @@ Global
{B5D7CED5-EF1E-4539-9220-1ABBA2A70AE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5D7CED5-EF1E-4539-9220-1ABBA2A70AE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5D7CED5-EF1E-4539-9220-1ABBA2A70AE4}.Release|Any CPU.Build.0 = Release|Any CPU
{3BCE1801-0354-4062-94E1-A3AE9CF82874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BCE1801-0354-4062-94E1-A3AE9CF82874}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BCE1801-0354-4062-94E1-A3AE9CF82874}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BCE1801-0354-4062-94E1-A3AE9CF82874}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -854,6 +860,7 @@ Global
{ADB8BA40-F8AA-41C9-A56E-0550061A4D71} = {7CA5B2C7-37ED-47CA-92F2-E599370F0DCB}
{7538A00F-C5A1-4D74-A264-FA4E6861A19B} = {C5C6EB91-CF9B-48F8-A198-9D9065E65BD1}
{B5D7CED5-EF1E-4539-9220-1ABBA2A70AE4} = {F17A86AE-A174-4B6C-BAA7-9D9A9704BE85}
{3BCE1801-0354-4062-94E1-A3AE9CF82874} = {F17A86AE-A174-4B6C-BAA7-9D9A9704BE85}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5C00D0F1-6138-4ED9-846B-97E43D6DFF1C}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Databases\Sqlite\src\LangChain.Databases.Sqlite.csproj" />
<ProjectReference Include="..\..\src\DocumentLoaders\Pdf\src\LangChain.DocumentLoaders.Pdf.csproj" />
<ProjectReference Include="..\..\src\Meta\src\LangChain.csproj" />
<ProjectReference Include="..\..\src\Providers\Ollama\src\LangChain.Providers.Ollama.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions examples/LangChain.Samples.LocalRAG/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using LangChain.Databases.Sqlite;
using LangChain.DocumentLoaders;
using LangChain.Providers.Ollama;
using LangChain.Extensions;
using Ollama;

var provider = new OllamaProvider(options: new RequestOptions
{
Stop = ["\n"],
Temperature = 0.0f,
});
var embeddingModel = new OllamaEmbeddingModel(provider, id: "all-minilm");
var llm = new OllamaChatModel(provider, id: "llama3");

var vectorDatabase = new SqLiteVectorDatabase(dataSource: "vectors.db");

var vectorCollection = await vectorDatabase.AddDocumentsFromAsync<PdfPigPdfLoader>(
embeddingModel, // Used to convert text to embeddings
dimensions: 1536, // Should be 1536 for TextEmbeddingV3SmallModel
dataSource: DataSource.FromUrl("https://canonburyprimaryschool.co.uk/wp-content/uploads/2016/01/Joanne-K.-Rowling-Harry-Potter-Book-1-Harry-Potter-and-the-Philosophers-Stone-EnglishOnlineClub.com_.pdf"),
collectionName: "harrypotter", // Can be omitted, use if you want to have multiple collections
textSplitter: null,
behavior: AddDocumentsToDatabaseBehavior.JustReturnCollectionIfCollectionIsAlreadyExists);


const string question = "What is Harry's Address?";
var similarDocuments = await vectorCollection.GetSimilarDocuments(embeddingModel, question, amount: 5);
// Use similar documents and LLM to answer the question
var answer = await llm.GenerateAsync(
$"""
Use the following pieces of context to answer the question at the end.
If the answer is not in context then just say that you don't know, don't try to make up an answer.
Keep the answer as short as possible.

{similarDocuments.AsString()}

Question: {question}
Helpful Answer:
""").ConfigureAwait(false);

Console.WriteLine($"LLM answer: {answer}");

0 comments on commit 9ecb3f6

Please sign in to comment.