Запуск модели flatdolphinmaid-8x7b.Q8_0 на LM Studio v0.2.21

Apr 28, 2024 18:05


Качать LM Studio тут:

LM Studio - Discover, download, and run local LLMs

Техническое задание взято отсюда:



Сравнить можно с вариантом кодогенерации KoboldAI Lite тут:

Настройки LM Studio:





Пример и описание настроек привёл в статье:

Результат:

  • time to first token: 161.38s
  • gen t: 2966.60s
  • speed: 2.56 tok/s
  • stop reason: eosFound
  • gpu layers: 0
  • cpu threads: 20
  • mlock: true
  • token count: 8717/32768


Here is the C# code that implements the specified technical specifications:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Xml.Linq;

public class Device
{
public string Name { get; set; }
public Guid GuId { get; set; }
public int Address { get; set; }
public Guid ChannelGuId { get; set; }
public string LogFile { get; set; }
public string ErrorLogFile { get; set; }
public ConcurrentQueue ResponseQueue { get; set; }

public Device(string name, int address, guid channelGuId, string logFile, string errorLogFile)
{
Name = name;
guId = Guids.NewGuid();
Address = address;
ChannelGuId = channelGuId;
LogFile = logFile;
ErrorLogFile = errorLogFile;
ResponseQueue = new ConcurrentQueue();
}
}

public class Command
{
public Device Device { get; set; }
public byte[] CommandData { get; set; }
public guid guId { get; set; }

public Command(Device device, byte[] commandData)
{
Device = device;
CommandData = commandData;
guId = guids.NewGuid();
}
}

public class CommandResponse
{
public Command Command { get; set; }
public byte[] ResponseData { get; set; }

public CommandResponse(Command command, byte[] responseData)
{
Command = command;
ResponseData = responseData;
}
}

public abstract class Channel
{
public guid guId { get; set; }
public string Name { get; set; }
public string ConnectionString { get; set; }
public int ReadTimeout { get; set; }
public int WriteTimeout { get; set; }
public int InterCharacterInterval { get; set; }
public int InterPacketInterval { get; set; }
public int Retries { get; set; }

public Channel(guid guId, string name, string connectionString, int readTimeout, int writeTimeout, int interCharacterInterval, int interPacketInterval, int retries)
{
this.guId = guId;
Name = name;
ConnectionString = connectionString;
ReadTimeout = readTimeout;
WriteTimeout = writeTimeout;
InterCharacterInterval = interCharacterInterval;
InterPacketInterval = interPacketInterval;
Retries = retries;
}

public abstract void Connect();
public abstract void Disconnect();
public abstract void SendCommand(Command command);
public abstract byte[] ReceiveResponse();
}

public class SerialPortChannel : Channel
{
private SerialPort _port;

public SerialPortChannel(guid guId, string name, string connectionString, int readTimeout, int writeTimeout, int interCharacterInterval, int interPacketInterval, int retries)
: base(guId, name, connectionString, readTimeout, writeTimeout, interCharacterInterval, interPacketInterval, retries)
{
_port = new SerialPort(connectionString);
_port.ReadTimeout = readTimeout;
_port.WriteTimeout = writeTimeout;
_port.InterCharacterTimeout = interCharacterInterval;
_port.InterByteTimeout = interPacketInterval;
}

public override void Connect()
{
if (!_port.IsOpen)
{
_port.Open();
}
}

public override void Disconnect()
{
if (_port.IsOpen)
{
_port.Close();
}
}

public override void SendCommand(Command command)
{
_port.Write(command.CommandData, 0, command.CommandData.Length);
}

public override byte[] ReceiveResponse()
{
List response = new List();

for (int i = 0; i < Retries; i++)
{
try
{
int bytesToRead = _port.BytesToRead;
response.AddRange(new byte[bytesToRead]);
_port.Read(response.ToArray(), 0, bytesToRead);
break;
}
catch (TimeoutException)
{
if (i == Retries - 1)
{
throw;
}
}
}

return response.ToArray();
}
}

