Hi there,
via RESTXQ I request a vectorstore. The response is an JSON-Object.
For example: With the following curl: curl https://vector.io/query-data \ -H "Authorization: Bearer ABcFMGhetc" \ -d '{ "data": "Du hast dem Menschen ein so freies, herrliches und üppiges Leben bestimmt", "topK": 2, "includeVectors": false, "includeMetadata": true }'
I'm getting this object:
{ "result" : [ { "id" : "ba02277", "score" : 0.9949091, "metadata" : {"genre":"ba","blatt":"1810-01","autor":"Kleist","titel":"Zoroaster.","link":"https://kleist-digital.de/etc%22,%22text%22:%22freies, herrliches und üppiges Leben bestimmt."} }, { "id" : "ba01711", "score" : 0.8891792, "metadata" : {"genre":"ba","blatt":"1810-29","autor":"Ompteda","titel":"Fragmente","link":"https://kleist-digital.de/etc%22,%22text%22:%22So mit dem Menschen im Moralischen."} } ] }
1. Is the request in my RESTXQ in the right form? http:send-request( <http:request method="post" href="{$prod:url}/query-data"> <http:header name="Authorization" value="Bearer {$prod:token}"/> <http:body media-type="application/json"> {{ "data": "{$query}", "topK": 20, "includeVectors": false, "includeMetadata": true }} </http:body> </http:request> )
2. I'm getting a full Object (but I dont know, if its the raw JSON-Object like above), but I am not able, to parse it, to get a list in html. In the documentation https://docs.basex.org/wiki/JSON_Module is an example: let $input := '{ "Title": "Drinks", "Author": [ "Jim Daniels", "Jack Beam" ] }' let $data := json:parse($input, map { 'format': 'xquery' }) return map:for-each($data, function($k, $v) { $k || ': ' || string-join($v, ', ') })
But my Object is more complex and I don't know, how to get two and more datasets with $k and $v out of the object. I want to have the following list: <div> <li> <ul> <li><span>Score: </span> <span>result score</span></li> <li><span>Blatt: </span> <span>result blatt</span></li> <li><span>Autor: </span> <span>result autor</span></li> <li><span>Titel: </span> <span>result titel</span></li> <li><span>Text: </span> <span>result text</span></li> <li><span>Link: </span> <span>result link</span></li> </ul> </li> <ul> <li></li> </ul> etc. </div>
Thanks for any help Günter
On 25/04/2024 21:58, Günter Dunz-Wolff wrote:
Hi there,
via RESTXQ I request a vectorstore. The response is an JSON-Object.
For example: With the following curl:
curl https://vector.io/query-data \
-H "Authorization: Bearer ABcFMGhetc" \
-d '{ "data": "Du hast dem Menschen ein so freies, herrliches und üppiges Leben bestimmt", "topK": 2, "includeVectors": false, "includeMetadata": true }'
I'm getting this object:
{
"result" : [ {
"id" : "ba02277",
"score" : 0.9949091,
"metadata" : {"genre":"ba","blatt":"1810-01","autor":"Kleist","titel":"Zoroaster.","link":"https://kleist-digital.de/etc%22,%22text%22:%22freies, herrliches und üppiges Leben bestimmt."}
}, {
"id" : "ba01711",
"score" : 0.8891792,
"metadata" : {"genre":"ba","blatt":"1810-29","autor":"Ompteda","titel":"Fragmente","link":"https://kleist-digital.de/etc%22,%22text%22:%22So mit dem Menschen im Moralischen."}
} ]
}
<div>
<li>
<ul>
<li><span>Score: </span> <span>result score</span></li>
<li><span>Blatt: </span> <span>result blatt</span></li>
<li><span>Autor: </span> <span>result autor</span></li>
<li><span>Titel: </span> <span>result titel</span></li>
<li><span>Text: </span> <span>result text</span></li>
<li><span>Link: </span> <span>result link</span></li>
</ul>
</li>
<ul>
<li></li>
</ul>
etc.
</div>
Parse the result (if it is not parsed and you have it as JSON string) with parse-json($result)) then run XQuery like
<div> { parse-json($result)?result?*!(let $score := ?score return ?metadata ! <ul>
<li><span>Score: </span> <span>{$score}</span></li>
<li><span>Blatt: </span> <span>{?blatt}</span></li>
<li><span>Autor: </span> <span>{?autor}</span></li>
<li><span>Titel: </span> <span>{?titel}</span></li>
<li><span>Text: </span> <span>{?text}</span></li>
<li><span>Link: </span> <span>{?link}</span></li>
</ul> ) } </div>
and it should give you something like
<div> <ul> <li> <span>Score: </span> <span>0.9949091</span> </li> <li> <span>Blatt: </span> <span>1810-01</span> </li> <li> <span>Autor: </span> <span>Kleist</span> </li> <li> <span>Titel: </span> <span>Zoroaster.</span> </li> <li> <span>Text: </span> <span>freies, herrliches und üppiges Leben bestimmt.</span> </li> <li> <span>Link: </span> <span>https://kleist-digital.de/etc</span> </li> </ul> <ul> <li> <span>Score: </span> <span>0.8891792</span> </li> <li> <span>Blatt: </span> <span>1810-29</span> </li> <li> <span>Autor: </span> <span>Ompteda</span> </li> <li> <span>Titel: </span> <span>Fragmente</span> </li> <li> <span>Text: </span> <span>So mit dem Menschen im Moralischen.</span> </li> <li> <span>Link: </span> <span>https://kleist-digital.de/etc</span> </li> </ul> </div>
It is not quite the result you showed but I have left out the outer `li` elements as an li inside a div doesn't make sense
Fiddle https://bxfiddle.cloud.basexgmbh.de/?share=%28%27query%21%27%3Cdiv%3EH%28H%3...
Hi Günter,
2. I'm getting a full Object (but I dont know, if its the raw JSON-Object
like above), but I am not able, to parse it, to get a list in html.
Could you share the result of your response with us (possibly shortened)?
It would be interesting to learn something about the type of the response body. What do you get if you inspect:type($response[2]) ?
let $data := json:parse($input, map { 'format': 'xquery' })
return map:for-each($data, function($k, $v) {
$k || ': ' || string-join($v, ', ')
})
If you use json:parse($input) without a specific format, you’ll get an XML representation of the JSON data, which is usually simpler to postprocess.
Hope this helps Christian
Hi Christian, hi Martin,
I checked the response-typ. It's "document-node()".
The structure is like this: <json type="object"> <result type="array"> <_ type="object"> <id>ba03177</id> <score type="number">0.83175087</score> <metadata type="object"> <genre>ba</genre> <blatt>1810-40</blatt> <autor>Heinrich von Kleist</autor> <titel>Die heilige Cäcilie</titel> <link> "https://kleist-digital.de/berliner-abendblaetter-etc" <text>Die Aebtissinn , die schon, in der Stunde der Mitternacht, durch einen Freund, von der Gefahr, etc</text> </metadata> <!--_--> etc.
I hope this helps. How to parse it, to get my proposed html-structure?
Thanks for further help.
Günter
Am 25.04.2024 um 23:08 schrieb Christian Grün christian.gruen@gmail.com:
Hi Günter,
- I'm getting a full Object (but I dont know, if its the raw JSON-Object like above), but I am not able, to parse it, to get a list in html.
Could you share the result of your response with us (possibly shortened)?
It would be interesting to learn something about the type of the response body. What do you get if you inspect:type($response[2]) ?
let $data := json:parse($input, map { 'format': 'xquery' }) return map:for-each($data, function($k, $v) { $k || ': ' || string-join($v, ', ') })
If you use json:parse($input) without a specific format, you’ll get an XML representation of the JSON data, which is usually simpler to postprocess.
Hope this helps Christian
Thanks. Here’s one way to do it:
let $body := document { <json type="object"> <result type="array"> <_ type="object"> <id>ba03177</id> <score type="number">0.83175087</score> <metadata type="object"> <genre>ba</genre> <blatt>1810-40</blatt> <autor>Heinrich von Kleist</autor> <titel>Die heilige Cäcilie</titel> <link>https://kleist-digital.de/...</link> <text>Die Aebtissinn, ...</text> </metadata> </_> </result> </json> } return <div> <ul>{ for $result in $body/json/result/_ return <ul>{ for $field in ('Score', 'Blatt', 'Autor', 'Titel', 'Text', 'Link') return <li> <span>{ $field }: </span> <span>{ data($result//*[name() = lower-case($field)]) }</span> </li> }</ul> }</ul> </div>
Another one is…
return <div> <ul>{ for $result in $body/json/result/_ return <ul>{ for $text in $result//text() let $field := name($text/..) return <li> <span>{ $field ! (upper-case(substring(., 1, 1)) || substring(., 2)) }: </span> <span>{ $text }</span> </li> }</ul> }</ul> </div>
…but of course you can also output only the relevant fields.
Hope this helps, Christian
Hi Christian,
now it's working. Thanks a lot!!
Regards Günter
Am 26.04.2024 um 11:21 schrieb Christian Grün christian.gruen@gmail.com:
Thanks. Here’s one way to do it:
let $body := document {
<json type="object"> <result type="array"> <_ type="object"> <id>ba03177</id> <score type="number">0.83175087</score> <metadata type="object"> <genre>ba</genre> <blatt>1810-40</blatt> <autor>Heinrich von Kleist</autor> <titel>Die heilige Cäcilie</titel> <link>https://kleist-digital.de/.. <https://kleist-digital.de/>.</link> <text>Die Aebtissinn, ...</text> </metadata> </_> </result> </json> } return <div> <ul>{ for $result in $body/json/result/_ return <ul>{ for $field in ('Score', 'Blatt', 'Autor', 'Titel', 'Text', 'Link') return <li> <span>{ $field }: </span> <span>{ data($result//*[name() = lower-case($field)]) }</span> </li> }</ul> }</ul> </div>
Another one is…
return <div>
<ul>{ for $result in $body/json/result/_ return <ul>{ for $text in $result//text() let $field := name($text/..) return <li> <span>{ $field ! (upper-case(substring(., 1, 1)) || substring(., 2)) }: </span> <span>{ $text }</span> </li> }</ul> }</ul> </div>
…but of course you can also output only the relevant fields.
Hope this helps, Christian
basex-talk@mailman.uni-konstanz.de