Recent

Author Topic: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?  (Read 10392 times)

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #30 on: December 25, 2022, 06:15:57 pm »
Despite the initial "Wow" impression, it raises a numerous controversies.
Quote
Given criticisms of GPT-3’s bias and abusive language patterns, it seems that OpenAI hasn’t found a way to prevent algorithms from inheriting its training data’s worst elements.
On the Dangers of Stochastic Parrots: Can Language Models Be Too Big?
Class-action lawsuit filed against Microsoft's GitHub Copilot for software piracy
GitHub and Copilot Intellectual Property Litigation

AFAIR there was a discussion in that forum some time ago about GitHub's possible nefarious use of the open source code and perhaps it comes to fruition.
Isn't it that fear one of the reasons to move Laz to Gitlab?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #31 on: December 25, 2022, 06:49:32 pm »
Well, that lawsuit is a bit ridiculous. In my opinion the OpenAI partners are allowed to use open sourcecode to feed their learning models as long as they adhere to the licenses.
Which they do. Frivolous and nothing worth paying attention to.
Happens all the time.
The main guy involved is probably a "better" lawyer than he is as a programmer.
"case dismissed"  ::) No merit.

(did a prep in law way back in 1981 as a second study (propedeuse) because I was bored and passed it with honors, dropped it for a board place in a student organisation. Law is often a second study in the Netherlands, it is easy and only requires lookups and a bit of memorizing, the philosophy part came already with my political science, in parallel and better)
« Last Edit: December 25, 2022, 07:08:37 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #32 on: December 25, 2022, 08:13:12 pm »
Aside from the legal stuff, which I admit I'm not well versed in, using a huge amount of code of varying quality is of questionable outcome to say the least.

In my everyday work with a limited aspect of NN (namely ALPR) it is easy to see its peculiarities compared to more traditional methods and to imagine how they can be magnified by using such a mammoth volumes of training data as those into the recent NLP models.

Perhaps the first paper cited is worth reading.

"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #33 on: December 25, 2022, 11:37:20 pm »
Aside from the legal stuff, which I admit I'm not well versed in,

The issue appears to be that the "AI" isn't propagating an acceptable synthesis of the licenses stipulated for the code from which it's borrowing.

OK, so has anybody tried the "Star Trek trick" of asking it to evaluate "this statement is false"? Or some heavy maths involving imaginary numbers?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1064
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #34 on: December 26, 2022, 01:05:51 am »
I've tried some variations of a simple task. The starting one was:
Q: write a program in pascal which prints Fibonacci numbers in range from 1 to 100
And the answer was:
Code: Pascal  [Select][+][-]
  1. program PrintFibonacci;
  2.  
  3. var
  4.   current, next, count: Integer;  // loop variables
  5.  
  6. begin
  7.   current := 1;  // initialize current to the first Fibonacci number
  8.   next := 1;  // initialize next to the second Fibonacci number
  9.   count := 1;  // initialize count to 1
  10.  
  11.   WriteLn(current);  // print the first Fibonacci number
  12.  
  13.   while count < 100 do  // loop until we have printed 100 Fibonacci numbers
  14.   begin
  15.     WriteLn(next);  // print the next Fibonacci number
  16.     Inc(count);  // increment the count
  17.  
  18.     next := current + next;  // calculate the next Fibonacci number
  19.     current := next - current;  // update current to the previous value of next
  20.   end;
  21. end.
  22.  
It will obviously print 100 Fibonacci numbers no matter of the range.

The next one was:
Q: write a program in pascal which prints half of the Fibonacci numbers in range from 1 to 100
The answer was:
Code: Pascal  [Select][+][-]
  1. program PrintHalfFibonacci;
  2.  
  3. var
  4.   current, next, count: Integer;  // loop variables
  5.  
  6. begin
  7.   current := 1;  // initialize current to the first Fibonacci number
  8.   next := 1;  // initialize next to the second Fibonacci number
  9.   count := 1;  // initialize count to 1
  10.  
  11.   WriteLn(current);  // print the first Fibonacci number
  12.  
  13.   while count < Trunc(100 / 2) do  // loop until we have printed half of the desired number of Fibonacci numbers
  14.   begin
  15.     WriteLn(next);  // print the next Fibonacci number
  16.     Inc(count);  // increment the count
  17.  
  18.     next := current + next;  // calculate the next Fibonacci number
  19.     current := next - current;  // update current to the previous value of next
  20.   end;
  21. end.
