ChatGPT integration examples?

This forum is meant for examples of X# code.

jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

I'm trying to integrate a ChatGPT query in my X#/VO/Winform application - I'm sure some of the group must've done this already.

I'm making a function to use ChatGPT to elaborate on a basic summary of a job that will be seeded by including some facts and details -customer name, address of a venue, and some other information about the equipment and what the event is achieving.

I've tried a couple of libraries and they look good: https://github.com/D7EAD/liboai, and https://www.nuget.org/packages/OpenAI/
Linked the api library as a reference in my app and Added USING OpenAI_API at the top, but that's about it.

I'm stuck declaring the Vars correctly... <yes, I do have an API key>

Here's the thing below that I'm trying to achieve but declaring those Vars... other than that I think the general idea is (almost) correct. (this is in a winform)

METHOD PBOpenAIClick(sender AS System.Object , e AS System.EventArgs) AS VOID
LOCAL request AS ???
LOCAL Openai AS ???
LOCAL Response AS ???

openai := OpenAI.OpenAIAPI{(" <my API Key> ")}
request := CompletionRequest{}

request:Model := "text-davinci-002"
request:Prompt := "Acting as a copywriter, write a description of an event in about 200 words, in the style of a summary. The Date and times are 'XYZ', The location and organiser is: 'ZYX', and... "
request:MaxTokens := 50
request:Temperature := 0.7

response := openai:Completion:Create(request)

textbox{,,AsString(response:Choices[0])}:show()
RETURN

Can anyone share a snippet of syntax and point out a library they use to do something similar already?

Thank you.
Jonathan
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

ChatGPT integration examples?

Post by wriedmann »

Hi Jonathan,
my wife has done that in plain PHP without using any library (in here corrispondence tool the ChatGPT integration is in production now for several weeks), so I would do that without any special library with plain .NET code. It must be a normal webservice.
I will ask here and look at the code and let you know.
Wolfgang
P.S. in the past I had used Googles libraries for the Google Ads API, but that has been gotten too complicate and since them I use plain X# code for this
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

Thanks Wolfgang,
I asked ChatGPT about that and she suggested using RESTSharp, with an example (my translation first, and the actual C# suggestion below)

LOCAL client AS RestClient
LOCAL request AS RestRequest

client := RestClient("https://api.openai.com/v1")
request := RestRequest("/engines/davinci-codex/completions", Method.POST)
THESE 2 LINES ABOVE GIVE: error XS1955: Non-invocable member 'RestSharp.RestClient' cannot be used like a method.

request:AddHeader("Authorization", "Bearer MY API KEY")
request:AddParameter("prompt", "Hello, world!")
request:AddParameter("max_tokens", 5)
request:AddParameter("n", 1)

LOCAL response, Content AS USUAL
response := client:Execute(request)
THIS LINE ABOVE GIVES: error XS7069: Reference to type 'System.Threading.Tasks.Task<>' claims it is defined in 'System.Runtime', but it could not be found

content := response:Content

THE ACTUAL C# CODE THAT CHATGPT SUGGESTED (maybe I've translated it wrong)
using RestSharp;

var client = new RestClient("https://api.openai.com/v1");
var request = new RestRequest("/engines/davinci-codex/completions", Method.POST);
request.AddHeader("Authorization", "Bearer <your API key here>");
request.AddParameter("prompt", "Hello, world!");
request.AddParameter("max_tokens", 5);
request.AddParameter("n", 1);

var response = client.Execute(request);
var content = response.Content;
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

ChatGPT integration examples?

Post by wriedmann »

Hi Jonathan,
I've tried to translate:

Code: Select all

using RestSharp

var client := RestClient{ "api.openai.com/v1" }
var request = RestRequest{ "/engines/davinci-codex/completions", Method.POST }
request:AddHeader("Authorization", "Bearer <your API key here>")
request:AddParameter("prompt", "Hello, world!")
request:AddParameter("max_tokens", 5)
request:AddParameter("n", 1)

var response := client:Execute(request)
var content  := response:Content
Of course you have to add exception handling because this code can fail in several places.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

Thank you! Yes, much closer now.
It nearly compiles apart from an error on this line which I'll have to do some research on:

var response := client:Execute(request)

error XS7069: Reference to type 'System.Threading.CancellationToken' claims it is defined in 'System.Runtime', but it could not be found
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

ChatGPT integration examples?

Post by wriedmann »

Hi Jonathan,
it seems you are using a RestSharp DLL that is compiled for .NET and not for .NET Framework 4.x.
The package seems to include versions for 4.7.1, 6.0 and 7.0. For X# currently you have to take the one for 4.7.1.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

Yes, that was it... Thank you again.
It all compiles - now to work out the runtime errors.
I'll post the working example here when I get it working.
jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

This should probably work, but I'm getting a consistent error about the JSON being wrong, OR the HTTP library is being used incorrectly, but I'm following the examples, and it seems simple enough.

The Error: We could not parse the JSON body of your request – Hint: This is likely because you aren’t using your HTTP library correctly. Open AI API expects a JSON payload, but what was sent is not valid JSON.

I’m using RestSharp and had to reference system.Net.Http. I’ve tried creating different JSON strings using Newtonsoft.Json.Linq library but the error message remains the same so it is the way I’m using Restsharp or something else wrong – not the JSON.

The code is below –most likely I’ve got a parameter order wrong, or missing something in putting the request together - can anyone spot the mistake?

THE CODE
VAR client := RestSharp.RestClient{ https://api.openai.com/v1 }
VAR request := RestSharp.RestRequest{ "/engines/text-davinci-002/completions", RestSharp.Method.POST }
request:AddHeader("Authorization", "Bearer <My Key>")

request:AddHeader("Content-Type", "application/json")
request:AddParameter("prompt", "Hello, world!")
request:AddParameter("max_tokens", 5)
request:AddParameter("n", 1)

VAR response := client:Execute(request)
VAR content := response:Content

The content is returned ok, only containing the error message.


Below is an example in C# that I'm working from:
using RestSharp;

var client = new RestClient("https://api.openai.com/v1");
var request = new RestRequest("/engines/text-davinci-002/completions", Method.POST);
request.AddHeader("Authorization", "Bearer <My Key>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("prompt", "Hello, world!");
request.AddParameter("max_tokens", 5);
request.AddParameter("n", 1);

var response = client.Execute(request);
var content = response.Content;
User avatar
wriedmann
Posts: 3783
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

ChatGPT integration examples?

Post by wriedmann »

Hi Jonathan,
do you are using your key?
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
jonhn
Posts: 86
Joined: Thu Feb 01, 2018 8:04 am
Location: New Zealand

ChatGPT integration examples?

Post by jonhn »

Yes! I will email it to you for testing - if you want to try it.
Post Reply