Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Trucos (https://www.clubdelphi.com/foros/forumdisplay.php?f=52)
-   -   Integrar ChatGPT en aplicación C++ Builder (https://www.clubdelphi.com/foros/showthread.php?t=96118)

chenech 20-02-2023 15:18:02

Integrar ChatGPT en aplicación C++ Builder
 
Por si le sirve a alguien, integrar una llamada al ChatGPT, un edit (EPregunta) para la pregunta y un memo (Memo1) para la respuesta (sustituir las XXX... por el código al registrarse).
Un éxito entre los clientes que tengo, jajajaja todos lo usan mucho ahora.
Código:

try
{
  TNetHeaders AuthHeader;
  AuthHeader.Length = 2;
  AuthHeader[0] = TNameValuePair("Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  AuthHeader[1] = TNameValuePair("Content-Type", "application/json");
  TMemoryStream *salida = new TMemoryStream();
  TMemoryStream *entrada = new TMemoryStream();
  TMemo *memo = new TMemo(Form148);
  memo->Parent = Form148;
  memo->Visible = false;
  memo->Lines->Clear();
  String  ASAux = EPregunta->Text;
  char dest[200];
  UnicodeToUtf8(dest, 200, EPregunta->Text.c_str(), 200);
  ASAux = dest;
  memo->Lines->Add("{\"model\": \"text-davinci-003\", \"prompt\": \"" + ASAux + "\", \"temperature\": 0, \"max_tokens\": 1000}");
  memo->Lines->SaveToStream(salida);
  salida->Position = soFromBeginning;
  NetHTTPC1->Post("https://api.openai.com/v1/completions", salida, entrada, AuthHeader);
  memo->Lines->Clear();
  memo->Lines->LoadFromStream(entrada);
  delete salida;
  delete entrada;
  TJSONObject *o = (TJSONObject*) TJSONObject::ParseJSONValue(TEncoding::ANSI->GetBytes(memo->Lines->Text), 0);
  TJSONArray *a = (TJSONArray*) o->Get("choices")->JsonValue;
  TJSONObject *book = (TJSONObject*) a->Items[0];
  Memo1->Lines->Clear();
  ASAux = book->Pairs[0]->JsonValue->ToString();
  ASAux = StringReplace(ASAux, "\\n\\n", sLineBreak, TReplaceFlags() << rfReplaceAll);
  ASAux = StringReplace(ASAux, "\\n", sLineBreak, TReplaceFlags() << rfReplaceAll);
  Memo1->Lines->Add(ASAux);
  o->Free();
  memo->Free();
}
catch (...)
{
  ShowMessage("Error accediendo a los servicios IA de HFocused");
}


Casimiro Notevi 20-02-2023 21:16:20

^\||/^\||/^\||/


La franja horaria es GMT +2. Ahora son las 19:09:58.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi