SequentialChain

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using LangChain.Abstractions.Chains.Base;
using LangChain.Chains.LLM;
using LangChain.Chains.Sequentials;
using LangChain.Prompts;
using LangChain.Providers.OpenAI.Predefined;
using LangChain.Schema;

using var httpClient = new HttpClient();
var llm = new OpenAiLatestFastChatModel("api-key");

var firstTemplate = "What is a good name for a company that makes {product}?";
var firstPrompt = new PromptTemplate(new PromptTemplateInput(firstTemplate, new List<string>(1) { "product" }));

var chainOne = new LlmChain(new LlmChainInput(llm, firstPrompt)
{
    Verbose = true,
    OutputKey = "company_name"
});

var secondTemplate = "Write a 20 words description for the following company:{company_name}";
var secondPrompt = new PromptTemplate(new PromptTemplateInput(secondTemplate, new List<string>(1) { "company_name" }));

var chainTwo = new LlmChain(new LlmChainInput(llm, secondPrompt));

var overallChain = new SequentialChain(new SequentialChainInput(
    new IChain[]
    {
        chainOne,
        chainTwo
    },
    new[] { "product" },
    new[] { "company_name", "text" }
));

var result = await overallChain.CallAsync(new ChainValues(new Dictionary<string, object>(1)
{
    { "product", "colourful socks" }
}));

Console.WriteLine(result.Value["text"]);
Console.WriteLine("SequentialChain sample finished.");