Jump to content

Including a personal assistant

Recommended Posts

  • Moderators

This afternoon I came across API.ai, a service that allows developers to build natural language processing artificial intelligence system which can be trained up with custom functionality. 


I had thought about pursuing something like this and briefly looked at Watson/Bluemix, however this service (API.ai) seemed right in my alley.

So I signed up (freemium model).


As it turns out the service comes pre-loaded with a couple of general knowledge domains so if you don't add your own knowledge base at least there is something.


Their API is pretty easy and below is the code for a simple assistant : an input memo for the question, a button to activate the assistant and an output memo for the results.


I must say this blew me away. See below a couple of questions and answers straight out of the box :


==> who is barack obama
Barack Hussein Obama II is an American politician currently serving as the 44th President of the United States, the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he served as president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney and taught constitutional law at University of Chicago Law School between 1992 and 2004. He served three terms representing the 13th District in the Illinois Senate from 1997 to 2004, running unsuccessfully for the United States House of Representatives in 2000 against Bobby Rush.
==> will it rain tomorrow in Syney
Doubtful. Tomorrow will be mostly cloudy, and temperatures will range from 15 degrees and 37 degrees in Sydney.
==> best restaurants in Amsterdam
Restaurant De Kas, Restaurant Café in de Waag, Café Restaurant Amsterdam, Restaurant RED, Moeders This is what I was able to find: Restaurant De Kas . Rating: 4.5 . Restaurant Café in de Waag . Rating: 4.2 . Café Restaurant Amsterdam . Rating: 4.1 . Restaurant RED . Rating: 4.6 . Moeders . Rating: 4.2 
==> how are you
Couldn't be better.
==> tell me about wwII
World War II, also known as the Second World War, was a global war that lasted from 1939 to 1945, though related conflicts began earlier. It involved the vast majority of the world's nations—including all of the great powers—eventually forming two opposing military alliances: the Allies and the Axis. It was the most widespread war in history, and directly involved more than 100 million people from over 30 countries. In a state of "total war", the major participants threw their entire economic, industrial, and scientific capabilities behind the war effort, erasing the distinction between civilian and military resources. Marked by mass deaths of civilians, including the Holocaust and the strategic bombing of industrial and population centres, it resulted in an estimated 50 million to 85 million fatalities. These made World War II the deadliest conflict in human history.
==> translate chair in japanese
Here is the Japanese translation of "chair": 椅子
The form code :
(an input memo for the question, a button to activate the assistant and an output memo for the results)

unit Form1;
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Inet,
  SmartCL.Controls.Button, system.types, SmartCL.Controls.Memo;
  TForm1 = class(TW3Form)
    procedure W3Button1Click(Sender: TObject);
    {$I 'Form1:intf'}
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
    procedure AskAssistant(url, accesstoken, subscriptionkey: string);
    procedure GetAnswer(Sender: TW3HttpRequest);
    FHTTP: Tw3HttpRequest;
    smscursor: Variant;
{ TForm1 }
procedure TForm1.W3Button1Click(Sender: TObject);
  accessToken = <your token>;
  subscriptionKey = <your key>;
  baseUrl = "https://api.api.ai/v1/";
procedure TForm1.InitializeForm;
  // this is a good place to initialize components
procedure TForm1.InitializeObject;
  {$I 'Form1:impl'}
procedure TForm1.Resize;
procedure TForm1.AskAssistant(url, accesstoken, subscriptionkey: string);
  V: Variant;
  FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := GetAnswer;
  FHttp.open("POST",url + "query/");
  FHttp.setRequestHeader("Content-type","application/json; charset=utf-8");
  FHttp.setRequestHeader("Authorization","Bearer " + accessToken);
  V := TVariant.CreateObject;
  v.q := W3Memo1.Text;   // 'who is Albert Einstein';
  v.lang := 'en';
procedure TForm1.GetAnswer(Sender: TW3HttpRequest);
  smscursor := JSON.parse(Sender.ResponseText);
//  ShowMessage(smscursor.result.speech);
//  ShowMessage(sender.ResponseText);
  W3Memo2.Text := smscursor.result.speech;
  Forms.RegisterForm({$I %FILE%}, TForm1);

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...