LangChain .NET supports a functional, composable syntax for building pipelines called Chains. Chains allow you to pipe operations together using the | operator, creating a declarative workflow.
Common chain components include:
Set(value): Sets the initial input value.RetrieveSimilarDocuments(collection, embeddingModel, amount): Fetches relevant documents from a vector collection.CombineDocuments(outputKey): Merges retrieved documents into a single string assigned to a specific key.Template(promptTemplate): Injects variables into a prompt string.LLM(model): Sends the final prompt to the language model.
To execute a chain, use await chain.RunAsync(inputKey).
var promptTemplate = @"Use the following context: {context}\nQuestion: {text}\nAnswer:";
var chain =
Set("Who was drinking a unicorn blood?")
| RetrieveSimilarDocuments(vectorCollection, embeddingModel, amount: 5)
| CombineDocuments(outputKey: "context")
| Template(promptTemplate)
| LLM(llm.UseConsoleForDebug());
var chainAnswer = await chain.RunAsync("text");