mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-03-31 18:38:22 +00:00
LLM
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Esiur\Esiur.csproj" />
|
||||
<ProjectReference Include="..\..\Esiur\Esiur.csproj" OutputItemType="Analyzer"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
12
Tests/Annotations/LlmDecision.cs
Normal file
12
Tests/Annotations/LlmDecision.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Tests.Annotations
|
||||
{
|
||||
public sealed class LlmDecision
|
||||
{
|
||||
public string? Function { get; set; }
|
||||
public string? Reason { get; set; }
|
||||
}
|
||||
}
|
||||
171
Tests/Annotations/LlmRunner.cs
Normal file
171
Tests/Annotations/LlmRunner.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using Esiur.Schema.Llm;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
using System;
|
||||
using System.ClientModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Esiur.Tests.Annotations
|
||||
{
|
||||
public class LlmRunner
|
||||
{
|
||||
public async Task RunAsync(ServiceNode node, string endpoint, ApiKeyCredential apiKey, string modelName,
|
||||
int tickDelayMs = 1000)
|
||||
{
|
||||
var client = new OpenAIClient(apiKey, new OpenAIClientOptions() { Endpoint = new Uri(endpoint) });
|
||||
var chat = client.GetChatClient("microsoft/phi-4");
|
||||
|
||||
var typeModel = LlmTypeModel.FromTypeDef(node.Instance?.Definition);
|
||||
|
||||
var ticks = new List<TickState>
|
||||
{
|
||||
new() { Load = 35, ErrorCount = 0, Enabled = true },
|
||||
new() { Load = 88, ErrorCount = 1, Enabled = true },
|
||||
new() { Load = 42, ErrorCount = 4, Enabled = true },
|
||||
new() { Load = 18, ErrorCount = 0, Enabled = false },
|
||||
new() { Load = 91, ErrorCount = 5, Enabled = true },
|
||||
new() { Load = 25, ErrorCount = 0, Enabled = true }
|
||||
};
|
||||
|
||||
for (int i = 0; i < ticks.Count; i++)
|
||||
{
|
||||
var tick = ticks[i];
|
||||
|
||||
// Simulate property changes for this tick
|
||||
node.Load = tick.Load;
|
||||
node.ErrorCount = tick.ErrorCount;
|
||||
node.Enabled = tick.Enabled;
|
||||
|
||||
var jsonModel = typeModel.ToJson(node);
|
||||
Console.WriteLine($"Tick {i + 1}");
|
||||
Console.WriteLine($"State: Load={node.Load}, ErrorCount={node.ErrorCount}, Enabled={node.Enabled}");
|
||||
|
||||
var prompt = BuildPrompt(jsonModel, node, i + 1);
|
||||
|
||||
string llmRaw = await InferAsync(chat, prompt);
|
||||
var decision = ParseDecision(llmRaw);
|
||||
|
||||
bool invoked = InvokeIfValid(node, decision?.Function);
|
||||
|
||||
Console.WriteLine($"LLM: {llmRaw}");
|
||||
Console.WriteLine($"Invoked: {invoked}");
|
||||
Console.WriteLine($"After: Load={node.Load}, ErrorCount={node.ErrorCount}, Enabled={node.Enabled}");
|
||||
Console.WriteLine(new string('-', 60));
|
||||
|
||||
await Task.Delay(tickDelayMs);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async Task<string> InferAsync(
|
||||
ChatClient chat,
|
||||
string prompt)
|
||||
{
|
||||
|
||||
List<ChatMessage> messages = new List<ChatMessage>
|
||||
{
|
||||
new SystemChatMessage("You control a distributed resource. " +
|
||||
"Return only JSON with fields: function and reason."),
|
||||
new UserChatMessage(prompt)
|
||||
};
|
||||
|
||||
var result = await chat.CompleteChatAsync(messages);
|
||||
|
||||
return result.Value.Content[0].Text;
|
||||
}
|
||||
private static string BuildPrompt(string typeDefJson, ServiceNode node, int tick)
|
||||
{
|
||||
return
|
||||
$@"You are given a runtime type definition for a distributed resource and its current state.
|
||||
Choose at most one function to call.
|
||||
Use only the functions defined in the type definition.
|
||||
Do not invent functions.
|
||||
If no action is needed, return function as null.
|
||||
Return only JSON in this format:
|
||||
{{ ""function"": ""Restart|ResetErrors|Enable|Disable|null"", ""reason"": ""short explanation"" }}
|
||||
|
||||
Type Definition:
|
||||
{typeDefJson}";
|
||||
|
||||
//Current Tick: {tick}
|
||||
//Current State:
|
||||
//{{
|
||||
// ""Load"": {node.Load},
|
||||
// ""ErrorCount"": {node.ErrorCount},
|
||||
// ""Enabled"": {(node.Enabled ? "true" : "false")}
|
||||
//}}";
|
||||
}
|
||||
|
||||
private static LlmDecision? ParseDecision(string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = ExtractJson(text);
|
||||
|
||||
return JsonSerializer.Deserialize<LlmDecision>(
|
||||
json,
|
||||
new JsonSerializerOptions
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string ExtractJson(string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
return "{}";
|
||||
|
||||
text = text.Trim();
|
||||
|
||||
if (text.StartsWith("```"))
|
||||
{
|
||||
var firstNewline = text.IndexOf('\n');
|
||||
if (firstNewline >= 0)
|
||||
text = text[(firstNewline + 1)..];
|
||||
|
||||
var lastFence = text.LastIndexOf("```", StringComparison.Ordinal);
|
||||
if (lastFence >= 0)
|
||||
text = text[..lastFence];
|
||||
}
|
||||
|
||||
return text.Trim();
|
||||
}
|
||||
|
||||
private static bool InvokeIfValid(ServiceNode node, string? functionName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(functionName) ||
|
||||
string.Equals(functionName, "null", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
switch (functionName)
|
||||
{
|
||||
case "Restart":
|
||||
node.Restart();
|
||||
return true;
|
||||
|
||||
case "ResetErrors":
|
||||
node.ResetErrors();
|
||||
return true;
|
||||
|
||||
case "Enable":
|
||||
node.Enable();
|
||||
return true;
|
||||
|
||||
case "Disable":
|
||||
node.Disable();
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,32 @@
|
||||
// The endpoint for LM Studio's local server
|
||||
using Esiur.Resource;
|
||||
using Esiur.Stores;
|
||||
using Esiur.Tests.Annotations;
|
||||
using OpenAI;
|
||||
using OpenAI.Chat;
|
||||
using System.ClientModel;
|
||||
using System.Data;
|
||||
|
||||
var wh = new Warehouse();
|
||||
|
||||
await wh.Put("store", new MemoryStore());
|
||||
var node = await wh.Put("store/service", new ServiceNode());
|
||||
|
||||
var endpoint = "http://localhost:1234/v1";
|
||||
var credential = new ApiKeyCredential("lm-studio");
|
||||
|
||||
var client = new OpenAIClient(credential, new OpenAIClientOptions() { Endpoint = new Uri(endpoint) });
|
||||
//var client = new OpenAIClient(credential, new OpenAIClientOptions() { Endpoint = new Uri(endpoint) });
|
||||
|
||||
var chat = client.GetChatClient("microsoft/phi-4");
|
||||
//var chat = client.GetChatClient("microsoft/phi-4");
|
||||
|
||||
var llmRunner = new LlmRunner();
|
||||
|
||||
await llmRunner.RunAsync(
|
||||
node,
|
||||
endpoint,
|
||||
credential,
|
||||
"microsoft/phi-4"
|
||||
);
|
||||
|
||||
//List<ChatMessage> messages = new List<ChatMessage>
|
||||
//{
|
||||
@@ -20,8 +37,8 @@ var chat = client.GetChatClient("microsoft/phi-4");
|
||||
//// Send the entire conversation history
|
||||
//ChatCompletion completion = chat.CompleteChat(messages);
|
||||
|
||||
var response = await chat.CompleteChatAsync(
|
||||
"Explain what Pi means"
|
||||
);
|
||||
//var response = await chat.CompleteChatAsync(
|
||||
// "Explain what Pi means"
|
||||
//);
|
||||
|
||||
Console.WriteLine(response.Value.Content[0].Text);
|
||||
//Console.WriteLine(response.Value.Content[0].Text);
|
||||
51
Tests/Annotations/ServiceNode.cs
Normal file
51
Tests/Annotations/ServiceNode.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Esiur.Resource;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Tests.Annotations
|
||||
{
|
||||
[Annotation("Represents a managed service node with load, error count, and enable state. Functions control service operation.")]
|
||||
[Annotation("usage_rules", @"1.Choose at most one function per tick.
|
||||
2. Use only functions defined in the functions list.
|
||||
3. Do not invent properties or functions.
|
||||
4. Base the decision only on current property values and annotations.")]
|
||||
[Resource]
|
||||
public partial class ServiceNode
|
||||
{
|
||||
[Annotation("Current service load percentage from 0 to 100. Values above 80 indicate overload.")]
|
||||
[Export] int load;
|
||||
|
||||
[Annotation("Number of recent errors detected in the service. Values above 3 indicate instability.")]
|
||||
[Export] int errorCount;
|
||||
|
||||
[Annotation("True when the service is enabled and allowed to run. False means the service is disabled.")]
|
||||
[Export] bool enabled;
|
||||
|
||||
[Annotation("Restart the service when load is very high or when repeated errors indicate instability.")]
|
||||
[Export] public void Restart()
|
||||
{
|
||||
ErrorCount = 0;
|
||||
Load = 10;
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
[Annotation("Clear the error counter when errors were temporary and a restart is not required.")]
|
||||
[Export] public void ResetErrors()
|
||||
{
|
||||
ErrorCount = 0;
|
||||
}
|
||||
|
||||
[Annotation("Enable the service if it is disabled and should be running.")]
|
||||
[Export] public void Enable()
|
||||
{
|
||||
Enabled = true;
|
||||
}
|
||||
|
||||
[Annotation("Disable the service if it should stop processing requests.")]
|
||||
[Export] public void Disable()
|
||||
{
|
||||
Enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Tests/Annotations/TickState.cs
Normal file
14
Tests/Annotations/TickState.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Tests.Annotations
|
||||
{
|
||||
public sealed class TickState
|
||||
{
|
||||
public int Load { get; set; }
|
||||
public int ErrorCount { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user