ChatGpt RegEx

@Kem_Tekinay

2 Likes

Hmm…

Is Kem is going to be out of a job… replaced by AI?

2 Likes

What a fantastic tutorial. Not too fast, not too slow, everything step-by-step with clear visuals. The power of AI with nice UI made accessible in minutes, amazing.

1 Like

Here is the direct link to the video

The video is showing how to use the OpenAI API to build a RegEx generator. I don’t have the time yet to dive into, but it would be nice to have a Xojo project using the OpenAI API.

1 Like

Haha, here

my input to gpt-4: write me a xojo function that connects to the openai api

To connect to the OpenAI API from a Xojo application, you’ll need to make an HTTP request. Xojo has a built-in class called HTTPSocket that you can use to make HTTP requests. Here’s a Xojo function that connects to the OpenAI API:

Function ConnectToOpenAI(apiKey As String, prompt As String) As String
  // Define OpenAI API URL
  Const openAIUrl As String = "https://api.openai.com/v1/engines/davinci-codex/completions"
  
  // Initialize HTTPSocket
  Var http As New HTTPSocket
  
  // Set request headers
  http.SetRequestHeader("Content-Type", "application/json")
  http.SetRequestHeader("Authorization", "Bearer " + apiKey)
  
  // Define request data
  Var requestData As New Dictionary
  requestData.Value("prompt") = prompt
  requestData.Value("max_tokens") = 50
  requestData.Value("n") = 1
  requestData.Value("stop") = ""
  
  // Send request
  Var response As String
  Try
    response = http.PostJSON(openAIUrl, requestData)
  Catch e As IOException
    Return "Error: " + e.Message
  End Try
  
  // Parse response
  Var jsonResponse As Dictionary
  Try
    jsonResponse = ParseJSON(response)
  Catch e As JSONException
    Return "Error: " + e.Message
  End Try
  
  // Get generated text
  Var generatedText As String = ""
  If jsonResponse.HasKey("choices") Then
    Var choices As xojo.Core.Array = jsonResponse.Value("choices")
    If choices.Count > 0 Then
      Var choice As Dictionary = choices(0)
      If choice.HasKey("text") Then
        generatedText = choice.Value("text")
      End If
    End If
  End If
  
  Return generatedText
End Function

This function takes two arguments: the API key and the prompt you want to use. It sends a request to the OpenAI API and returns the generated text. Make sure to include the appropriate API key in the apiKey parameter when calling this function. Note that this example uses the DaVinci Codex engine; you can replace it with a different engine if needed.

my input to gpt-4: did you know that xojo now uses urlconnection?

Yes, Xojo has a more modern URLConnection class, which is a better choice for making HTTP requests. Here’s the same function using URLConnection instead of the older HTTPSocket :

Function ConnectToOpenAI(apiKey As String, prompt As String) As String
  // Define OpenAI API URL
  Const openAIUrl As String = "https://api.openai.com/v1/engines/davinci-codex/completions"
  
  // Initialize URLConnection
  Var http As New URLConnection
  
  // Set request headers
  http.RequestHeader("Content-Type") = "application/json"
  http.RequestHeader("Authorization") = "Bearer " + apiKey
  
  // Define request data
  Var requestData As New Dictionary
  requestData.Value("prompt") = prompt
  requestData.Value("max_tokens") = 50
  requestData.Value("n") = 1
  requestData.Value("stop") = ""
  
  // Send request
  Var response As String
  Try
    response = http.Send("POST", openAIUrl, GenerateJSON(requestData), 30)
  Catch e As Xojo.Core.InvalidJSONException
    Return "Error: " + e.Message
  End Try
  
  // Parse response
  Var jsonResponse As Dictionary
  Try
    jsonResponse = ParseJSON(response)
  Catch e As JSONException
    Return "Error: " + e.Message
  End Try
  
  // Get generated text
  Var generatedText As String = ""
  If jsonResponse.HasKey("choices") Then
    Var choices As xojo.Core.Array = jsonResponse.Value("choices")
    If choices.Count > 0 Then
      Var choice As Dictionary = choices(0)
      If choice.HasKey("text") Then
        generatedText = choice.Value("text")
      End If
    End If
  End If
  
  Return generatedText
End Function

This function is similar to the previous one, but uses the URLConnection class instead of the HTTPSocket class for making the request. The Send method is used to perform the request, and the response is handled in a similar way.

and now we have a simple connection to openai api.

1 Like

Kind of. The implementation is incorrect, as is the error handling.

3 Likes

Since ChatGpt remembers the context, you could add the error handling suggestions. And keep the fingers crossed for a better solution.

But, it still requires the input of an experienced Xojo coder to make those suggestions. The AI models have still a long way to go to make us obsolete.
But, AI might just be a helpful tool to get things done quicker.

1 Like

it can become a nice assistant if you learn how to talk to it, it will not replace you. not yet.

2 Likes

The /engines endpoint is deprecated.

https://platform.openai.com/docs/api-reference/engines

1 Like

yup, good catch. I just wanted to show the op, that he can basically do the same thing in Xojo, as the video he posted, but faster and easier imo.

I thought it’d be interesting to compare a corrected version to GPT’s original.

1 Like

As the Xojo.Core.*

I had some generated results that included the older framework.
I told ChatGPT that I wanted the newer Xojo API 2 framework.

It gave me a brief explanation about when the newer framework was introduced. But it happily continued with the whole Xojo.Core… stuff :unamused:

Also, I pointed out some mistakes. Like a method, used with AddHandler. It didn’t have the Sender parameter. It apologized and regenerated the code. Again, without the Sender parameter. After a 3rd try, it did generate the correct code.

It learns from the mistakes just as humans, until the 3rd try :slight_smile:

Thank you for correctly using JSONItem. Someone should correct ChatGPT to use JSONItem and not ParseJSON before this plague gets too far.

1 Like

Blasphemy!

1 Like

GPT-4’s training data is up to Sep. 2021 so I’m guessing if the documentation of Xojo at that time used Xojo.Core, etc. … that’s why he’s using so much old stuff.

What’s wrong with ParseJSON? It always seems more straightforward to me - put it all in a dictionary and I can deal with it from there.

Inspired by this and related threads, I used ChatGPT today to write me some Applescript - looks like it will work fine and saved me a bunch of googling and doc searching.