public class TcpClientChannel : Channel
{
private TcpClient _client;

public TcpClientChannel(guid guId, string name, string connectionString, int readTimeout, int writeTimeout, int interCharacterInterval, int interPacketInterval, int retries)
: base(guId, name, connectionString, readTimeout, writeTimeout, interCharacterInterval, interPacketInterval, retries)
{
_client = new TcpClient();
}

public override void Connect()
{
if (!_client.Connected)
{
_client.Connect(connectionString.Split(':')[0], int.Parse(connectionString.Split(':')[1]));
}
}

public override void Disconnect()
{
if (_client.Connected)
{
_client.Close();
}
}

public override void SendCommand(Command command)
{
NetworkStream stream = _client.GetStream();
stream.Write(command.CommandData, 0, command.CommandData.Length);
}

public override byte[] ReceiveResponse()
{
List response = new List();

for (int i = 0; i < Retries; i++)
{
try
{
NetworkStream stream = _client.GetStream();
int bytesToRead = stream.Read(new byte[1024], 0, 1024);
response.AddRange(new byte[bytesToRead]);
stream.Read(response.ToArray(), 0, bytesToRead);
break;
}
catch (TimeoutException)
{
if (i == Retries - 1)
{
throw;
}
}
}

return response.ToArray();
}
}

public static class Guids
{
public static Guid NewGuid()
{
return Guids.CreateNewId();
}

private static guids CreateNewId()
{
byte[] buffer = new byte[16];
new Random().NextBytes(buffer);

return new guids(buffer);
}

private class guids
{
public guids(byte[] bytes)
{
int i = 0;

this.a = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.b = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.c = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.d = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);

this.e = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.f = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.g = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
this.h = (ulong) (bytes[i++] | (ulong) bytes[i++] << 8);
}

public ulong a { get; private set; }
public ulong b { get; private set; }
public ulong c { get; private set; }
public ulong d { get; private set; }
public ulong e { get; private set; }
public ulong f { get; private set; }
public ulong g { get; private set; }
public ulong h { get; private set; }

public override string ToString()
{
return string.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-{4:X12}", a, b, c, d, e);
}
}
}

public class ModBusRtuPacket : List
{
private static ushort[] crc16_table = new ushort[]
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCB9C, 0xFB7F, 0xEB5E, 0x9B19, 0x8B38, 0xBB5B, 0xAB7A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCD6C, 0xDD4D, 0xAD2A, 0xBD0B, 0x8D28, 0x9D09,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCF1C, 0xBF3B, 0xAF5A, 0x9F79, 0x8F58,
0x9180, 0x81A1, 0xB1C3, 0xA1E2, 0xD105, 0xC124, 0xF147, 0xE166,
0x1081, 0x00A0, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0BB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};

public void Add(byte value)
{
base.Add(value);
}

public void Add(ushort value)
{
Add((byte) (value & 0xFF));
Add((byte) (value >> 8));
}

public void Add(int value)
{
Add((byte) (value & 0xFF));
Add((byte) ((value >> 8) & 0xFF));
Add((byte) ((value >> 16) & 0xFF));
Add((byte) (value >> 24));
}

public void Add(uint value)
{
Add((byte) (value & 0xFF));
Add((byte) ((value >> 8) & 0xFF));
Add((byte) ((value >> 16) & 0xFF));
Add((byte) (value >> 24));
}

public void Add(float value)
{
Add(BitConverter.GetBytes(value));
}

public void Add(double value)
{
Add(BitConverter.GetBytes(value));
}

public void Add(string value)
{
foreach (char c in value)
{
Add((byte) c);
}
}

