sepia-docs icon indicating copy to clipboard operation
sepia-docs copied to clipboard

install java packages for smart-service

Open royrogermcfreely opened this issue 1 year ago • 5 comments

hello,

i am trying to create a smart-service with java.

for that i have to install the wolframalpha.jar from wolfram-alpha-java-binding

i copied the jar file to the folling folders: sepia-mesh-nodes/libs sepia-assist-server/libs sepia-teach-server/libs sepia-websocket-server-java/libs

but when i try to import the needed files i get the following error:

{` "result": "fail", "error": "ERROR: Class 'DynamicQuestionAnswering' - Compile errors: \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 20: package com.wolfram.alpha does not exist \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 21: package com.wolfram.alpha does not exist \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 22: package com.wolfram.alpha does not exist \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 23: package com.wolfram.alpha does not exist \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 24: package com.wolfram.alpha does not exist \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 25: package com.wolfram.alpha does not exist \n" `}

what i am doing wrong? do i have to import the file somewhere else?

sorry i am a total beginner ;)

/roy

royrogermcfreely avatar Sep 21 '22 14:09 royrogermcfreely

Hi roy,

the reason this does not work is because during start of the server the classpath is set to the specific .jar files listed inside the main jar manifest (theoretically you can unzip this file and modify it, but that's another story ^^).

There are a few options to work around this:

  1. Personally I'd prefer to load the jar file dynamically inside your smart-service with the SandboxClassLoader for more security, but I've not yet tested this myself. I'll try to optimize and document the procedure for the next release.

  2. Quick and dirty. Shutdown your SEPIA server then go to your SEPIA/sepia-assist-server/ folder and look for the run.sh. Replace the last line like this:

#nohup java -jar -Xms200m -Xmx200m $JAR_NAME --my &> log.out&
nohup java -cp "$JAR_NAME:libs/*" net.b07z.sepia.server.assist.server.Start --my &> log.out&

Then place your jar file inside SEPIA/sepia-assist-server/libs/ and it should theoretically be available after restart. The modified run command will load ALL jar-files from the libs folder instead of just the one mentioned in the original manifest.

Let me know if option 2 worked if you test it 😁

fquirin avatar Sep 22 '22 09:09 fquirin

after i change the lines in the run.sh file i cannot upload any service with the default code. just gives me just this error: { "result":"fail", "error":"ERROR: null"}

the log.out file gives no importend errors

royrogermcfreely avatar Sep 22 '22 12:09 royrogermcfreely

Hmm :-/ did you try any of the demo services? I'll try to do some tests later myself.

fquirin avatar Sep 22 '22 12:09 fquirin

sorry for the misunderstanding. i tried it with the demo services and they give me all the same error

royrogermcfreely avatar Sep 22 '22 12:09 royrogermcfreely

I did a quick test and noticed that the server failed to properly shutdown because I forgot to adjust the SEPIA/sepia-assist-server/shutdown.sh 🤦‍♂️. Can you please change the last line like this:

#pkill -f 'java -jar .*sepia-assist.*'
pkill -f 'java -cp .*sepia-assist.*'

Then shutdown the whole server again, restart and try again?

I was able to upload the HelloWorld demo service afterwards including import com.wolfram.alpha.WAEngine; in the header (with WolframAlpha-1.1.jar inside SEPIA/sepia-assist-server/libs) 🙂

[EDIT] I also noticed that the WolframAlpha-1.1.jar is 12 years old 😅 , I hope it is compatible with Java 11 and 8 🙈

fquirin avatar Sep 22 '22 14:09 fquirin

nice to hear its working for you but sadly i still get the same error when i try upload a demo service: { "result":"fail", "error":"ERROR: null"}

do i need to copy the files again or it doesnt matter if they are there from before?

btw i am on the newest version.

yeah noticed that too but couldnt find something newer^^

ps: saw on twitter you working on sepia with homeassistant :) i am looking forward for your work there

[Edit] i just saw the part of the "DynamicQuestionAnswering" service which handles the answering. there you wrote a example about a http call to web api.

now you can use wolfram alpha also via web call

so the api request is https://api.wolframalpha.com/v1/spoken?i=What+is+the+fastest+car+in+the+world%3F&appid=DEMO

with that it should be easy to get the answer from the question but when i just uncomment the part at line 166

	try{
				String qUrlParam = "?q=" + URLEncoder.encode(qRaw, "UTF-8");
				JSONObject response = Connectors.simpleJsonGet(
					"http://localhost:20731/my-service/" + qUrlParam
				);
				if (response != null && response.containsKey("answer")){
					questionResponse = JSON.getString(response, "answer");
				}
			}catch (Exception e){
				Debugger.println("DynamicQuestionAnswering - failed to call API - Error: " + e.getMessage(), 1);
			}

and try to upload the service with your code i get the following error:

{
  "result": "fail",
  "error": "ERROR: Class 'DynamicQuestionAnswering' - Compile errors: \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 167: cannot find symbol\n  symbol:   variable URLEncoder\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 168: cannot find symbol\n  symbol:   class JSONObject\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 168: cannot find symbol\n  symbol:   variable Connectors\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 172: cannot find symbol\n  symbol:   variable JSON\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 175: cannot find symbol\n  symbol:   variable Debugger\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \n"
}

royrogermcfreely avatar Sep 22 '22 15:09 royrogermcfreely

do i need to copy the files again or it doesnt matter if they are there from before?

The order doesn't matter as long as everything is set before you start the server. Maybe test with the "check" script before you start the server if the old one was closed properly.

saw on twitter you working on sepia with homeassistant :) i am looking forward for your work there

Supporting HA was a bit more complicated than I had hoped 😅 , but the basics should work well in the next release :-)

and try to upload the service with your code i get the following error: ...

There are a few imports missing when you uncomment the code lines:

import java.net.URLEncoder;
import org.json.simple.JSONObject;
import net.b07z.sepia.server.core.tools.Connectors;
import net.b07z.sepia.server.core.tools.Debugger;
import net.b07z.sepia.server.core.tools.JSON;

This should be it, I think. Did you restore the old run/shutdown command? Or did you get those errors with the same settings that triggered { "result":"fail", "error":"ERROR: null"} before? That error is really weird somehow 🤔 Which demo service was that?

fquirin avatar Sep 23 '22 17:09 fquirin

i stopped sepia, changed the lines in run.sh and shutdown.sh, placed the files in the lib folder and restartet the machine. nothing changed.

no the error just came with the changes in run.sh and shutdown.sh

when i start the checkscript after the shutdown script, i get this: `sepia 9454 9452 0 19:12 pts/0 00:00:00 grep java.sepia-assist.

and after the change and start i get:

sepia       9670       1  2 19:15 pts/0    00:00:04 java -cp sepia-assist-v2.6.2.jar:libs/* net.b07z.sepia.server.assist.server.Start --my
sepia      10007   10005  0 19:19 pts/0    00:00:00 grep java.*sepia-assist.*

about the http request: do i have to import something else to open a http request? i dont get a json, just a string with text so i tried this code:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://api.wolframalpha.com/v1/spoken?i=" + qUrlParam))
	.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());

but it fails with:

{
  "result": "fail",
  "error": "ERROR: Class 'DynamicQuestionAnswering' - Compile errors: \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 179: cannot find symbol\n  symbol:   class HttpClient\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 179: cannot find symbol\n  symbol:   variable HttpClient\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 180: cannot find symbol\n  symbol:   class HttpRequest\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 180: cannot find symbol\n  symbol:   variable HttpRequest\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 181: cannot find symbol\n  symbol:   variable URI\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 184: cannot find symbol\n  symbol:   class HttpResponse\n  location: class net.b07z.sepia.sdk.services.uid1007.DynamicQuestionAnswering \nstring:///net/b07z/sepia/sdk/services/uid1007/DynamicQuestionAnswering.java - Line 184: package HttpResponse does not exist \n"
}

royrogermcfreely avatar Sep 24 '22 17:09 royrogermcfreely

and after the change and start i get: ...

looks ok. I can't really explain where the error is coming from since I can't seem to be able to reproduce it 🤔 Is the rest of the server working without errors?

do i have to import something else to open a http request? i dont get a json, just a string with text so i tried this code: ...

There are many possible ways to do the HTTP request, I'd recommend to use this one from the "Connectors" package:

import java.net.URLEncoder;
import net.b07z.sepia.server.core.tools.Connectors;
import net.b07z.sepia.server.core.tools.Connectors.HttpClientResult;
import net.b07z.sepia.server.core.tools.Debugger;
...
try {
  String url = "https://api.wolframalpha.com/v1/spoken?i=" + URLEncoder.encode(qRaw, "UTF-8");
  String contentType = "text/plain";
  HttpClientResult result = Connectors.apacheHttpGET(url, contentType);
  if (result.statusCode == 200){
	  //the string content
	  Debugger.println("DynamicQuestionAnswering - API response: " + result.content, 3);
	  //you can still parse it if it's JSON:
	  //JSONObject json = JSON.parseStringOrFail(result.content);
  }else{
	  //error
	  Debugger.println("DynamicQuestionAnswering - failed to call API - Error: " + result.statusCode + " - " + result.statusLine, 1);
  }
}catch (Exception e){
  Debugger.println("DynamicQuestionAnswering - failed to call API - Error: " + e.getMessage(), 1);
}

If you need to change the request headers there is a version with headers map as well: apacheHttpGET(String url, Map<String, String> headers).

If you need more customizations you can check out the given implementation and look-up the imports in the class header.

fquirin avatar Sep 25 '22 10:09 fquirin