Prompts

 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
42
43
44
45
46
using LangChain.Chains.LLM;
using LangChain.Prompts;
using LangChain.Providers.OpenAI.Predefined;
using LangChain.Schema;

var llm = new OpenAiLatestFastChatModel(Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);

var prompt = new PromptTemplate(new PromptTemplateInput(
    template: "What is a good name for a company that makes {product}?",
    inputVariables: ["product"]));

var chain = new LlmChain(new LlmChainInput(llm, prompt));

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

// The result is an object with a `text` property.
Console.WriteLine(result.Value["text"]);

// Since the LLMChain is a single-input, single-output chain, we can also call it with `run`.
// This takes in a string and returns the `text` property.
var result2 = await chain.RunAsync("colourful socks");

Console.WriteLine(result2);

var chatPrompt = ChatPromptTemplate.FromPromptMessages([
    SystemMessagePromptTemplate.FromTemplate(
        "You are a helpful assistant that translates {input_language} to {output_language}."),
    HumanMessagePromptTemplate.FromTemplate("{text}")
]);

var chainB = new LlmChain(new LlmChainInput(llm, chatPrompt)
{
    Verbose = true
});

var resultB = await chainB.CallAsync(new ChainValues(new Dictionary<string, object>(3)
{
    {"input_language", "English"},
    {"output_language", "French"},
    {"text", "I love programming"},
}));

Console.WriteLine(resultB.Value["text"]);