-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Hook for chain execution tracing (#98)
- Loading branch information
Showing
6 changed files
with
165 additions
and
18 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
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
102 changes: 102 additions & 0 deletions
102
src/libs/LangChain.Core/Chains/StackableChains/Hooks/ConsoleTraceHook.cs
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,102 @@ | ||
| ||
using LangChain.Chains.HelperChains; | ||
|
||
namespace LangChain.Chains.StackableChains.Context; | ||
|
||
public class ConsoleTraceHook: StackableChainHook | ||
{ | ||
public bool UseColors { get; set; }=true; | ||
public int ValuesLength { get; set; } = 40; | ||
public override void ChainStart(StackableChainValues values) | ||
Check warning on line 10 in src/libs/LangChain.Core/Chains/StackableChains/Hooks/ConsoleTraceHook.cs
|
||
{ | ||
Console.WriteLine(); | ||
} | ||
public override void LinkEnter(BaseStackableChain chain, StackableChainValues values) | ||
Check warning on line 14 in src/libs/LangChain.Core/Chains/StackableChains/Hooks/ConsoleTraceHook.cs
|
||
{ | ||
|
||
Console.Write("|"); | ||
Console.Write(chain.GetType().Name); | ||
Console.WriteLine(); | ||
if (chain.InputKeys.Count > 0) | ||
{ | ||
Console.Write("|"); | ||
Console.Write(" "); | ||
Console.Write("\u2514"); | ||
Console.Write("Input:"); | ||
Console.WriteLine(); | ||
foreach (string inputKey in chain.InputKeys) | ||
{ | ||
Console.Write("|"); | ||
Console.Write(" "); | ||
Console.Write("\u2514"); | ||
var value = values.Value[inputKey]; | ||
var oldColor = Console.ForegroundColor; | ||
Console.ForegroundColor = GetColorForKey(inputKey); | ||
Console.Write($" {inputKey}={ShortenString(value.ToString() ?? "", ValuesLength)}"); | ||
Console.ForegroundColor = oldColor; | ||
Console.WriteLine(); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
Dictionary<string, ConsoleColor> _colorMap = new Dictionary<string, ConsoleColor>(); | ||
|
||
ConsoleColor GetColorForKey(string key) | ||
{ | ||
if(!UseColors) | ||
return Console.ForegroundColor; | ||
// if key is not in map, get unique color(except black and white) | ||
// if there no unique colors left, return white | ||
if (!_colorMap.ContainsKey(key)) | ||
{ | ||
var color = ConsoleColor.White; | ||
var colors = Enum.GetValues(typeof(ConsoleColor)); | ||
foreach (ConsoleColor c in colors) | ||
{ | ||
if (c == ConsoleColor.Black || c == ConsoleColor.White) | ||
continue; | ||
if (!_colorMap.ContainsValue(c)) | ||
{ | ||
color = c; | ||
break; | ||
} | ||
} | ||
_colorMap.Add(key, color); | ||
} | ||
return _colorMap[key]; | ||
} | ||
|
||
string ShortenString(string str, int length) | ||
{ | ||
if (str.Length <= length) | ||
return str; | ||
return str.Substring(0, length - 3) + "..."; | ||
} | ||
|
||
public override void LinkExit(BaseStackableChain chain, StackableChainValues values) | ||
Check warning on line 78 in src/libs/LangChain.Core/Chains/StackableChains/Hooks/ConsoleTraceHook.cs
|
||
{ | ||
if (chain.OutputKeys.Count > 0) | ||
{ | ||
Console.Write("|"); | ||
Console.Write(" "); | ||
Console.Write("\u2514"); | ||
Console.Write("Output:"); | ||
Console.WriteLine(); | ||
foreach (string outputKey in chain.OutputKeys) | ||
{ | ||
Console.Write("|"); | ||
Console.Write(" "); | ||
Console.Write("\u2514"); | ||
var value = values.Value[outputKey]; | ||
var oldColor = Console.ForegroundColor; | ||
Console.ForegroundColor = GetColorForKey(outputKey); | ||
Console.Write($" {outputKey}={ShortenString(value.ToString() ?? "", ValuesLength)}"); | ||
Console.ForegroundColor = oldColor; | ||
Console.WriteLine(); | ||
} | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/libs/LangChain.Core/Chains/StackableChains/Hooks/StackableChainHook.cs
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,21 @@ | ||
using LangChain.Chains.HelperChains; | ||
|
||
namespace LangChain.Chains.StackableChains.Context; | ||
|
||
public class StackableChainHook | ||
{ | ||
public virtual void ChainStart(StackableChainValues values) | ||
{ | ||
|
||
} | ||
|
||
public virtual void LinkEnter(BaseStackableChain chain, StackableChainValues values) | ||
{ | ||
|
||
} | ||
|
||
public virtual void LinkExit(BaseStackableChain chain, StackableChainValues values) | ||
{ | ||
|
||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/libs/LangChain.Core/Chains/StackableChains/Hooks/StackableChainValues.cs
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,8 @@ | ||
using LangChain.Schema; | ||
|
||
namespace LangChain.Chains.StackableChains.Context; | ||
|
||
public class StackableChainValues : ChainValues | ||
{ | ||
public StackableChainHook? Hook { get; set; } | ||
} |
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