♻️ Replace the LangChain with Semantic Kernel
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
using LangChain.Providers;
|
||||
using LangChain.Providers.Ollama;
|
||||
using Microsoft.SemanticKernel;
|
||||
|
||||
namespace DysonNetwork.Insight.Thinking;
|
||||
|
||||
public class ThinkingProvider
|
||||
{
|
||||
public readonly Provider Provider;
|
||||
public readonly Kernel Kernel;
|
||||
public readonly string? ModelProviderType;
|
||||
public readonly string? ModelDefault;
|
||||
|
||||
@@ -13,25 +12,37 @@ public class ThinkingProvider
|
||||
{
|
||||
var cfg = configuration.GetSection("Thinking");
|
||||
ModelProviderType = cfg.GetValue<string>("Provider")?.ToLower();
|
||||
ModelDefault = cfg.GetValue<string>("Model");
|
||||
var endpoint = cfg.GetValue<string>("Endpoint");
|
||||
|
||||
var builder = Kernel.CreateBuilder();
|
||||
|
||||
switch (ModelProviderType)
|
||||
{
|
||||
case "ollama":
|
||||
var endpoint = cfg.GetValue<string>("Endpoint");
|
||||
Provider = new OllamaProvider(endpoint ?? "http://localhost:11434/api");
|
||||
builder.AddOllamaChatCompletion(ModelDefault!, new Uri(endpoint ?? "http://localhost:11434/api"));
|
||||
break;
|
||||
default:
|
||||
throw new IndexOutOfRangeException("Unknown thinking provider: " + ModelProviderType);
|
||||
}
|
||||
|
||||
ModelDefault = cfg.GetValue<string>("Model");
|
||||
}
|
||||
Kernel = builder.Build();
|
||||
|
||||
public ChatModel GetModel(string? name = null)
|
||||
{
|
||||
return ModelProviderType switch
|
||||
{
|
||||
"ollama" => new OllamaChatModel((Provider as OllamaProvider)!, (name ?? ModelDefault)!),
|
||||
_ => throw new IndexOutOfRangeException("Unknown thinking provider: " + ModelProviderType),
|
||||
};
|
||||
// Add Solar Network tools plugin
|
||||
Kernel.ImportPluginFromFunctions("helper_functions", [
|
||||
KernelFunctionFactory.CreateFromMethod(async (string userId) =>
|
||||
{
|
||||
// MOCK: simulate fetching user profile
|
||||
await Task.Delay(100);
|
||||
return $"{{\"userId\":\"{userId}\",\"name\":\"MockUser\",\"bio\":\"Loves music and tech.\"}}";
|
||||
}, "get_user_profile", "Get a user profile from the Solar Network."),
|
||||
KernelFunctionFactory.CreateFromMethod(async (string topic) =>
|
||||
{
|
||||
// MOCK: simulate fetching recent posts
|
||||
await Task.Delay(200);
|
||||
return
|
||||
$"[{{\"postId\":\"p1\",\"topic\":\"{topic}\",\"content\":\"Mock post content 1.\"}}, {{\"postId\":\"p2\",\"topic\":\"{topic}\",\"content\":\"Mock post content 2.\"}}]";
|
||||
}, "get_recent_posts", "Get recent posts from the Solar Network.")
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user