English
Source / code snippets

ChatGPT API in XProfan use (by cURL)

 

Sven
Bader
The mighty thing hieran is really The usage of cURL, there itself so complex Web-inquire by POST too JSON kodiert versenden let. cURL is since 2018 part Windows 10 and in windows 11 available, with windows 7/8 becomes not without further weg.

The response becomes then from the Konsole gotten.

your needed a own API-Key. one must Although a couple dollars deposit but I have to round 100 (smaller) inquire straight once a cent uses. (  [...]  )

The code functions with X4 but too with XProfan 10. plenty Fun at testing
 $H windows.ph
Def GetSysColor(1)      !"USER32", "GetSysColor"'Systemfarben inquire

Proc Utf8Decode

    'in neueren XProfan versions becomes automatically The gleichnamige interne function uses
    Parameters utf8$
    Declare ansistring$, unicode$, laenge&
    laenge& = len(utf8$) * 2
    unicode$ = mkstr$(chr $(0),laenge&)
    ~MultiByteToWideChar(~CP_UTF8,0,Addr(utf8$),-1,Addr(unicode$),laenge&)
    ansistring$ = mkstr$(chr $(0),laenge&)
    ~WideCharToMultiByte(~CP_ACP,0,Addr(unicode$),-1,Addr(ansistring$),laenge&,0,0)
    ansistring$ = substr$(ansistring$,1,chr $(0))
    Return ansistring$

ENDPROC

Proc CMDOut

    'original of Andreas Miethe
    Parameters Progname$,Command$,MemBuffer&
    Struct PROCESS_INFORMATION = hProcess&,hThread&,dwProcessId&,dwThreadId&
    Struct SECURITY_ATTRIBUTES =  nLength&,lpSecurityDescriptor&,bInheritHandle&
    Struct STARTUPINFO =cb&,lpReserved&,lpDesktop&,lpTitle&,dwX&,dwY&,dwXSize&,dwYSize&,dwXCountChars&,dwYCountChars&,dwFillAttribute&,dwFlags&,wShowWindow%,cbReserved2%,lpReserved2&,hStdInput&,hStdOutput&,hStdError&
    Def &NORMAL_PRIORITY_CLASS  $20
    Def &STARTF_USESTDHANDLES  $100
    Def &STARTF_USESHOWWINDOW  $1
    Declare hRPipe&,hWPipe&,pRBytes&,Result&
    Declare PI#,SI#,SA#, out$
    Dim PI#,PROCESS_INFORMATION
    Dim SI#,STARTUPINFO
    Dim SA#,SECURITY_ATTRIBUTES
    Declare StringBuffer$, output$
    StringBuffer$ = Space$(MemBuffer&)
    SA#.nLength& =SizeOf(SA#)
    SA#.bInheritHandle& = 1
    SA#.lpSecurityDescriptor& = 0
    Result& = ~CreatePipe(Addr(hRPipe&), Addr(hWPipe&), SA#, 0)

    If Result& = 0

        Return ""'MessageBox("Pipe couldn't launched werden","",0)

    EndIf

    If Progname$ <> ""

        Command$ = " " + Command$

    EndIf

    SI#.cb& = SizeOf(SI#)
    SI#.dwFlags& = &STARTF_USESHOWWINDOW | &STARTF_USESTDHANDLES
    SI#.hStdOutput& = hWPipe&
    SI#.hStdError& = hWPipe&

    If Progname$ = ""

        Result& = ~CreateProcess(0, Addr(Command$), SA#, SA#, 1, &NORMAL_PRIORITY_CLASS, 0, 0, SI#,PI#)

    Else

        Result& = ~CreateProcess(Addr(Progname$), Addr(Command$),SA#,SA#,1,&NORMAL_PRIORITY_CLASS, 0, 0,SI#,PI#)

    EndIf

    If Result& <> 1

        Return ""'MessageBox("Datei not gefunden","",0)

    EndIf

    Result& = ~CloseHandle(hWPipe&)

    While Result& <> 0

        Result& = ~ReadFile(hRPipe&,Addr(StringBuffer$),MemBuffer&,Addr(pRBytes&), 0)

        If pRBytes& > 0

            out$ = Left$(StringBuffer$,pRBytes&)
            output$ = output$ + out$

        EndIf

    EndWhile

    ~CloseHandle(PI#.hProcess&)
    ~CloseHandle(PI#.hThread&)
    ~CloseHandle(hRPipe&)
    Dispose PI#
    Dispose SI#
    Dispose SA#
    output$ = Utf8Decode(output$)
    Return output$

ENDPROC

Window Style 1+2+4+8+16
Window Title "ChatGPT"
Window 100,100 - 650,600;0
Cls GetSysColor(15)'Fensterfarbe
User Messages 16
UseFont "Tahoma",15,0,0,0,0'Standardfont
SetDialogFont 1
Declare edit&,font&[10],button&,text&
Font&[1] = Create("Font","Tahoma",15,0,0,0,0)'normal
text& = Create("Text",%hWnd,"", 20, 20, 600, 320)
Edit& = Create("MultiEdit", %hWnd, "", 20, 400, 600, 96)
button& = Create("Button", %hWnd, "Senden", 20, 500, 600, 24)
SetFont Edit&,font&[1]

Proc GPT

    Parameters speedy$
    Declare api_key$, endpoint$, payload$, response$,responseJson$, content$,model$,curl_exe$
    Declare curl_command$
    curl_exe$ =  trim$(CMDOut("", "where curl", 8192))
    api_key$  = "sk-proj-..........................................................................................."
    endpoint$ = "https://api.openai.com/v1/chat/completions"
    content$  = ""'Vorabbriefing, z.B. "Du are Yoda from Star Wars."
    model$    = "gpt-4o-mini"
    payload$ = "{\\\qmodel\\\q:\\\q" + model$ + "\\\q,\\\qmessages\\\q:[{\\\qrole\\\q:\\\qsystem\\\q,\\\qcontent\\\q:\\\q" + content$ + "\\\q},{\\\qrole\\\q:\\\quser\\\q,\\\qcontent\\\q:\\\q" + speedy$ + "\\\q}]}"
    curl_command$ = curl_exe$ + " -s -k -X POST " + endpoint$ + " -H \qAuthorization: Bearer " + api_key$ + "\q -H \qContent-Type: application/json\q -d \q" + payload$ + "\q"
    responseJson$ =  CMDOut("", curl_command$, 8192)
    'one could too The JSON functions of XProfan X4 use, but not without Umwege over a File
    response$ =  translate $(responseJson$,"\\\q", "\\q")' \" in XProfan-Syntax transfiguring (yet escaped)
    response$ =  Substr$(response$,2, "\qcontent\q: \q")' response extract
    response$ =  Substr$(response$,1, "\q")' response extract
    response$ =  translate $(response$,"\\q", "\q")' quotation marks final code
    response$ =  translate $(response$,"\\n","\n" )' Zeilenumbrüche

    If (response$ = "")

        SetText text&,  responseJson$

    Else

        SetText text&,  response$

    EndIf

ENDPROC

WhileNot (%umessage = 16)

    WaitInput

    If Clicked(button&)

        GPT(gettext$(edit&))

    EndIf

EndWhile

 
vor 11 Tagen  
 



Zum Quelltext


Topictitle, max. 100 characters.
 

Systemprofile:

no Systemprofil laid out. [anlegen]

XProfan:

 Posting  Font  Smilies  ▼ 

Please register circa a Posting To verfassen.
 

Topic-Options

111 Views

Untitledvor 0 min.
HofK vor 4 Tagen
Walter vor 4 Tagen
R.Schneider vor 7 Tagen
Peter Max Müller vor 8 Tagen
More...

Themeninformationen

this Topic has 1 subscriber:

Sven Bader (1x)


Admins  |  AGB  |  Applications  |  Authors  |  Chat  |  Privacy Policy  |  Download  |  Entrance  |  Help  |  Merchantportal  |  Imprint  |  Mart  |  Interfaces  |  SDK  |  Services  |  Games  |  Search  |  Support

One proposition all XProfan, The there's!


My XProfan
Private Messages
Own Storage Forum
Topics-Remember-List
Own Posts
Own Topics
Clipboard
Log off
 Deutsch English Français Español Italia
Translations

Privacy Policy


we use Cookies only as Session-Cookies because of the technical necessity and with us there no Cookies of Drittanbietern.

If you here on our Website click or navigate, stimmst You ours registration of Information in our Cookies on XProfan.Net To.

further Information To our Cookies and moreover, How You The control above keep, find You in ours nachfolgenden Datenschutzerklärung.


all rightDatenschutzerklärung
i want none Cookie