#include "FiveWin.ch"
#define DT_WORDBREAK 16
static lExit := .F.
function Main()
local oDlg, cWelcome, oOutput, cOutput := "", oBtn
local oPrompt, cPrompt := Space( 100 )
cWelcome = Read()
DEFINE DIALOG oDlg TITLE "Llama3-70B chat" SIZE 1200, 600
@ 5.5, 0.7 GET oOutput VAR cOutput MULTILINE SIZE 590, 200 READONLY
@ 18.7, 1 SAY "Prompt:"
@ 21.5, 4 GET oPrompt VAR cPrompt SIZE 510, 15
@ 15.5, 92 BUTTON oBtn PROMPT "Send" SIZE 40, 15 ACTION SendPrompt( cPrompt, oOutput, oBtn, oPrompt ) DEFAULT
ACTIVATE DIALOG oDlg CENTERED ;
ON PAINT ( SetBkColor( hDC, GetPixel( hDC, 5, 5 ) ),;
SelectObject( hDC, oDlg:oFont:hFont ),;
DrawTextEx( hDC, cWelcome, { -10, 15, 800, 1000 }, DT_WORDBREAK ) )
if ! lExit
Write( "/exit" + CRLF )
endif
EndChat()
return nil
function SendPrompt( cPrompt, oOutput, oBtn, oPrompt )
local cToken
oBtn:Disable()
Write( AllTrim( cPrompt ) + CRLF )
if cPrompt != "/exit"
while Right( ( cToken := Read() ), 8 ) != " [0m" + CRLF + "> "
if Left( cToken, 6 ) == " [36m<"
cToken = SubStr( cToken, 9 )
endif
oOutput:SetFocus()
oOutput:Append( cToken )
SysRefresh()
end
endif
oOutput:SetFocus()
if cPrompt != "/exit"
oOutput:Append( SubStr( cToken, 1, Len( cToken ) - 8 ) + CRLF + CRLF )
else
lExit = .T.
oOutput:oWnd:End()
endif
oBtn:Enable()
oPrompt:SetFocus()
return nil
#pragma BEGINDUMP
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <hbapi.h>
HANDLE hInputRead, hInputWrite, hOutputRead, hOutputWrite;
PROCESS_INFORMATION pi;
HB_FUNC( WRITE )
{
DWORD dwBytesWritten;
WriteFile( hInputWrite, hb_parc( 1 ), hb_parclen( 1 ), &dwBytesWritten, NULL );
}
HB_FUNC( READ )
{
char buffer[1024];
DWORD dwBytesRead;
ReadFile( hOutputRead, buffer, 1024, &dwBytesRead, NULL );
buffer[ dwBytesRead ] = '\0'; // Agregar fin de cadena
hb_retc( buffer );
}
HB_FUNC( BEGINCHAT )
{
STARTUPINFO si;
SECURITY_ATTRIBUTES sa;
char command[256];
char email[256];
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
strcpy( email, hb_parc( 1 ) );
if (!CreatePipe(&hInputRead, &hInputWrite, &sa, 0))
MessageBox( 0, "Error al crear pipe de entrada", "ok", 0 );
if (!CreatePipe(&hOutputRead, &hOutputWrite, &sa, 0))
MessageBox( 0, "Error al crear pipe de salida", "ok", 0 );
// Preparar la cadena de comando con los parámetros
sprintf(command, "hchat.bin -u %s", email);
// Crear proceso externo
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdInput = hInputRead;
si.hStdOutput = hOutputWrite;
si.hStdError = hOutputWrite;
if (!CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
MessageBox( 0, "Error al crear proceso externo", "ok", 0 );
}
HB_FUNC( ENDCHAT )
{
CloseHandle( hInputRead );
CloseHandle( hInputWrite );
CloseHandle( hOutputRead );
CloseHandle( hOutputWrite );
TerminateProcess( pi.hProcess, 0 );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
#pragma ENDDUMP