As it can be seen, it will print 50 numbers, which is 5x 10x times more than needed .

Then:
Q: write a program in pascal which prints first 13 of the prime numbers
Code: Pascal  [Select][+][-]
  1. program PrintPrimes;
  2.  
  3. const
  4.   MAX_PRIMES = 13;  // number of prime numbers to print
  5.  
  6. var
  7.   num, count: Integer;  // loop variables
  8.  
  9. begin
  10.   count := 0;  // initialize count to 0
  11.  
  12.   for num := 2 to MaxInt do  // loop through all positive integers
  13.   begin
  14.     if IsPrime(num) then  // check if the current number is prime
  15.     begin
  16.       WriteLn(num);  // print the prime number
  17.       Inc(count);  // increment the count
  18.     end;
  19.  
  20.     if count = MAX_PRIMES then  // if we have printed the desired number of primes, exit the loop
  21.       Break;
  22.   end;
  23. end.
  24.  
And that one will work.

The last one:
Q: write a program in pascal which prints the first two and the last three of the prime numbers not bigger than 100
Code: Pascal  [Select][+][-]
  1. program PrintPrimeRange;
  2.  
  3. var
  4.   num, count: Integer;  // loop variables
  5.   last_three: array[1..3] of Integer;  // array to store the last three prime numbers
  6.  
  7. begin
  8.   count := 0;  // initialize count to 0
  9.  
  10.   for num := 2 to 100 do  // loop through the range from 2 to 100
  11.   begin
  12.     if IsPrime(num) then  // check if the current number is prime
  13.     begin
  14.       Inc(count);  // increment the count
  15.  
  16.       if count <= 2 then  // if count is less than or equal to 2, print the prime number
  17.         WriteLn(num)
  18.       else  // if count is greater than 2, store the prime number in the last_three array
  19.         last_three[count - 2] := num;
  20.     end;
  21.   end;
  22.  
  23.   for num := 1 to 3 do  // loop through the last_three array and print the stored prime numbers
  24.     WriteLn(last_three[num]);
  25. end.
  26.  
This one will print 2, 3 and give a range error on the 6-th prime, 13.

For me, it is of no use at all. Assuming you should go through every line of the generated code to search for errors, then where is the point?
« Last Edit: December 26, 2022, 10:46:49 am by y.ivanov »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #35 on: December 26, 2022, 09:35:07 am »
I have used OpenAI chatGPT to answer a topic in another thread:
https://forum.lazarus.freepascal.org/index.php/topic,61711.msg465087.html#msg465087

I was using this slightly better code where you can formulate any request and just returns the answer:
Code: Pascal  [Select][+][-]
  1. program fpopenai_6;
  2. {
  3.   Demo on how to use openai in Freepascal
  4.   Find out more at https://openai.com where you can also obtain a key.
  5.   Put the key in a file called openai.key surrounded by single quotes.
  6.  
  7.   Enjoy, Thaddy de Koning, 26 december 2022
  8. }  
  9. {$mode objfpc}{$ifdef mswindows}{$apptype console}{$endif}{$H+}
  10. uses classes, sysutils,fphttpclient,opensslsockets,fpJson, jsonparser;
  11. const
  12.   model ='{"model": "text-davinci-003",  "prompt": "%s","temperature": 0.7,"max_tokens": 2048,"top_p": 1,  "frequency_penalty": 0,  "presence_penalty": 0}';
  13. var
  14.   d:TJsonData;
  15.   s:string;
  16. begin
  17.   writeln('What is your request?');
  18.   readln(s);
  19.   writeln;
  20.   with TfpHttpClient.Create(nil) do
  21.   Try
  22.     AllowRedirect:= true;
  23.     RequestHeaders.Add('Content-Type: application/json');
  24.     // put your key with single quotes in a textfile called openai.key
  25.     RequestHeaders.Add('Authorization: Bearer '+{$I openai.key});
  26.     RequestBody:=TStringStream.Create(format(model,[s]));    
  27.     writeln('Please wait for the response, this can take some time:');
  28.     try
  29.       d:=GetJson(Post('https://api.openai.com/v1/completions'));
  30.       writeln(d.findpath('choices[0].text').AsString);
  31.     finally
  32.       d.free;
  33.     end;    
  34.   finally
  35.     RequestBody.Free;
  36.     Free;
  37.   end;
  38. end.
Code: [Select]
What is your question?
write a pascal function that returns the inverse hyperbolic cosequant

Please wait for the response, this can take some time:


Function InverseHyperbolicCosecant(x : Real): Real;
Begin
    Result := ArcCosh(1/x);
End;

The model can give different responses for the same request...
Try to make the same request more than once...
« Last Edit: December 26, 2022, 10:29:08 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #36 on: December 26, 2022, 11:55:56 am »
You can also ask the model to give multiple solutions like here:
Code: Bash  [Select][+][-]
  1. What is your request?
  2. write three pascal programs that solve the knapsack problem in three different ways
  3.  
  4. Please wait for the response, this can take some time:
  5.  
  6.  
  7. //Solution 1: Dynamic Programming
  8.  
  9. program Knapsack;
  10.  
  11. const
  12.   MAXWEIGHT = 100;
  13.   MAXITEMS = 10;
  14.  
  15. type
  16.   Item = record
  17.     weight, value : integer;
  18.   end;
  19.  
  20. var
  21.   items : array[1..MAXITEMS] of Item;
  22.   maxval : array[0..MAXWEIGHT] of integer;
  23.   numitems, weight : integer;
  24.   i, w : integer;
  25.  
  26. begin
  27.   // Read data
  28.   writeln('How many items?');
  29.   readln(numitems);
  30.   for i := 1 to numitems do
  31.     begin
  32.       writeln('Enter weight and value of item ', i);
  33.       readln(items[i].weight, items[i].value);
  34.     end;
  35.   writeln('Maximum weight?');
  36.   readln(weight);
  37.  
  38.   // Initialize maxval array
  39.   maxval[0] := 0;
  40.   for w := 1 to weight do
  41.     maxval[w] := -1;
  42.  
  43.   // Calculate maximum value
  44.   for i := 1 to numitems do
  45.     for w := weight downto 0 do
  46.       if (items[i].weight <= w) and (maxval[w - items[i].weight] >= 0) then
  47.         if maxval[w] < 0 then
  48.           maxval[w] := maxval[w - items[i].weight] + items[i].value
  49.         else
  50.           maxval[w] := maxval[w] + maxval[w - items[i].weight] + items[i].value;
  51.  
  52.   // Show result
  53.   writeln('Maximum value = ', maxval[weight]);
  54. end.
  55.  
  56.  
  57. //Solution 2: Greedy Algorithm
  58.  
  59. program Knapsack;
  60.  
  61. const
  62.   MAXWEIGHT = 100;
  63.   MAXITEMS = 10;
  64.  
  65. type
  66.   Item = record
  67.     weight, value : integer;
  68.     ratio : real;
  69.   end;
  70.  
  71. var
  72.   items : array[1..MAXITEMS] of Item;
  73.   maxval : array[0..MAXWEIGHT] of integer;
  74.   numitems, weight : integer;
  75.   i, w : integer;
  76.  
  77. function cmp(i1, i2 : integer) : boolean;
  78. begin
  79.   cmp := items[i1].ratio > items[i2].ratio;
  80. end;
  81.  
  82. begin
  83.   // Read data
  84.   writeln('How many items?');
  85.   readln(numitems);
  86.   for i := 1 to numitems do
  87.     begin
  88.       writeln('Enter weight and value of item ', i);
  89.       readln(items[i].weight, items[i].value);
  90.       items[i].ratio := items[i].value / items[i].weight;
  91.     end;
  92.   writeln('Maximum weight?');
  93.   readln(weight);
  94.  
  95.   // Sort items by ratio
  96.   for i := 1 to numitems - 1 do
  97.     for j := i + 1 to numitems do
  98.       if cmp(j, i) then
  99.         begin
  100.           items[0] := items[i];
  101.           items[i] := items[j];
  102.           items[j] := items[0];
  103.         end;
  104.  
  105.   // Calculate maximum value
  106.   maxval[0] := 0;
  107.   for i := 1 to numitems do
  108.     for w := weight downto 0 do
  109.       if (items[i].weight <= w) and (maxval[w - items[i].weight] >= 0) then
  110.         maxval[w] := maxval[w] + items[i].value;
  111.  
  112.   // Show result
  113.   writeln('Maximum value = ', maxval[weight]);
  114. end.
  115.  
  116.  
  117. //Solution 3: Branch and Bound
  118.  
  119. program Knapsack;
  120.  
  121. const
  122.   MAXWEIGHT = 100;
  123.   MAXITEMS = 10;
  124.  
  125. type
  126.   Item = record
  127.     weight, value : integer;
  128.     ratio : real;
  129.   end;
  130.  
  131. var
  132.   items : array[1..MAXITEMS] of Item;
  133.   maxval : array[0..MAXWEIGHT] of integer;
  134.   numitems, weight : integer;
  135.   i, w : integer;
  136.  
  137. function cmp(i1, i2 : integer) : boolean;
  138. begin
  139.   cmp := items[i1].ratio > items[i2].ratio;
  140. end;
  141.  
  142. procedure branch_and_bound(i, w : integer; curval : integer);
  143. begin
  144.   if i > numitems then
  145.     begin
  146.       if curval > maxval[w] then
  147.         maxval[w] := curval;
  148.       exit;
  149.     end;
  150.   if (items[i].weight <= w) and (curval + maxval[w - items[i].weight] > maxval[w]) then
  151.     begin
  152.       branch_and_bound(i + 1, w, curval);
  153.       branch_and_bound(i + 1, w - items[i].weight, curval + items[i].value);
  154.     end;
  155. end;
  156.  
  157. begin
  158.   // Read data
  159.   writeln('How many items?');
  160.   readln(numitems);
  161.   for i := 1 to numitems do
  162.     begin
  163.       writeln('Enter weight and value of item ', i);
  164.       readln(items[i].weight, items[i].value);
  165.       items[i].ratio := items[i].value / items[i].weight;
  166.     end;
  167.   writeln('Maximum weight?');
  168.   readln(weight);
  169.  
  170.   // Sort items by ratio
  171.   for i := 1 to numitems - 1 do
  172.     for j := i + 1 to numitems do
  173.       if cmp(j, i) then
  174.         begin
  175.           items[0] := items[i];
  176.           items[i] := items[j];
  177.           items[j] := items[0];
  178.         end;
  179.  
  180.   // Initialize maxval array
  181.   maxval[0] := 0;
  182.   for w := 1 to weight do
  183.     maxval[w] := -1;
  184.  
  185.   // Calculate maximum value
  186.   branch_and_bound(1, weight, 0);
  187.  
  188.   // Show result
  189.   writeln('Maximum value = ', maxval[weight]);
  190. end.
