-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
examples/LangChain.Samples.LocalRAG/LangChain.Samples.LocalRAG.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); |