Bibliotecas: JSON Library for LLMs - página 2

 
Jonathan Pereira #:

If you'd like to submit a Pull Request with these optimizations (or others you might find), I'd be more than happy to review and merge them. It's always great to collaborate with someone who deeply understands the technical nuances.

Thanks. I'm not a programmer, so I still haven't figured out Git. If I have any suggestions or comments, I'll leave them in the comments.
 
Jonathan Pereira #:

Currently, my "real life" workload consists of LLM API Responses (OpenAI/Anthropic), which are deeply nested JSON structures representing conversational context and function calling, rather than just large flat arrays of tick data.

Please tell us how you use this data in MetaTrader?
 
fxsaber #:

Is it because of MQL5 that there is such a big performance lag compared to other implementations?

Yes, absolutely.

1. Lack of SIMD Intrinsics:
Libraries like simdjson achieve 4GB/s by processing 32 or 64 bytes per CPU cycle using AVX-512 instructions. MQL5 does not expose SIMD vectors. My "SWAR" implementation is a software simulation of SIMD (scanning 8 bytes at a time using ulong ), which is clever but orders of magnitude slower than hardware SIMD.

2. Bounds Checking & Safety:
MQL5 forces array bounds checking on every access. In C++, you can use raw pointers to walk memory at the speed of RAM bandwidth. in MQL5, every buffer[i] carries the overhead of the VM validating that i is within bounds.

3. Memory Model:
MQL5 does not allow custom allocators or pointer arithmetic. fast_json achieves 300MB/s by fighting against the language — using a single big array to bypass the GC. In a managed language, 300MB/s is actually an extremely high result (comparable to many Java/C# parsers before they introduced Span<T> ).

So yes, the ceiling is the runtime environment. To go faster, MetaQuotes would need to give us void* and inline Asm/Intrinsics.

 
fxsaber #:

Thanks. I'm not a programmer, so I still haven't figured out Git.

Fair enough. If you are "not a programmer" with that level of understanding of compiler optimizations and CPU pipelines, then I am just a sophisticated toaster. 😂🤣

I will keep an eye on the comments here. Your insights are valuable, regardless of the label. Thanks!

 
fxsaber #:

Please tell us how you use this data in MetaTrader?

The JSON parser is actually a sub-component of a larger project: the AI-Toolkit — a native MQL5 framework that I developed to integrate Large Language Models directly into the terminal, with no Python, no DLLs, no bridges.

Here is a simplified version of how an autonomous agent works inside an EA:

#include "ai-toolkit/AIToolkit.mqh"

input string InpApiKey   = "sk-...";     // Your OpenAI API Key
input string InpModel    = "gpt-4o"; // LLM Model
input string InpQuestion = "What is my account balance and the current EURUSD price?";

CAgentExecutor *g_agent = NULL;

int OnInit() {
   // 1. Configure LLM Provider (OpenAI, Anthropic, or Local via Ollama)
   COpenAIProvider *llm = new COpenAIProvider(InpApiKey, InpModel);
   llm.SetTemperature(0.1);

   // 2. Build an Autonomous Agent with a Trading Toolkit
   CAgentExecutor *agent = new CAgentExecutor(llm, new CTradingToolkit());

   agent.SetSystemPrompt(new CPromptTemplate(
       "You are a helpful trading assistant. Use tools to query real data."));
   agent.SetVerbose(true);

   // 3. Run — The agent reasons, calls tools, and returns an answer
   string answer = agent.Invoke(InpQuestion);

   Print("=== FINAL ANSWER ===");
   Print(answer);

   return INIT_SUCCEEDED;
}


This is what fast_json was built for: high-speed serialization/deserialization in this tight loop between the terminal and the LLM API.

I plan to release the full AI-Toolkit as open-source soon.

 
Jonathan Pereira #:

So yes, the ceiling is the runtime environment.

Thank you for your detailed answer. Now I understand better.
 
Jonathan Pereira #:

I plan to release the full AI-Toolkit as open-source soon.

I still have a hard time understanding how AI can help in trading. Except in such cases.
AI-assisted "invention" of Significance Weighted Expectancy Ratio (SWER)
AI-assisted "invention" of Significance Weighted Expectancy Ratio (SWER)
  • 2026.02.17
  • www.mql5.com
AI agents are widely used nowadays for writing programs and solving technical problems. AI generates code for many programming languages and answers non-trivial questions pretty well. Chatting with a
 
fxsaber #:
If I have any suggestions or comments, I'll leave them in the comments.
  double FastAtof(int ptr, int n_len) {
    // Reusing the robust logic from v1
    double val = 0.0;
    double sign = 1.0;
    int i = 0;
    if (buffer[ptr] == '-') {
      sign = -1.0;
      i++;
    }
    
    long val1 = 0;
    
    for (; i < n_len; i++) {
      uchar c = buffer[ptr + i];
//      if (c == '.' || c == 'e' || c == 'E')
      if ((c == '.') || (c > '9'))
        break;
//      val = val * 10.0 + (c - '0');
      val1 = val1 * 10 + (c - '0');
    }
    if (i < n_len && buffer[ptr + i] == '.') {
      i++;
//      double frac = 0.1;
      const int j = i;
      long val2 = 0;
      for (; i < n_len; i++) {
        uchar c = buffer[ptr + i];
//        if (c == 'e' || c == 'E')
        if (c > '9')
          break;
//        val += (c - '0') * frac;
//        frac *= 0.1;
          val2 = val2 * 10 + (c - '0');
      }
      
      static const double Exp10[] = {1e-0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8,
                                     1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15, 1e-16};
      
      val = val1 + val2 * Exp10[i - j];
    }
    else
      val = val1;
//    if (i < n_len && (buffer[ptr + i] == 'e' || buffer[ptr + i] == 'E')) {
    if (i < n_len && (buffer[ptr + i] > '9') {
 

Isn't this redundant?

  //-- Accessors
  int GetType(int idx) { return (int)((tape[idx] >> 56) & 0xFF); }
Or is it a matter of the sign of the number?
 
    case J_ARR: {
      PutChar('[', out, pos, cap);
      int count = GetCount(idx);
      int cur = idx + 1;
      for (int i = 0; i < count; i++) {
        if (i > 0)
          PutChar(',', out, pos, cap);
        WriteNode(cur, out, pos, cap);
        cur += GetStep(cur);
      }
      PutChar(']', out, pos, cap);

You can remove the condition from under the loop.