Now you should be impressed.. ;D
Seems a perfect way to not have to answer the recurring requests for help that can be identified as homework....
« Last Edit: December 26, 2022, 12:03:27 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #37 on: December 26, 2022, 12:12:30 pm »
Or these two moonlander games (inspired by the first game I wrote in BASIC on a Burroughs terminal in 1979).
Code: Text  [Select][+][-]
  1. What is your request?
  2. write two pascal programs that implement a moonlander game in two different ways
  3.  
  4. Please wait for the response, this can take some time:
  5.  
  6.  
  7. 1. Program to Implement a Moonlander Game Using a While Loop
  8.  
  9. program MoonlanderGame;
  10.  
  11. var
  12.   fuel, velocity, altitude : real;
  13.   done : boolean;
  14.  
  15. begin
  16.   done := false;
  17.   altitude := 1000;
  18.   fuel := 50;
  19.   velocity := 0;
  20.  
  21.   writeln('Welcome to Moonlander!');
  22.   writeln('You have 50 units of fuel to land your lunar module on the moon.');
  23.   writeln('Good luck!');
  24.  
  25.   while not done do
  26.   begin
  27.     writeln('Altitude = ', altitude:6:2, 'Velocity = ', velocity:6:2, 'Fuel = ', fuel:6:2);
  28.     writeln('Enter 1 to fire thruster, 0 to skip: ');
  29.     if (readkey = '1') then
  30.     begin
  31.       if (fuel > 0) then
  32.       begin
  33.         fuel := fuel - 1;
  34.         velocity := velocity + 1;
  35.         altitude := altitude - velocity;
  36.       end
  37.       else
  38.         writeln('Out of fuel!');
  39.     end
  40.     else
  41.       altitude := altitude - velocity;
  42.  
  43.     if (altitude <= 0) then
  44.       done := true;
  45.   end;
  46.  
  47.   if (velocity = 0) then
  48.     writeln('Landed safely!')
  49.   else
  50.     writeln('Crashed!');
  51. end.
  52.  
  53. 2. Program to Implement a Moonlander Game Using a Repeat-Until Loop
  54.  
  55. program MoonlanderGame;
  56.  
  57. var
  58.   fuel, velocity, altitude : real;
  59.  
  60. begin
  61.   altitude := 1000;
  62.   fuel := 50;
  63.   velocity := 0;
  64.  
  65.   writeln('Welcome to Moonlander!');
  66.   writeln('You have 50 units of fuel to land your lunar module on the moon.');
  67.   writeln('Good luck!');
  68.  
  69.   repeat
  70.     writeln('Altitude = ', altitude:6:2, 'Velocity = ', velocity:6:2, 'Fuel = ', fuel:6:2);
  71.     writeln('Enter 1 to fire thruster, 0 to skip: ');
  72.     if (readkey = '1') then
  73.     begin
  74.       if (fuel > 0) then
  75.       begin
  76.         fuel := fuel - 1;
  77.         velocity := velocity + 1;
  78.         altitude := altitude - velocity;
  79.       end
  80.       else
  81.         writeln('Out of fuel!');
  82.     end
  83.     else
  84.       altitude := altitude - velocity;
  85.   until (altitude <= 0);
  86.  
  87.   if (velocity = 0) then
  88.     writeln('Landed safely!')
  89.   else
  90.     writeln('Crashed!');
  91. end.
« Last Edit: December 26, 2022, 12:14:51 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #38 on: December 26, 2022, 04:36:19 pm »
And even christmas dinner (Yes, we have duck from the Kamado on second christmas day):
Code: [Select]
What is your request?
Can you give me a recipe to cook a whole duck on the Kamado? I prefer an orange sauce and plenty of garlic.

Please wait for the response, this can take some time:


Ingredients:

1 whole duck

1 orange

2 cloves garlic, minced

1 teaspoon ground ginger

1 teaspoon ground cinnamon

1 teaspoon ground nutmeg

2 tablespoons olive oil

Salt and pepper to taste

Directions:

1. Preheat the Kamado grill to 350 degrees F.

2. Rinse the duck and pat dry with paper towels.

3. Rub the duck with the olive oil and season with salt and pepper.

4. Place the duck on the grill and cook for 1 hour and 15 minutes, flipping every 15 minutes.

5. Meanwhile, in a medium saucepan, combine the orange juice, garlic, ginger, cinnamon, and nutmeg. Bring to a simmer over medium heat and cook until reduced by half.

6. Remove the duck from the grill and transfer to a cutting board. Let rest for 10 minutes before carving.

7. Serve the duck alongside the orange sauce and garlic. Enjoy!
The recipe is good, but I still use my own twist.
« Last Edit: December 26, 2022, 04:58:57 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #39 on: January 02, 2023, 03:19:36 pm »
When I enter
Quote
create pascal source code example for socketcan
into
Quote
https://chat.openai.com/chat
I get this almost working code:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   Classes, SysUtils, Sockets, can;
  5.  
  6. var
  7.   sock: LongInt;
  8.   ifr: TIfReq;
  9.   addr: TSockAddrCAN;
  10.   frame: TCanFrame;
  11.   nbytes: LongInt;
  12. begin
  13.   // Create a socket
  14.   sock := fpsocket(PF_CAN, SOCK_RAW, CAN_RAW);
  15.   if sock < 0 then
  16.   begin
  17.     raise Exception.Create('Error creating socket');
  18.     exit;
  19.   end;
  20.  
  21.   // Bind the socket to a CAN interface
  22.   StrPCopy(ifr.ifr_name, 'can0');
  23.   fpsioctl(sock, SIOCGIFINDEX, @ifr);
  24.  
  25.   addr.can_family := AF_CAN;
  26.   addr.can_ifindex := ifr.ifr_ifindex;
  27.   if fpsocket(sock, SOCK_RAW, PF_CAN, @addr, SizeOf(addr)) < 0 then
  28.   begin
  29.     raise Exception.Create('Error binding socket to interface');
  30.     exit;
  31.   end;
  32.  
  33.   // Build the CAN frame
  34.   frame.can_id := 0x123;
  35.   frame.can_dlc := Length('Hello, world!');
  36.   Move('Hello, world!', frame.data[0], frame.can_dlc);
  37.  
  38.   // Send the frame
  39.   nbytes := fpsend(sock, @frame, SizeOf(frame), 0);
  40.   if nbytes < 0 then
  41.   begin
  42.     raise Exception.Create('Error sending frame');
  43.     exit;
  44.   end;
  45.  
  46.   WriteLn('Frame sent successfully');
  47.  
  48.   // Close the socket
  49.   fpclose(sock);
  50. end.
  51.  

