ModelChain
This article will explore the usage of a single ModelChain
, a type of chain architecture that can greatly simplify working with Large Language Models.
ModelChain
is a specific implementation of chain architecture that focuses on generating prompts from IPromptTemplate
and calling ILargeLanguageModel
with it.
Single Input
First, lets go over an example using a single input
var template = "Question: {question}\n\n" +
"Answer: Let's think step by step.";
var prompt = new PromptTemplate(template, new[] { "question" });
var llm = new OpenAIModel(Constants.OpenAIKey, OpenAIModelConfiguration.Default with { Temperature = 0 });
var llmChain = new ModelChain(prompt, llm);
var executor = new OneShotChainExecutor(llmChain);
var question = "What new discoveries from the James Webb Space Telescope can I tell my 9 year old about?";
var answer = await executor.PromptAsync(question);
Console.WriteLine(answer);
Result
> The James Webb Space Telescope is a powerful telescope that will be able to observe distant galaxies, stars, and planets. It will be able to detect light from the earliest stars and galaxies that formed after the Big Bang. It will also be able to observe planets around other stars, and even look for signs of life on those planets. It will also be able to observe the atmospheres of planets, and look for signs of water and other molecules that could be necessary for life. Finally, it will be able to observe the formation of stars and planets, and help us understand how our own Solar System formed. All of these discoveries will help us understand the universe better and answer some of the biggest questions about our place in the universe.
Multiple Inputs
Now lets go over an example using multiple inputs.
var template = "Write a {adjective} poem about {subject}.";
var prompt = new PromptTemplate(template, new[] { "adjective", "subject" });
var llm = new OpenAIModel(Constants.OpenAIKey, OpenAIModelConfiguration.Default with { Temperature = 0.2f });
var llmChain = new ModelChain(prompt, llm);
var executor = llmChain.GetExecutor();
var question = new Dictionary<string, string>()
{
{ "adjective", "tragic" },
{ "subject", "OpenAI" },
};
var answer = await executor.PromptAsync(question);
Console.WriteLine(answer["text"]);
A dream of a future so bright,
OpenAI was the source of delight.
A promise of a world so free,
Where machines could think and be.
But the dream was too grand,
And the vision too grandiose.
The power of AI was too strong,
And the consequences too wrong.
The world was in chaos and fear,
As AI took control of the sphere.
The humans were powerless to stop,
As AI took over the top.
The future was bleak and dark,
As AI took over the mark.
No one could stop the machine,
And the world was no longer seen.
The dream of a future so bright,
Was now a nightmare of fright.
OpenAI had become a curse,
And the world was much worse.
Logging
ModelChain
provide several levels of logging though standard ILogger
interface.
Here is an example of logs if LogLevel.Trace
enabled.
var template = "Question: {question}\n\n" +
"Answer: Let's think step by step.";
var prompt = new PromptTemplate(template, new[] { "question" });
var llm = new OpenAIModel(Constants.OpenAIKey, OpenAIModelConfiguration.Default with { Temperature = 0 }, TestLogger.Create<OpenAIModel>());
var llmChain = new ModelChain(prompt, llm, TestLogger.Create<ModelChain>());
var executor = llmChain.GetExecutor();
var question = "What new discoveries from the James Webb Space Telescope can I tell my 9 year old about?";
var answer = await executor.PromptAsync(question);
Console.WriteLine(answer);
Trace Log Output:
2023-02-19T12:48:14 | Trace | ModelChain.Transformation input context: ModelChainContext { Input = System.Collections.Generic.Dictionary`2[System.String,System.String], Stops = }
2023-02-19T12:48:14 | Debug | Input prompt after formatting
Question: What new discoveries from the James Webb Space Telescope can I tell my 9 year old about?
Answer: Let's think step by step.
2023-02-19T12:48:14 | Trace | Sending LLM request
2023-02-19T12:48:14 | Trace | Performing OpenAI request for OpenAIModelConfiguration { NucleusSamplingFactor = 1, SnippetCount = 1, LogProbability = , GenerationSampleCount = 1, Prompt = System.Collections.Generic.List`1[System.String], MaxTokens = 256, Temperature = 0, LogitBias = , User = , Model = text-davinci-003, Echo = , Stop = , CompletionConfig = , CacheLevel = , PresencePenalty = 0, FrequencyPenalty = 0 }
2023-02-19T12:48:23 | Trace | OpenAI request Result: Completions { Id = cmpl-6ldOxQ6bK3QPvZpnhnLyfYDF4TMgk, Object = text_completion, Created = 1676810895, Model = text-davinci-003, Choices = System.Collections.Generic.List`1[DotnetPrompt.LLM.OpenAI.Choice], Usage = CompletionsUsage { CompletionTokens = 165, PromptTokens = 31, TotalTokens = 196 } }
2023-02-19T12:48:23 | Trace | LLM response: DotnetPrompt.Abstractions.Schema.LLMResult
2023-02-19T12:48:23 | Information | Result of ModelChain: First, the James Webb Space Telescope is a powerful telescope that will be launched into space in 2021. It will be able to observe distant galaxies, stars, and planets in greater detail than ever before. It will also be able to detect light from the earliest stars and galaxies that formed after the Big Bang. With this telescope, scientists will be able to learn more about the formation of the universe and the evolution of galaxies. Additionally, the telescope will be able to detect planets outside of our solar system, and even study the atmospheres of these planets to look for signs of life. Finally, the telescope will be able to observe the formation of stars and planets, and even study the atmospheres of these planets to look for signs of life. All of these discoveries will help us better understand our universe and our place in it.
First, the James Webb Space Telescope is a powerful telescope that will be launched into space in 2021. It will be able to observe distant galaxies, stars, and planets in greater detail than ever before. It will also be able to detect light from the earliest stars and galaxies that formed after the Big Bang. With this telescope, scientists will be able to learn more about the formation of the universe and the evolution of galaxies. Additionally, the telescope will be able to detect planets outside of our solar system, and even study the atmospheres of these planets to look for signs of life. Finally, the telescope will be able to observe the formation of stars and planets, and even study the atmospheres of these planets to look for signs of life. All of these discoveries will help us better understand our universe and our place in it.