AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

having trouble connecting from Prolog
 
 

I’m trying to connect chatscript to a SWI-Prolog client. 

Running the server as
C:\docs\chatscript\chatscript.exe port=4050


and querying with

:-module(test_chatscript_connection, [talk/2]).
/** <module> Test routine for chatscript connection
*
*/
:- use_module(library(http/http_client)).
:- use_module(library(http/http_open)).
:- use_module(library(http/http_header)).

talk(User, Message) :-
format(string(S), ‘~w\x00\x00\~w\x00’, [User, Message]),
string_codes(S, Codes), % after this line Codes is [98, 105, 108, 108, 121, 0, 0, 0]
writeq(Codes),
tcp_connect(localhost:4050, StreamPair, []),
stream_pair(StreamPair, Read, Write),
write(Write, S),
read_string(Read, _, Reply),  % hangs waiting for some response
writeln(Reply),
close(StreamPair).


?-  talk(annie, ‘’).

I’m baffled - obviously I’m violating the protocol somehow, but I’m not sure how.

I open a raw socket , send the user name and 3 nuls and wait for reply… and nothing comes

Am I misunderstanding the protocol?  It looks from the php sample SIMPLE\index.php that this is all that needs done.  Should I not be expecting a reply the first time?

 

 
  [ # 1 ]

So,  first try using the SERVERBATCH FILES/localclient.bat file to see if that can connect to your server.

 

 
  [ # 2 ]

the file will have to be modified to use the port you said.  Or maybe you should try your server on port 1024 first

 

 
  [ # 3 ]

I would humbly suggest trying first to use port 1024, then change the port to 4050 if that is successful. Testing in stages may take longer, but it’s more certain to isolate problems that might otherwise be more difficult to determine. For example, if the first step works (starting the CS server on port 1024), but fails after the port number is changed, then it’s most likely a port allocation problem (another app is using that port), or traffic denial issue (a firewall has blocked that port).

 

 
  [ # 4 ]

yes, I can connect with the SERVERBATCH file.

I doubt it’s a firewall issue or a problem with changing the port (I did check on 1024), as I’m connecting to the socket and writing my data successfully. I’m just not getting anythign back.

 

 
  [ # 5 ]

Maybe readstring hangs because your readstring requires a newline in the response.  Try adding
\n to the output section of your rules and see what happens.

 

 
  [ # 6 ]

well, it’s hanging waiting for some input - changing to read_pending_input makes it ‘succeed’, and gives this
(I’m changing the user name each time) in the console:

tart: user:hobart bot: ip:127.0.0.1 () 0 ==> Would you prefer to focus on the o
ter world or on your own inner world?  When:Apr17 11:47:23 47ms Version:5.21 Bu
ld0:Mar29’15-21:13:10 Build1:Mar29’15-21:13:18

But I’m not seeing the string ‘Would you prefer to focus on the o
ter world or on your own inner world?’

So at this point it’s something on my end, not a protocol issue I’d guess.

It does seem strange that it’s not sending after it realizes it should.

 

 
  [ # 7 ]

given that you get the results in the log, and that the client batch file communicates correctly to the server, then it must be a question about how prolog is functioning (and my comment on readstring still perhaps applies)

 

 
  [ # 8 ]

yup. Thanks Bruce. I’m just baffled.

 

 
  [ # 9 ]

Understandable. I still focus on ReadString as that isn’t what I’d expect to read the output from CS… eg what happens when the output consists of multiple strings? What happens if there is no end of line marker?  Are there any other api calls you could make instead?

 

 
  [ # 10 ]

apparently some buffering issue. Adding a flush fixed it.
Thanks for the help. Here’s the working version in case somebody following needs it:

talk(User, Message) :-
format(string(S), ‘~w\x00\x00\~w\x00’, [User, Message]),
string_codes(S, Codes),
writeq(Codes),
nl,
tcp_connect(localhost:4050, StreamPair, []),
stream_pair(StreamPair, Read, Write),
write(Write, S),
flush_output(Write),
% read_pending_input(Read, Codes1, []),
% peek_string(Read, 1, Reply),
read_stream_to_codes(Read, Codes1),
string_codes(Reply, Codes1),
writeln(Reply),
close(StreamPair).

 

 
  [ # 11 ]

YEAH!  congratulations!

 

 
  [ # 12 ]

FWIW we’re using ChatScript to drive NPC interaction in SWI-Prolog’s team entry in Ludum Dare 32 game jam.

The chatbots are going to be the central way you interact with the game.

 

 
  [ # 13 ]

Also, by end of week or so, this code should be a ‘pack’ for SWI-Prolog, so anybody can install it by just querying pack_install(plchatscript).

 

 
  [ # 14 ]

very cool!!! 

btw - if you want to simplify the interaction and cut out the need for a CS sever over tcp you might check out my swig/chatscript work - I just checked and there is http://pwig.sourceforge.net - a swig extension to support prolog.  This would allow you to create a library out of chat script and access it directly from prolog.  smile  I’m doing that for C# so I can embed chat script as a library in a unity3d game.

http://vinterstitial.blogspot.com/2014/05/a-swig-of-chatbot-making-unity-chatter.html

 

 
  [ # 15 ]

well, the existing system has a shorter toolchain length.

Could have directly called chatscript via ffi but the server route seemed more robust.

For users, it’s as simple as:

pack_install(plchatscript).
start the chatscript server with a port #

and start making calls

 

 

 
  login or register to react
‹‹ Connecting to postgres from CS      r: ››