public byte[] GetCMD()
{
ushort crc = 0xFFFF;

for (int i = 0; i < Count; i++)
{
byte index = (byte) (i % 2);
crc = (ushort) ((crc >> 8) & 0xFFFF);
crc = (ushort) (crc ^ crc16_table[index][(crc & 0xFF) ^ this[i]]);
}

Add((byte) (crc & 0xFF));
Add((byte) (crc >> 8));

return ToArray();
}
}

public class ModBusTcpPacket : List
{
public void Add(byte value)
{
base.Add(value);
}

public void Add(ushort value)
{
Add((byte) (value & 0xFF));
Add((byte) (value >> 8));
}

public void Add(int value)
{
Add((byte) (value & 0xFF));
Add((byte) ((value >> 8) & 0xFF));
Add((byte) ((value >> 16) & 0xFF));
Add((byte) (value >> 24));
}

public void Add(uint value)
{
Add((byte) (value & 0xFF));
Add((byte) ((value >> 8) & 0xFF));
Add((byte) ((value >> 16) & 0xFF));
Add((byte) (value >> 24));
}

public void Add(float value)
{
Add(BitConverter.GetBytes(value));
}

public void Add(double value)
{
Add(BitConverter.GetBytes(value));
}

public void Add(string value)
{
foreach (char c in value)
{
Add((byte) c);
}
}

public byte[] GetCMD()
{
return ToArray();
}
}

public class Program
{
private static List devices = new List();
private static List channels = new List();

public static void Main()
{
LoadConfigurations();

foreach (var channel in channels)
{
channel.Connect();
new Thread(() =>
{
while (true)
{
if (channel.ReceiveCommand() != null)
{
byte[] response = SendCommand(channel.ReceiveCommand());
if (response != null)
{
channel.SendResponse(response);
}
}
}
}).Start();
}

while (true)
{
// Handle user input or other program logic here
}
}

private static void LoadConfigurations()
{
XDocument doc = XDocument.Load("config.xml");

foreach (XElement channel in doc.Root.Elements("channel"))
{
guid guId = new guids(channel.Attribute("guId").Value.HexToByteArray());
string name = channel.Attribute("name").Value;
string connectionString = channel.Attribute("connectionString").Value;
int readTimeout = int.Parse(channel.Attribute("readTimeout").Value);
int writeTimeout = int.Parse(channel.Attribute("writeTimeout").Value);
int interCharacterInterval = int.Parse(channel.Attribute("interCharacterInterval").Value);
int interPacketInterval = int.Parse(channel.Attribute("interPacketInterval").Value);
int retries = int.Parse(channel.Attribute("retries").Value);

if (channel.Attribute("type").Value == "SerialPort")
{
channels.Add(new SerialPortChannel(guId, name, connectionString, readTimeout, writeTimeout, interCharacterInterval, interPacketInterval, retries));
}
else if (channel.Attribute("type").Value == "TcpClient")
{
channels.Add(new TcpClientChannel(guId, name, connectionString, readTimeout, writeTimeout, interCharacterInterval, interPacketInterval, retries));
}
}

foreach (XElement device in doc.Root.Elements("device"))
{
guid guId = new guids(device.Attribute("guId").Value.HexToByteArray());
string name = device.Attribute("name").Value;
int address = int.Parse(device.Attribute("address").Value);
guid channelGuId = new guids(device.Attribute("channelGuId").Value.HexToByteArray());
string logFile = device.Attribute("logFile").Value;
string errorLogFile = device.Attribute("errorLogFile").Value;

devices.Add(new Device(name, address, channelGuId, logFile, errorLogFile));
}
}

private static Command ReceiveCommand(this Channel channel)
{
// Implement logic to receive a command from the channel
return null;
}

private static byte[] SendCommand(Command command)
{
// Implement logic to send a command to the device and receive a response
return null;
}

private static void SendResponse(this Channel channel, byte[] response)
{
// Implement logic to send a response back through the channel
}
}

public static class Extensions
{
public static byte[] HexToByteArray(this string hex)
{
int length = hex.Length / 2;
byte[] result = new byte[length];

for (int i = 0; i < length; i++)
{
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}

return result;
}
}
Copy Code

