Manuale per l'uso della libreria OpenAI Library MT5 by StormWave

Manuale per l'uso della libreria OpenAI Library MT5 by StormWave

9 febbraio 2024, 23:38
VitalDefender Inc.
0
49

MANUALE TECNICO

Questo blog vuole essere una breve guida tecnica per poter iniziare ad usare le API di OpenAI e sfruttare le grandissime potenzialità che offre l'uso dell'intelligenza artificiale, in particolare i Large Language Models, applicata al trading.

Link per acquistare la libreria : 

https://www.mql5.com/it/market/product/112766?source=Unknown

Per prima cosa è necessario procedere ad includere il file allegato StormWaveOpenAI.mqh, che contiene le classi e l'header della libreria, in modo che voi non dobbiate preoccuparvi di nietn'altro.

Una volta inclusa la libreria, possiamo iniziare con un esempio veramente semplice, ossia quello che è il codice sorgente commentato per l'Expert Advisor "OpenAI API", che potete scaricare gratuitamente e testare con le mie API di OpenAI al seguente link: 
https://www.mql5.com/it/market/product/112756?source=Site+Market+Product+Page#description

Procediamo!

#include <StormWaveOpenAI.mqh>      //--- Include the custom OpenAI header file for API integration

input string OPENAI_API_KEY_ = "";  // YOUR OPENAI APIKEY
input string MESSAGE_ = "";         // SEND MESSAGE
input int MAX_TOKEN_ = 300;         // MAX TOKEN

COpenAI *client;                    //--- Declare a pointer to the OpenAI client
CMessages *_message_;               //--- Declare a pointer for handling messages

string __api_key__ = OPENAI_API_KEY_;

//+------------------------------------------------------------------+
//| OnInit()                                                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(MQLInfoInteger(MQL_TESTER))
      return(INIT_FAILED);

   client = iOpenAI(__api_key__); //--- Initialize the OpenAI client with your API key
   
//--- I decide to do message parsing directly from the cache so as to fetch data to continue chats already started
   client.ParseCache();
   client.start_thread();            //--- Start a new thread for the OpenAI client to operate in
   string completion;

//+------------------------------------------------------------------+
//|                           MESSAGE 1                              |
//+------------------------------------------------------------------+
//--- BOT PREPARATION ---
//--- I create a message
   _message_ = iMessages();          //--- Initialize the message handler
   string system_content = "You are a technical and professional financial assistant specializing in forex chart analysis and respond in a maximum of 40 words in the language of the last message sent by the user";//--- Define the message content
   _message_.AddMessage(system_content, system); //--- Add the message to the handler with a system identifier

   string warning = "Warning. You are using API KEY, so you are limited to entering a shorter message!\n" +
                    "If you want to enter a longer message you can use your API KEY." +
                    "You can get them by going to the following URL : https://platform.openai.com/api-keys";

   string user_content = MESSAGE_;
//--- Checking whether it is within the maximum number of tokens
   int str_tokens = client.CalculateTokens(MESSAGE_);
   if(str_tokens > MAX_TOKEN_)
     {
      ::MessageBox(warning, "Error", MB_OK | MB_ICONWARNING);
      return(INIT_FAILED);
     }
   user_content = MESSAGE_;

   _message_.AddMessage(user_content, user);           //--- Add the message to the handler with a user identifier
   int token = MAX_TOKEN_;

//--- Call the API to generate a completion based on the provided messages
   completion = client.completions_create(
                   /*model =        */  "gpt-3.5-turbo-0125", //--- Specify the model to use for the completion
                   /*messages =     */   _message_,           //--- Pass the messages to the API
                   /*max_tokens =   */   300,                 //--- Set the maximum number of tokens to generate
                   /*temperature =  */   1.0                  //--- Set the creativity level of the response
                );

   ::MessageBox(client.PrintResultMessage(), "Result", MB_OK | MB_ICONINFORMATION); //--- Print the result of the API call
   DeletePointer(_message_);
   DeletePointer(client);
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| OnTick()                                                         |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(MQLInfoInteger(MQL_TESTER))
      return;
   ExpertRemove();
  }

template<typename T>
void DeletePointer(T* &ptr)
  {
   if(::CheckPointer(ptr) == POINTER_DYNAMIC && ptr != NULL)
     {
      delete ptr;
      ptr = NULL;
     }
  }

Bene in questo esempio abbiamo creato un semplice EA che risponde alle domande effettuate attraverso la finestra di input.

Prossimamente seguiranno altri esempi con l'uso dei tools (ossia delle funzioni) e con l'uso della Computer Vision di GPT-Vision.

Grazie per aver letto questo articolo!



File:
JAson.mqh  61 kb
Condividi con gli amici: