Archived
forked from RDTKEditor/RDTKEditor
103 lines
3.0 KiB
C#
103 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using CSharpToJsonSchema;
|
|
using Ollama;
|
|
using RhythmBase.RhythmDoctor.Components;
|
|
using System.ComponentModel;
|
|
|
|
namespace RDTKEditor.AIAssistant
|
|
{
|
|
public class Assistant
|
|
{
|
|
public const string Deepseek = "deepseek-r1:14b";
|
|
public const string Smollm = "smollm2:135m";
|
|
public const string Qwen_0_5 = "qwen2.5-coder:0.5b";
|
|
public const string Qwen_3 = "qwen2.5-coder:7b";
|
|
public OllamaApiClient Ollama { get; } = new(baseUri: new Uri("http://127.0.0.1:11434/api"));
|
|
public Chat Chat { get; }
|
|
public Assistant(string modelName)
|
|
{
|
|
Chat = Ollama.Chat(
|
|
model: modelName,
|
|
autoCallTools: true
|
|
);
|
|
var utils = new RhythmBaseUtils();
|
|
Chat.AddToolService(utils.AsTools().AsOllamaTools(), utils.AsCalls());
|
|
}
|
|
public async IAsyncEnumerable<GenerateCompletionResponse> GetResponse(string modelName, string userInput)
|
|
{
|
|
await foreach (var responce in Ollama.Completions.GenerateCompletionAsync(modelName, userInput))
|
|
{
|
|
if (responce != null)
|
|
{
|
|
yield return responce;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
[GenerateJsonSchema]
|
|
public interface IRhythmBaseUtils
|
|
{
|
|
[Description("创建空关卡")]
|
|
public void CreateEmptyLevel();
|
|
[Description("创建关卡模板(默认关卡)")]
|
|
public void CreateLevelTemplate();
|
|
[Description("打开关卡")]
|
|
public void OpenLevel([Description("关卡路径")] string filepath);
|
|
[Description("添加轨道")]
|
|
public void AddRow(
|
|
[Description("轨道所在的房间")] RDRoomIndex roomIndex,
|
|
[Description("轨道名称")] string trackName
|
|
);
|
|
[Description("保存关卡")]
|
|
public void SaveLevel([Description("关卡路径,后缀为 .rdlevel")] string filepath);
|
|
}
|
|
public class RhythmBaseUtils : IRhythmBaseUtils
|
|
{
|
|
private RDLevel? level;
|
|
public void CreateEmptyLevel()
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"Called CreateEmptyLevel");
|
|
Console.ResetColor();
|
|
level = [];
|
|
}
|
|
public void CreateLevelTemplate()
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"Called CreateLevelTemplate");
|
|
Console.ResetColor();
|
|
level = RDLevel.Default;
|
|
}
|
|
public void OpenLevel(string filepath)
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"Called OpenLevel: {filepath}");
|
|
Console.ResetColor();
|
|
level = RDLevel.Read(filepath);
|
|
}
|
|
public void AddRow(RDRoomIndex roomIndex, string trackName)
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"Called AddRow: [{roomIndex}] {trackName}");
|
|
Console.ResetColor();
|
|
level?.CreateRow(roomIndex, trackName);
|
|
}
|
|
public void SaveLevel(string filepath)
|
|
{
|
|
Console.BackgroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"Called SaveLevel: {filepath}");
|
|
Console.ResetColor();
|
|
string dir = Path.GetDirectoryName(filepath) ?? "";
|
|
if (!string.IsNullOrEmpty(dir))
|
|
{
|
|
Directory.CreateDirectory(dir);
|
|
}
|
|
level?.Write(filepath);
|
|
}
|
|
}
|
|
}
|