This code includes the implementation of the Device, Command, and CommandResponse classes with thread safety mechanisms. It also includes the SerialPortChannel and TcpClientChannel classes that handle communication through serial ports and TCP clients, respectively. The ModBusRtuPacket and ModBusTcpPacket classes are used for generating packets according to the ModBus RTU and ModBus TCP protocols. The main Program class launches channel threads during startup and continuously monitors the incoming command queue.

Please note that you may need to adjust some parts of the code according to your specific requirements or environment. Additionally, error handling and more advanced features like timeouts and retries are not fully implemented in this example but can be added as needed.

To use this code, create a new C# console application project and replace the contents of the Program.cs file with the provided code. Make sure to include the necessary using statements at the beginning of the file. You will also need to create an XML configuration file named "config.xml" in the same directory as your executable with the appropriate channel and device settings.

Here is an example of how the "config.xml" file might look like:

Copy Code

This example defines two channels (one serial port channel and one TCP client channel) and two devices that use these channels. You can modify this file to add more channels and devices as needed.

To enable debugging features, you can check for a command-line argument when the program starts and create sample configuration files if they don't exist. For example:

if (args.Length > 0 && args[0] == "- debug")
{
CreateSampleConfigurations();
}

private static void CreateSampleConfigurations()
{
string configFile = "config.xml";

if (!File.Exists(configFile))
{
XDocument doc = new XDocument(
new XElement("config",
new XElement("channel",
new XAttribute("guId", "00000000-0000-0000-0000-000000000000"),
new XAttribute("name", "Serial Port Channel"),
new XAttribute("type", "SerialPort"),
new XAttribute("connectionString", "COM1"),
new XAttribute("readTimeout", "1000"),
new XAttribute("writeTimeout", "1000"),
new XAttribute("interCharacterInterval", "100"),
new XAttribute("interPacketInterval", "100"),
new XAttribute("retries", "3")
),
new XElement("channel",
new XAttribute("guId", "11111111-1111-1111-1111-111111111111"),
new XAttribute("name", "TCP Client Channel"),
new XAttribute("type", "TcpClient"),
new XAttribute("connectionString", "127.0.0.1:502"),
new XAttribute("readTimeout", "1000"),
new XAttribute("writeTimeout", "1000"),
new XAttribute("interCharacterInterval", "100"),
new XAttribute("interPacketInterval", "100"),
new XAttribute("retries", "3")
),
new XElement("device",
new XAttribute("guId", "22222222-2222-2222-2222-222222222222"),
new XAttribute("name", "Device 1"),
new XAttribute("address", "1"),
new XAttribute("channelGuId", "00000000-0000-0000-0000-000000000000"),
new XAttribute("logFile", "device1.log"),
new XAttribute("errorLogFile", "device1_errors.log")
),
new XElement("device",
new XAttribute("guId", "33333333-3333-3333-3333-333333333333"),
new XAttribute("name", "Device 2"),
new XAttribute("address", "2"),
new XAttribute("channelGuId", "11111111-1111-1111-1111-111111111111"),
new XAttribute("logFile", "device2.log"),
new XAttribute("errorLogFile", "device2_errors.log")
)
)
);

doc.Save(configFile);
}
}
Copy Code

With this debugging feature enabled, you can run the program without any configuration files, and it will automatically create sample files for you. You can then modify these files to suit your needs. To enable debugging, simply pass the "- debug" command-line argument when running the program:

dotnet run - debug
Copy Code

This will create the "config.xml" file in the same directory as your executable and launch the program with the sample configurations. You can then modify the "config.xml" file to add more channels and devices as needed.

I hope this helps! Let me know if you have any questions or need further assistance.

Best regards, [Your Name]

P.S. Don't forget to replace [Your Name] with your actual name. 😊

ИИ, lm studio, mixtral 8x7b

Previous post Next post
Up