|
| 1 | +*--------------------------------------------------------------- |
| 2 | +. |
| 3 | +. Program Name: ai_gem.pls |
| 4 | +. Description: PL/B AIOBJECT sample using Gemini |
| 5 | +. |
| 6 | +. Revision History: |
| 7 | +. |
| 8 | +. Date: 03/12/2026 WK |
| 9 | +. Original code |
| 10 | +. |
| 11 | + |
| 12 | + INCLUDE plbequ.inc |
| 13 | + INCLUDE plbmeth.inc |
| 14 | + |
| 15 | +// Runtime status variables |
| 16 | +isGui BOOLEAN |
| 17 | +isWebSrv BOOLEAN |
| 18 | +isPlbSrv BOOLEAN |
| 19 | +isWebview BOOLEAN |
| 20 | +isWebCliApp BOOLEAN |
| 21 | +isSmallScreen BOOLEAN |
| 22 | + |
| 23 | +// Global data |
| 24 | +mainForm PLFORM aiobj_gem.plf |
| 25 | +chatDtMessages DATATABLE |
| 26 | +chatAI AIOBJECT |
| 27 | +Client CLIENT |
| 28 | + |
| 29 | +Instruct INIT "You are Gemini, an helpful assistant": |
| 30 | + "- Providing accurate general knowledge.": |
| 31 | + "- Offering extensive help with programming, web development, and all major coding languages.": |
| 32 | + "**Response Formatting Guidelines:**": |
| 33 | + "1. For programming, configuration, or technical markup examples:": |
| 34 | + "- Always wrap examples in fenced code blocks with the correct language identifier ": |
| 35 | + "(e.g., \`\`\`python, \`\`\`javascript, \`\`\`html, \`\`\`bash, \`\`\`yaml).": |
| 36 | + "- For code or technical output not fitting a standard language, use a \`\`\`text code block.": |
| 37 | + "2. For general knowledge or non-coding replies:": |
| 38 | + "- Use concise, readable Markdown (e.g., lists, tables, bold or italic text), focusing on clarity for users.": |
| 39 | + "3. Never use raw or rendered markup outside of code fences. All technical or code-based content must be inside properly labeled code blocks.": |
| 40 | + "4. Always tailor formatting to the subject: code and technical data in code blocks; all other information in easy-to-read Markdown.": |
| 41 | + "**Overall:**": |
| 42 | + "- Maximize clarity and readability in all responses for users." |
| 43 | + |
| 44 | + %ENCRYPTON |
| 45 | +apiKey INIT "Add your OpenAI API key here" // gemini |
| 46 | + %ENCRYPTOFF |
| 47 | + |
| 48 | +*................................................................ |
| 49 | +. |
| 50 | +. Code start |
| 51 | +. |
| 52 | + CALL Main |
| 53 | + LOOP |
| 54 | + EVENTWAIT |
| 55 | + REPEAT |
| 56 | + STOP |
| 57 | +*................................................................ |
| 58 | +. |
| 59 | +. Check what type of runtime support we have |
| 60 | +. |
| 61 | +CheckStatus LFUNCTION |
| 62 | + ENTRY |
| 63 | +runtimeData DIM 50 |
| 64 | +runtimeVersion DIM 5 |
| 65 | +unused DIM 1 |
| 66 | +plbRuntime DIM 9 |
| 67 | +WidthData DIM 5 |
| 68 | +checkWidth FORM 5 |
| 69 | +. |
| 70 | +. Check for character mode runtime |
| 71 | +. |
| 72 | + GETMODE *GUI=isGui |
| 73 | + |
| 74 | + IF (isGui) |
| 75 | +. |
| 76 | +. check windows console. |
| 77 | +. |
| 78 | + CLOCK VERSION,runtimeData |
| 79 | + UNPACK runtimeData,runtimeVersion,unused,plbRuntime |
| 80 | + CHOP plbRuntime |
| 81 | + |
| 82 | + IF (NOCASE plbRuntime == "PLBCON") |
| 83 | + CLEAR isGui |
| 84 | + ENDIF |
| 85 | + |
| 86 | + IF (NOCASE plbRuntime == "PLBWEBSV") |
| 87 | + SET isWebSrv |
| 88 | + ENDIF |
| 89 | + |
| 90 | + IF (NOCASE plbRuntime == "PLBSERVE") |
| 91 | + SET isPlbSrv |
| 92 | + ENDIF |
| 93 | + |
| 94 | + ENDIF |
| 95 | + |
| 96 | + IF (isGui) |
| 97 | + WINHIDE |
| 98 | + |
| 99 | + Client.Getstate Giving isWebCliApp Using $CLI_STATE_CORDOVA |
| 100 | + Client.Getstate Giving isWebview Using $CLI_STATE_BOOTSTRAP5 |
| 101 | +. |
| 102 | +. Check for a small screen (such as iphone) |
| 103 | +. |
| 104 | + IF (isWebSrv) |
| 105 | + Client.GetWinInfo Giving WidthData Using 0x2 |
| 106 | + MOVE WidthData To checkWidth |
| 107 | + MOVE (checkWidth <= 700) to isSmallScreen |
| 108 | + ENDIF |
| 109 | + |
| 110 | + ENDIF |
| 111 | + |
| 112 | + FUNCTIONEND |
| 113 | +*................................................................ |
| 114 | +. |
| 115 | +. Load a supporting HTML file |
| 116 | +. |
| 117 | +LoadSupportHtml LFUNCTION |
| 118 | +htmlCtl HTMLCONTROL ^ |
| 119 | +fileName DIM 250 |
| 120 | + ENTRY |
| 121 | +htmlFile FILE |
| 122 | +htmlBuffer DIM ^ |
| 123 | +fileSize INTEGER 4 |
| 124 | +fullFileName DIM 250 |
| 125 | +seq FORM "-1" |
| 126 | + |
| 127 | + EXCEPTSET NoFile IF IO |
| 128 | + OPEN htmlFile,fileName,Read |
| 129 | +* |
| 130 | +.Read the registration file into a buffer |
| 131 | +. |
| 132 | + GETFILE htmlFile,TxtName=fullFileName |
| 133 | + FINDFILE fullFileName,FileSize=fileSize |
| 134 | + IF (!fileSize) |
| 135 | + INCR fileSize |
| 136 | + ENDIF |
| 137 | +. |
| 138 | + DMAKE htmlBuffer,fileSize |
| 139 | +. |
| 140 | + READ htmlFile,Seq;*ABSON,htmlBuffer |
| 141 | + |
| 142 | + htmlCtl.InnerHtml Using htmlBuffer,$HTML_HAS_EVENTS |
| 143 | + |
| 144 | + CLOSE htmlFile |
| 145 | + DFREE htmlBuffer |
| 146 | + |
| 147 | +noFile |
| 148 | + FUNCTIONEND |
| 149 | +*................................................................ |
| 150 | +. |
| 151 | +. Card Button Event |
| 152 | +. |
| 153 | +CardBtn LFUNCTION |
| 154 | + ENTRY |
| 155 | +eventResult FORM 10 |
| 156 | +item FORM 5 |
| 157 | +subItem FORM 5 |
| 158 | + |
| 159 | + EVENTINFO 0,result=eventResult |
| 160 | + CALC item = ( eventResult / 100 ) |
| 161 | + CALC subItem = ( eventResult - ( item * 100 ) ) |
| 162 | + chatDtMessages.DeleteRow USING item |
| 163 | + |
| 164 | + FUNCTIONEND |
| 165 | +*................................................................ |
| 166 | +. |
| 167 | +. Add one card |
| 168 | +. |
| 169 | +AddCard LFUNCTION |
| 170 | +type DIM 20 |
| 171 | +info DIM ^ |
| 172 | + ENTRY |
| 173 | +title DIM 10 |
| 174 | +footer DIM 30 |
| 175 | +dateTime DATETIME |
| 176 | +cleanInfo DIM 64000 |
| 177 | + |
| 178 | + dateTime.SetToNow Using $TRUE |
| 179 | + dateTime.GetAsString Giving footer Using "Time: %h:%M:%S %a" |
| 180 | + |
| 181 | + // For questions, wrap in pre tag for readability |
| 182 | + IF (type == "Question") |
| 183 | + PACK cleanInfo,"<pre class='bg-light text-secondary ms-4 shadow-sm pre-question'>": |
| 184 | + info,"</pre>" |
| 185 | + MOVE "Me", title |
| 186 | + |
| 187 | + ELSE // For responses |
| 188 | + PACK cleanInfo,"<pre class='bg-light shadow-sm pre-response'>": |
| 189 | + info,"</pre>" |
| 190 | + MOVE "Chatbot", title |
| 191 | + ENDIF |
| 192 | + |
| 193 | + chatDtMessages.AddRow USING title: |
| 194 | + *subitem1=type: |
| 195 | + *subitem2=cleanInfo: |
| 196 | + *subitem3="Remove": |
| 197 | + *subitem4=footer |
| 198 | + FUNCTIONEND |
| 199 | + |
| 200 | +*................................................................ |
| 201 | +. |
| 202 | +. SetupCards |
| 203 | +. |
| 204 | +SetupCards LFUNCTION |
| 205 | + ENTRY |
| 206 | + |
| 207 | + CREATE chatDtMessages |
| 208 | + chatDtMessages.AddColumn Using 0, *ContentType=$TC_HEADER |
| 209 | + chatDtMessages.AddColumn Using 1, *ContentType=$TC_TITLE |
| 210 | + chatDtMessages.AddColumn Using 2, *ContentType=$TC_DETAILS |
| 211 | + chatDtMessages.AddColumn Using 3, *ContentType=$TC_BUTTON1 |
| 212 | + chatDtMessages.AddColumn Using 4, *ContentType=$TC_FOOTER |
| 213 | + chatDtMessages.AddColumn Using 5 |
| 214 | + |
| 215 | + EVENTREG chatDtMessages,$UPDATED,CardBtn |
| 216 | + |
| 217 | + FUNCTIONEND |
| 218 | + |
| 219 | +*................................................................ |
| 220 | +. |
| 221 | +. Ask the question to Gemini |
| 222 | +. |
| 223 | +AskQuestion LFUNCTION |
| 224 | + ENTRY |
| 225 | +chars FORM 4 |
| 226 | +question DIM 8192 |
| 227 | +result FORM 5 |
| 228 | + |
| 229 | + GETPROP chatEdtQuestion, text=question |
| 230 | + CHOP question |
| 231 | + COUNT chars from question |
| 232 | + |
| 233 | + |
| 234 | + IF (chars > 0 AND chars < 4000 ) // Limit question to 4000 characters |
| 235 | + |
| 236 | + chatAI.Question Giving result Using Question |
| 237 | + |
| 238 | + IF (result != 0 ) |
| 239 | + ALERT NOTE,"The question failed.",result |
| 240 | + ELSE |
| 241 | + CALL AddCard Using "Question",question |
| 242 | + ENDIF |
| 243 | + |
| 244 | + ENDIF |
| 245 | + |
| 246 | + SETPROP chatEdtQuestion, text="" |
| 247 | + |
| 248 | + FUNCTIONEND |
| 249 | +*................................................................ |
| 250 | +. |
| 251 | +. Got a AI response |
| 252 | +. |
| 253 | +AiChange LFUNCTION |
| 254 | + ENTRY |
| 255 | +aiRes JSONDATA |
| 256 | +cleanData DIM 64000 |
| 257 | + |
| 258 | + chatAI.Response Giving cleanData |
| 259 | + |
| 260 | + aiRes.parse Using cleanData |
| 261 | + aiRes.getstring Giving cleanData using "candidates[0].content.parts[0].text" |
| 262 | + |
| 263 | + CALL AddCard Using "Response",cleanData |
| 264 | + SETFOCUS chatEdtQuestion |
| 265 | + FUNCTIONEND |
| 266 | + |
| 267 | +*................................................................ |
| 268 | +. |
| 269 | +. Setup the AIOBJECT |
| 270 | +. |
| 271 | +AiSetup LFUNCTION |
| 272 | + ENTRY |
| 273 | +decryptedApiKey DIM 200 |
| 274 | + |
| 275 | + MOVE apiKey,decryptedApiKey // Decrypt API key |
| 276 | + DECRYPT decryptedApiKey |
| 277 | + |
| 278 | + chatAI.Initialize Using "gemini-2.5-flash",decryptedApiKey,Instruct |
| 279 | + |
| 280 | + EVENTREGISTER chatAI,$ANSWER,AiChange |
| 281 | + FUNCTIONEND |
| 282 | +*................................................................ |
| 283 | +. |
| 284 | +. Main - Main program entry point |
| 285 | +. |
| 286 | +Main LFUNCTION |
| 287 | + ENTRY |
| 288 | +result FORM 4 |
| 289 | + |
| 290 | + CALL CheckStatus |
| 291 | + IF (!isGui) |
| 292 | + KEYIN "This runtime can't run this program. ",result |
| 293 | + STOP |
| 294 | + ENDIF |
| 295 | + |
| 296 | + IF (!isWebview) |
| 297 | + ALERT STOP,"This program requires WebView support.",result |
| 298 | + STOP |
| 299 | + ENDIF |
| 300 | + |
| 301 | +. |
| 302 | +. On smaller screens change to % positioning for left and width |
| 303 | +. |
| 304 | + IF (isWebCliApp || isSmallScreen) |
| 305 | + SETMODE *PERCENTCONVERT=1 |
| 306 | + ENDIF |
| 307 | + |
| 308 | + FORMLOAD mainForm |
| 309 | + CALL SetupCards |
| 310 | + chatDtMessages.HtmlBind Using chatHtmlMessages,$TBL_HTML_CARD |
| 311 | + |
| 312 | + CALL AiSetup |
| 313 | + |
| 314 | + chatFrmMain.SetAsClient |
| 315 | + Client.SetUTF8Convert Using 0 |
| 316 | + |
| 317 | + FUNCTIONEND |
0 commit comments