Samples in c# of how tools from classes like in Semantic Kernel #1155
Replies: 3 comments 4 replies
-
There are lots of ways, all customizable. For example, if you wanted to add all instance and static from a particular type, you can have a method somewhere in your project with the desired semantics, e.g. static IEnumerable<AITool> GetTools<T>(T instance) =>
from m in typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
select AIFunctionFactory.Create(m, instance);or if you wanted a type to itself expose what should be used, possibly with dynamism to control which tools are provided based on conditions, e.g. class Test
{
public string Echo(string input) => input;
public static int Add(int a, int b) => a + b;
public IEnumerable<AITool> GetTools()
{
yield return AIFunctionFactory.Create(Echo);
if (Environment.ProcessorCount > 4)
{
yield return AIFunctionFactory.Create(Add);
}
}
}Etc. |
Beta Was this translation helpful? Give feedback.
-
|
yeah I assumed it would be something like this. Just asking if there was some kind of recommended way. public class MyPlugin(IMyService someService)
{
[KernelFunction]
public string SomeFunction() => someService.SomeMethod();
}and while service locator it's not generally recommended... it's probably at least for now what we have to do with Microsoft Agent Service? With your GetTools method, if we want DI it would be something like this? var tools = GetTools(services.GetRequiredService<SomeYeOldeSKPlugin>())
AIAgent agent = chatClient.CreateAIAgent(tools: tools);(yes I also could define a class to inject by constructor all the plugins I need I guess..) Again, just asking in case there is some other better way. If this is how it is, we'll deal with it. |
Beta Was this translation helpful? Give feedback.
-
|
If I am not wrong, all examples here cover DI handling for constructors parameters, but SK handled also DI for method parameters. Most of the time one does not need DI for parameters BUT i have met a scenario using MCP SDK where injecting the same type as parameter OR in the constructor did make a difference (since the object contained a property that was something like a session state for a callback . . maybe it was a bad design of the MCP SDK .. still ) Enrico |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've checked your samples and Semantic Kernel Migration guide and there are no samples on how to add tools when methods are defined in a class. All your samples use top level statement but most apps are not just a Program.cs
Also a small helper to add all methods in a plugin class as tools would be useful to the migration from your previous oficial AI Library until 2 days ago.
Beta Was this translation helpful? Give feedback.
All reactions