Recent

Author Topic: Programming Pascal with an AI Chatbot 2.0  (Read 22328 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3954
    • StrumPract is the musicians best friend
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #45 on: January 05, 2023, 06:37:32 am »
Q: is object pascal (fpc) case sensitive?
A: Yes, Object Pascal (also known as Free Pascal or FPC) is a case-sensitive programming language. This means that the compiler treats upper case and lower case letters as distinct characters, and will consider them to be different if they are used in different cases.

For example, in Object Pascal, the variables MyVariable and myvariable would be considered to be two different variables, and the compiler would not recognize them as being the same. Similarly, the function DoSomething would be considered to be different from the function dosomething.

It is important to be consistent with the use of case in Object Pascal, as using the wrong case can cause errors in your code. It is a good idea to adopt a standard naming convention for your variables and functions, and to stick to it consistently throughout your code.

cheers
S.
:)

Hello.

In their site https://chat.openai.com/chat there is:

Quote
Allows user to provide follow-up corrections

Maybe you should say after such a answer that he is wrong and AI will update his answer...
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #46 on: January 05, 2023, 06:53:23 am »
PS: I think it is the first time I try to access internet via fpc so I am totally lost, maybe there is something else to config  that I missed...  :-[.
No, you have not missed anything.

Run it from Lazarus or commandline (do not remove debug) and the exceptions becomes more clear. The code is a bit flawed (that is what you get when you expect everything to work, combine methods and not check for errors  :) )

Code: [Select]
project aiui raised exception class 'RunError(210)'with message:

Object not initialized
  In File 'aiui_unit.pas' at line 61:
  if result='' then result:=reponse.findpath('vhoices[0].text').AsString;

probably meaning that it can not find the path as the answer (JSON result) is different than expected.

EDIT:
change from line 61 into:
Code: Pascal  [Select][+][-]
  1.         if result='' then
  2.         begin
  3.           if response.findpath('choices[0].text') <> nil
  4.           then result:= response.findpath('choices[0].text').AsString
  5.           else result := response.AsJSON;
  6.         end;
  7.  

And in my case (wrong api key) it displays:
Code: [Select]
{ "error" : { "message" : "Incorrect API key provided: 123456. You can find your API key at https://beta.openai.com.", "type" : "invalid_request_error", "param" : null, "code" : "invalid_api_key" } }

But above solution is a quick hack. What needs to be done is parse the json result properly and check what json response was returned (and then act on it).
« Last Edit: January 05, 2023, 07:08:24 am by TRon »
Today is tomorrow's yesterday.

otoien

  • Jr. Member
  • **
  • Posts: 89
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #47 on: January 05, 2023, 08:04:38 am »
I tried it out for a) generating code for a well known algorithm for R-wave detection in an ECG signal (Pan-Tomkins) giving it a literature reference, and b) also to translate some existing open-source C-code for the same to Pascal. What I find in both cases is that it sort of has an attention deficit disorder, and stops the tasks in middle of the code generation.

It only outputs a limited number of lines. If you wanted to see more type out:

Q: Continue.

If it starts from the top, which it can do occasionally, then type:

Q: Continue from line 80.

Or whatever the last line it wrote. If you don't know the line, then copy its code into a text editor and use it to find the line numbers.

I hope this helps! Let me know if you have any further questions.

Thanks, yes I discovered just typing continue worked. Early on I started out typing a bit more elaborate request, stating that the code was not complete and ask it to complete the translation, but then it just repeated the whole code and stalled at mostly the same location. Just a single continue command works best (did not try line numbers, but it continued from slightly above where it stopped). However there is also a limitation to how many lines one can input to the chat, so one has to give it the original code piecemeal if it is long.
Unless otherwise noted I always use the latest stable version of Lasarus/FPC x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2921
    • http://www.cdbc.dk
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #48 on: January 05, 2023, 08:18:53 am »
Hi
@Fred vS:
If you log in to your openai.com account, then at the top right there is a link called "Personal". Click on that, in the menu, click on "View Api keys". On the next page, there's a button in the midddle of the page called "Generate New API key"... btw. the api-key is 53 chars long.
Hth, regards Benny
« Last Edit: January 05, 2023, 08:20:42 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #49 on: January 05, 2023, 09:00:47 am »
Looks like ChatGPT may be ammunition in a war between MS and Google.

Thaddy

  • Hero Member
  • *****
  • Posts: 19606
  • Glad to be alive.
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #50 on: January 05, 2023, 11:21:45 am »
@Fred VS

knocked up a basic GUI version, maybe it might pin point your problem

