https://fivetechsupport.com/forums/viewtopic.php?f=6&t=42575&start=0
and after seeking advice from ChatGPT, I have managed to implement "context" for it.
For example, if you first tell it "tell me a joke" and then say "tell me another," it knows what you mean
The goal is to provide different "prompts," make it work automatically, be able to remove previous steps, etc.
It's a work in progress and still has bugs. Here it is if you'd like to give it a try:
autogpt.prg
- Code: Select all Expand view RUN
- #include "FiveWin.ch"
function Main()
local oDlg, oGet1, cVar1 := Space( 100 )
local oGet2, cVar2 := Space( 2048 )
local oFont, oBtn1, oSay1, oSay2
local nChars := 2048
local oOpenAI := TOPenAI()
local oGetContext, cContext
oOpenAI:cKey = "sk-yours..."
TBtnBmp():lLegacyLookLeftRight := .T.
DEFINE FONT oFont NAME "Verdana" SIZE 0, -16
DEFINE DIALOG oDlg FROM 0, 0 TO 39, 150 TRUEPIXEL TITLE "autoGPT with OpenAI"
@ 24, 370 SAY "Question" OF oDlg FONT oFont SIZE 75, 15 PIXEL
@ 28, 1040 SAY oSay1 VAR "Chars: 0 / " + cValToChar( nChars ) OF oDlg PIXEL ;
SIZE 120, 15 UPDATE CENTER
@ 45, 370 GET oGet1 VAR cVar1 OF oDlg PIXEL SIZE 800,200 MEMO FONT oFont
oGet1:bGotfocus := {|| oGet1:SetSel( 0, 0 ) }
@ 250, 590 BTNBMP PROMPT "Clean context" OF oDlg PIXEL SIZE 150, 45 ;
ACTION oOpenAI:Clean() NOBORDER 2007
@ 250, 755 BTNBMP PROMPT "Submit" OF oDlg PIXEL SIZE 150, 45 ;
ACTION ( oGet2:SetText( oOpenAI:Ask( AllTrim( cVar1 ) ) ), oGetContext:SetText( oOpenAI:cContext ) ) ;
NOBORDER 2007
@ 296, 370 SAY "Result" OF oDlg FONT oFont SIZE 70,15 PIXEL
@ 317, 370 GET oGet2 VAR cVar2 OF oDlg PIXEL SIZE 800, 200 MEMO FONT oFont
oGet2:bGotfocus := {|| oGet2:SetSel( 0, 0 ) }
@ 24, 15 SAY "Context" OF oDlg FONT oFont SIZE 75, 15 PIXEL
@ 45, 15 GET oGetContext VAR cContext PIXEL SIZE 340, 473 MEMO FONT oFont
@ 535, 900 BTNBMP PROMPT "Close" OF oDlg PIXEL ACTION oDlg:End() SIZE 150,45;
NOBORDER 2007
ACTIVATE DIALOG oDlg CENTERED ON PAINT oGet1:SetPos( 0 )
return nil
//------------------------------------------------------------//
CLASS TOpenAI
DATA cKey
DATA cContext INIT ""
DATA cAnswer
METHOD Ask( cQuestion )
METHOD Clean() INLINE ::cContext := ""
ENDCLASS
//------------------------------------------------------------//
METHOD Ask( cQuestion ) CLASS TOpenAI
::cContext += "User: " + cQuestion
::cAnswer = OpenAICall( ::cContext, ::cKey )
if ! Empty( ::cAnswer )
::cContext += ". chatGPT: " + StrTran( ::cAnswer, CRLF, "\r\n" )
endif
return ::cAnswer
//------------------------------------------------------------//
function OpenAICall( cPrompt, cKey )
local cUrl := "https://api.openai.com/v1/engines/text-davinci-003/completions"
local cJSon, hAnswer := {=>}, cAnswer := ""
static oSoap
if oSoap == nil
oSoap = CreateObject( "MSXML2.ServerXMLHTTP.6.0" )
endif
TEXT INTO cJson
{
"prompt": "cPrompt_empty",
"temperature": 0,
"max_tokens": 2048
}
ENDTEXT
cPrompt = AllTrim( cPrompt )
cJson = StrTran( cJson, "cPrompt_empty", cPrompt )
cJson = StrTran( cJson, CRLF, "\r\n" )
oSoap:Open( "POST", cUrl, .F. )
oSoap:SetRequestHeader( "Content-Type", "application/json; charset=utf-8" )
oSoap:setRequestHeader( "Authorization", "Bearer " + cKey )
oSoap:Send( cJson )
hb_jsondecode( Alltrim( oSoap:responseText ), @hAnswer )
if hb_hHasKey( hAnswer, "error" )
if Empty( hAnswer[ "error" ][ "param" ] )
hAnswer[ "error" ][ "param" ] = ""
endif
if Empty( hAnswer[ "error" ][ "code" ] )
hAnswer[ "error" ][ "code" ] = ""
endif
MsgAlert( hAnswer[ "error" ][ "message" ] + CRLF + CRLF + ;
"Param: " + hAnswer[ "error" ][ "param" ] + CRLF + CRLF + ;
"Code: " + hAnswer[ "error" ][ "code" ] + CRLF + CRLF, ;
hAnswer[ "error" ][ "type" ] )
else
cAnswer = hAnswer[ "choices" ][ 1 ][ "text" ]
cAnswer = AllTrim( StrTran( cAnswer, Chr( 10 ), CRLF ) )
cAnswer = StrTran( cAnswer, '"', "'" )
endif
return cAnswer
//------------------------------------------------------------//