This is crazy. My SocketCAN lib did not get into official repo yet. This forum is the only place where you can find any pascal SocketCAN code, and it was not a translation from some other language to pascal because it uses exact names of the types and units I used.

Big brother is watching...  :o
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #40 on: January 02, 2023, 03:30:48 pm »
This is crazy. My SocketCAN lib did not get into official repo yet. This forum is the only place where you can find any pascal SocketCAN code,

The really interesting thing there is whether it would respect the robots=noindex tag.

It's probably too late to find out.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #41 on: January 02, 2023, 03:37:53 pm »
Different CAN, same question:
Code: Text  [Select][+][-]
  1. program SocketCAN;
  2.  
  3. uses
  4.   SysUtils,
  5.   SocketCAN;
  6.  
  7. // create a socket to use
  8. var
  9.   Socket: TSocketCAN;
  10.  
  11. // create a message to send
  12. var
  13.   SendMsg: TMsgCAN;
  14.   SendData: array[0..7] of Byte;
  15.  
  16. // create a message to receive
  17. var
  18.   RecvMsg: TMsgCAN;
  19.   RecvData: array[0..7] of Byte;
  20.  
  21. // setup the message to send
  22. SendMsg.ID := $100;
  23. SendMsg.DLC := 8;
  24. for i := 0 to 7 do
  25.   SendData[i] := i;
  26. SendMsg.Data := @SendData;
  27.  
  28. // open the socket
  29. Socket := TSocketCAN.Create;
  30. Socket.Open('vcan0');
  31.  
  32. // send the message
  33. Socket.SendMsg(@SendMsg);
  34.  
  35. // receive the message
  36. Socket.ReceiveMsg(@RecvMsg);
  37.  
  38. // save the received data
  39. for i := 0 to 7 do
  40.   RecvData[i] := RecvMsg.Data[i];
  41.  
  42. // close the socket
  43. Socket.Close;
  44.  
  45. // print the received data
  46. for i := 0 to 7 do
  47.   Writeln(IntToHex(RecvData[i], 2));
  48.  
  49. end.