based on Thaddy routine..
I am writing a GUI too, with the ability to change models, e.g. text-xxx-xxx to write poems, lyrics and pascal programs/methods and comp-xxx-xxx to translate between programming languages, convert C macro's to Pascal functions and explain programming code. It can also set variables like temperature - affects correctness - and tokens -affects length and complexity of output and therefor price after out of beta. A comp model also answers the question on how to convert programming languages. See the OpenAI API documentation.
« Last Edit: January 05, 2023, 11:26:04 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #51 on: January 05, 2023, 11:30:20 am »
Here's something curious:

Quote
Q: Write Duff's device in C

A: Duff's device is a code optimization technique that was invented by Tom Duff in the 1980s. It is a way of unrolling a loop so that it can be implemented more efficiently in some cases. Here is an example of Duff's device implemented in C:

Code: C  [Select][+][-]
  1. void duff_copy(char *to, char *from, int count) {
  2.   int n = (count + 7) / 8;
  3.   switch (count % 8) {
  4.     case 0: do { *to++ = *from++;
  5.     case 7:      *to++ = *from++;
  6.     case 6:      *to++ = *from++;
  7.     case 5:      *to++ = *from++;
  8.     case 4:      *to++ = *from++;
  9.     case 3:      *to++ = *from++;
  10.     case 2:      *to++ = *from++;
  11.     case 1:      *to++ = *from++;
  12.     } while (--n > 0);
  13.   }
  14. }
  15.  

This code uses a switch statement to unroll the loop and perform the copy operation in blocks of 8. The n variable is used to control the number of iterations, and the count % 8 expression is used to determine how many extra copies need to be performed at the end.

To use this function, you would pass it the destination buffer to, the source buffer from, and the number of bytes to copy count. The function will then copy the data from from to to using Duff's device to optimize the copy operation.

Then the C code put in gist.

And then the next question:

Quote
Q: translate https://gist.githubusercontent.com/alpinistbg/f69313cdd6a7f4a735213e4a4b215c8f/raw/b73a3ea23c710e0c5474a8f7c125129a734302d9/duff_copy.c to pascal

A: Here is a translation of the C code you provided into Pascal:

Code: Pascal  [Select][+][-]
  1. procedure DuffCopy(var To_: PChar; const From_: PChar; Count: Integer);
  2. var
  3.   N: Integer;
  4. begin
  5.   N := (Count + 7) div 8;
  6.   case Count mod 8 of
  7.     0:
  8.       repeat
  9.         To_^ := From_^; Inc(To_); Inc(From_);
  10.     7:  To_^ := From_^; Inc(To_); Inc(From_);
  11.     6:  To_^ := From_^; Inc(To_); Inc(From_);
  12.     5:  To_^ := From_^; Inc(To_); Inc(From_);
  13.     4:  To_^ := From_^; Inc(To_); Inc(From_);
  14.     3:  To_^ := From_^; Inc(To_); Inc(From_);
  15.     2:  To_^ := From_^; Inc(To_); Inc(From_);
  16.     1:  To_^ := From_^; Inc(To_); Inc(From_);
  17.       until N <= 0;
  18.   end;
  19. end;
  20.  
This code uses a repeat-until loop and a case statement to unroll the loop and perform the copy operation in blocks of 8. The N variable is used to control the number of iterations, and the Count mod 8 expression is used to determine how many extra copies need to be performed at the end.

To use this function, you would pass it the destination buffer To_, the source buffer From_, and the number of bytes to copy Count. The function will then copy the data from From_ to To_ using Duff's device to optimize the copy operation.

Pascal code won't compile for obvious reasons.

Perhaps we should use the AI to port various libraries to Pascal so we have native implementation of things such like MPEG-4 decoder, FLAC decoder, 7ZIP packer, LEPTON compressor, UNRAR etc.?
Still wondering?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

domasz

  • Hero Member
  • *****
  • Posts: 630
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #52 on: January 05, 2023, 11:33:46 am »

wp

  • Hero Member
  • *****
  • Posts: 13644
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #53 on: January 05, 2023, 11:37:55 am »
My own experiments with ChatGPT left me with an ambivalent impression: Some results are extremely astonishing and surprising, others are absolute nonsense. Unfortunately, the probability of getting a nonsense answer is quite high. Therefore, this tool has no practical use for me since I always have to assume that there was a nonsense answer.

alpine

  • Hero Member
  • *****
  • Posts: 1412
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #54 on: January 05, 2023, 11:43:41 am »