needs more work.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mika

  • Full Member
  • ***
  • Posts: 102
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #42 on: January 02, 2023, 06:18:45 pm »
Can AI write program in Pascal that produces compiler internal error? Answer will give away where from AI have learned that.

korba812

  • Sr. Member
  • ****
  • Posts: 394
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #43 on: January 02, 2023, 10:37:26 pm »
When I enter
Quote
create pascal source code example for socketcan
into
Quote
https://chat.openai.com/chat
I get this almost working code:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   Classes, SysUtils, Sockets, can;
  5.  
  6. var
  7.   sock: LongInt;
  8.   ifr: TIfReq;
  9.   addr: TSockAddrCAN;
  10.   frame: TCanFrame;
  11.   nbytes: LongInt;
  12. begin
  13.   // Create a socket
  14.   sock := fpsocket(PF_CAN, SOCK_RAW, CAN_RAW);
  15.   if sock < 0 then
  16.   begin
  17.     raise Exception.Create('Error creating socket');
  18.     exit;
  19.   end;
  20.  
  21.   // Bind the socket to a CAN interface
  22.   StrPCopy(ifr.ifr_name, 'can0');
  23.   fpsioctl(sock, SIOCGIFINDEX, @ifr);
  24.  
  25.   addr.can_family := AF_CAN;
  26.   addr.can_ifindex := ifr.ifr_ifindex;
  27.   if fpsocket(sock, SOCK_RAW, PF_CAN, @addr, SizeOf(addr)) < 0 then
  28.   begin
  29.     raise Exception.Create('Error binding socket to interface');
  30.     exit;
  31.   end;
  32.  
  33.   // Build the CAN frame
  34.   frame.can_id := 0x123;
  35.   frame.can_dlc := Length('Hello, world!');
  36.   Move('Hello, world!', frame.data[0], frame.can_dlc);
  37.  
  38.   // Send the frame
  39.   nbytes := fpsend(sock, @frame, SizeOf(frame), 0);
  40.   if nbytes < 0 then
  41.   begin
  42.     raise Exception.Create('Error sending frame');
  43.     exit;
  44.   end;
  45.  
  46.   WriteLn('Frame sent successfully');
  47.  
  48.   // Close the socket
  49.   fpclose(sock);
  50. end.
  51.  

This is crazy. My SocketCAN lib did not get into official repo yet. This forum is the only place where you can find any pascal SocketCAN code, and it was not a translation from some other language to pascal because it uses exact names of the types and units I used.

Big brother is watching...  :o
I don't think the AI wrote it himself. It looks similar:
https://github.com/speedbyte/llcf/blob/9565eaab6eaf7af5ea3dbf094d05d0bc252e15f3/llcf_src_vw/src/test/tst-raw-sendto-SingStream.c

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #44 on: January 03, 2023, 08:53:26 pm »
I don't think the AI wrote it himself. It looks similar:
https://github.com/speedbyte/llcf/blob/9565eaab6eaf7af5ea3dbf094d05d0bc252e15f3/llcf_src_vw/src/test/tst-raw-sendto-SingStream.c
You are right. My simple demos have C origins so that might be the answer. It still amazes me how it named structures on the fly in pascal fashion.

I wanted to check it, so I remembered that I have made 2 unique socketcan utilities which exist only in pascal and have no C counter part that I know of, and I instructed AI to generate code similar to one of them (hlcanerrdump):