Q: translate https://gist.githubusercontent.com/alpinistbg/f69313cdd6a7f4a735213e4a4b215c8f/raw/b73a3ea23c710e0c5474a8f7c125129a734302d9/duff_copy.c to pascal


I read it has no access to the internet so it's not following links and instead just starts lying.
Are you saying that the answer is according to my former question? Because in the latter there is no hint for what I'm asking for (duff's device). Furthermore, when asked 'Write Duff's device in Pascal' the answer is quite different.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

domasz

  • Hero Member
  • *****
  • Posts: 630
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #55 on: January 05, 2023, 11:46:22 am »

Are you saying that the answer is according to my former question? Because in the latter there is no hint for what I'm asking for (duff's device). Furthermore, when asked 'Write Duff's device in Pascal' the answer is quite different.

Quite likely it only uses what you tell it and what it saved in the DB when they were teaching it. I got nice results when asking it to translate code and pasting the whole source code.

Fred vS

  • Hero Member
  • *****
  • Posts: 3954
    • StrumPract is the musicians best friend
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #56 on: January 05, 2023, 04:15:16 pm »
Hi
@Fred vS:
If you log in to your openai.com account, then at the top right there is a link called "Personal". Click on that, in the menu, click on "View Api keys". On the next page, there's a button in the midddle of the page called "Generate New API key"... btw. the api-key is 53 chars long.
Hth, regards Benny

Hello Benny.

Ho, indeed that must be the problem.
They sent me a code with 6 digits on my mobile and it seems that is not this one that I have to use.

But when I am on the https://chat.openai.com/chat page (that works ok after connecting via my Gmail account)), I dont see  at the top right a link called "Personal".
Or maybe I miss something (as usual).

Many thanks for your light.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

domasz

  • Hero Member
  • *****
  • Posts: 630
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #57 on: January 05, 2023, 05:11:17 pm »
You need to log in here:
https://beta.openai.com/overview
and then "Personal" is in the top, right corner.

zeljko

  • Hero Member
  • *****
  • Posts: 1998
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #58 on: January 05, 2023, 05:46:34 pm »

Q: translate https://gist.githubusercontent.com/alpinistbg/f69313cdd6a7f4a735213e4a4b215c8f/raw/b73a3ea23c710e0c5474a8f7c125129a734302d9/duff_copy.c to pascal


I read it has no access to the internet so it's not following links and instead just starts lying.
Are you saying that the answer is according to my former question? Because in the latter there is no hint for what I'm asking for (duff's device). Furthermore, when asked 'Write Duff's device in Pascal' the answer is quite different.

Q: Write Duff's device in freepascal

{$MODE DELPHI}

procedure DuffDevice(var Data: array of Integer; Count : Integer);
var
  i, n : Integer;
begin
  n := Count div 8;
  i := 0;
  asm
    mov ECX, n
    jecxz @Exit

  @Loop:
    mov EAX, [Data + 4 * i]
    mov EBX, [Data + 4 * (i+1)]
    mov [Data + 4 * i], EBX
    mov [Data + 4 * (i + 1)], EAX
    mov EAX, [Data + 4 * (i + 2)]
    mov EBX, [Data + 4 * (i + 3)]
    mov [Data + 4 * (i + 2)], EBX
    mov [Data + 4 * (i + 3)], EAX
    mov EAX, [Data + 4 * (i + 4)]
    mov EBX, [Data + 4 * (i + 5)]
    mov [Data + 4 * (i + 4)], EBX
    mov [Data + 4 * (i + 5)], EAX
    mov EAX, [Data + 4 * (i + 6)]
    mov EBX, [Data + 4 * (i + 7)]
    mov [Data + 4 * (i + 6)], EBX
    mov [Data + 4 * (i + 7)], EAX
    add i, 8
    loop @Loop

  @Exit:
  end;
end;

var
  Data : array[0..7] of Integer;

begin
  Data[0] := 1;
  Data[1] := 2;
  Data[2] := 3;
  Data[3] := 4;
  Data[4] := 5;
  Data[5] := 6;
  Data[6] := 7;
  Data[7] := 8;
  DuffDevice(Data, 8);
end.

Fred vS

  • Hero Member
  • *****
  • Posts: 3954
    • StrumPract is the musicians best friend
Re: Programming Pascal with an AI Chatbot 2.0
« Reply #59 on: January 05, 2023, 06:40:35 pm »
You need to log in here:
https://beta.openai.com/overview
and then "Personal" is in the top, right corner.

Domasz you are Great.

Indeed with that code generated on this site, it is working.   ;D

Many thanks to you, Thaddy and Josh (your gui app works also great).

Fre;D

I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018