Quote
Q: write pascal code to check errors in socketcan messages and display their textual meaning
Quote
A: To check for errors in SocketCAN messages and display their textual meaning in Pascal, you can use the following code:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., // Include any necessary units here
  3.   SocketCAN;
  4.  
  5. const
  6.   // Define an array of error codes and their corresponding textual meanings
  7.   ERROR_CODES: array[0..15] of string = (
  8.     'No Error',
  9.     'Transmission request failed',
  10.     'Lost arbitration',
  11.     'Controller problems',
  12.     'Protocol violation',
  13.     'Transceiver status',
  14.     'No acknowledge on transmission',
  15.     'Software errors',
  16.     'CAN error passive',
  17.     'Controller restarted',
  18.     'Taken into operation',
  19.     'Left operation',
  20.     'CAN error warning',
  21.     'Protocol error in data field',
  22.     'Protocol error in transmission',
  23.     'Transceiver not available'
  24.   );
  25.  
  26. var
  27.   message: TSockCANMessage;
  28.   errorCode: Byte;
  29. begin
  30.   // Read the message from the SocketCAN interface
  31.   ReadSocketCANMessage(message);
  32.  
  33.   // Check the error code in the message
  34.   errorCode := message.ErrorCode;
  35.   if (errorCode > 0) and (errorCode <= 15) then
  36.   begin
  37.     // If an error is detected, display the corresponding textual meaning
  38.     writeln('Error detected: ', ERROR_CODES[errorCode]);
  39.   end;
  40. end.
Quote
This code defines an array of error codes and their corresponding textual meanings, and then reads a message from the SocketCAN interface using the ReadSocketCANMessage function. It then checks the error code in the message and, if an error is detected, displays the corresponding textual meaning.

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

Ok, that was very rudimentary, so I spiced it a little...

Quote
Q: do it again but using TxTimeout, LostArbitration and other individual bits
Quote
A: To check for specific errors in SocketCAN messages using individual bits and display their textual meanings in Pascal, you can use the following code:
Code: Pascal  [Select][+][-]
  1. uses
  2.   ..., // Include any necessary units here
  3.   SocketCAN;
  4.  
  5. const
  6.   // Define an array of error codes and their corresponding textual meanings
  7.   ERROR_CODES: array[0..7] of string = (
  8.     'No Error',
  9.     'Transmission request failed',
  10.     'Lost arbitration',
  11.     'Controller problems',
  12.     'Protocol violation',
  13.     'Transceiver status',
  14.     'No acknowledge on transmission',
  15.     'Software errors'
  16.   );
  17.  
  18. var
  19.   message: TSockCANMessage;
  20.   errorCode: Byte;
  21. begin
  22.   // Read the message from the SocketCAN interface
  23.   ReadSocketCANMessage(message);
  24.  
  25.   // Check the individual bits in the error code
  26.   if (message.ErrorCode and SocketCAN_ErrorFlag_TxTimeout) <> 0 then
  27.     writeln('Error detected: Transmission timeout');
  28.   if (message.ErrorCode and SocketCAN_ErrorFlag_LostArbitration) <> 0 then
  29.     writeln('Error detected: Lost arbitration');
  30.   if (message.ErrorCode and SocketCAN_ErrorFlag_ControllerProblems) <> 0 then
  31.     writeln('Error detected: Controller problems');
  32.   // Check the other individual bits as needed
  33.  
  34.   // Check the general error code as a fallback
  35.   errorCode := message.ErrorCode and $0F;
  36.   if (errorCode > 0) and (errorCode <= 7) then
  37.   begin
  38.     // If an error is detected, display the corresponding textual meaning
  39.     writeln('Error detected: ', ERROR_CODES[errorCode]);
  40.   end;
  41. end.
Quote
This code defines an array of general error codes and their corresponding textual meanings, and then reads a message from the SocketCAN interface using the ReadSocketCANMessage function. It then checks the individual bits in the error code to detect specific errors, and displays the corresponding textual meaning. If no specific error is detected, it falls back to checking the general error code and, if an error is detected, displays the corresponding textual meaning.

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

Still not even close to hlcanerrdump, but it built a basic skelethon which is almost usable. I really didn't expect that.

I even continued with
Quote
Q: also include check for OverflowRX bit
and it obeyed joyfully.

Wow factor has just been amplified...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018