I am trying to query a large language model from VS code using WolframEngine.
However, ImportSting
with JSON
options does not work in extract the desired output string of the LLM response. So I use the following code instead:
input = "Two locations, A and B, are 4000 meters apart. At 7:20 AM, Person X begins walking from location A towards location B at a constant speed of 60 meters per minute, while Person Y starts from location B towards location A at a constant speed of 260 meters per minute.After Person Y reaches location A, he immediately turn around and walk back towards location B at the same speed of 260 meters per minute. Eventually, Person Y catches up with Person X, and together, they walk at the same speed of 175 meters per minute until they reach location B. The question asks: How many minutes after will it be 8:00 AM when both Person X and Person Y reach location B together?";response = URLRead[HTTPRequest["https://api.siliconflow.cn/v1/chat/completions", <|"Method" -> "POST", "Headers" -> <|"Authorization" -> "Bearer " <> "sk-sbbecyooldruiqctrgwggqfksqnaxtkzugvtaggzwuvupxoo", "Content-Type" -> "application/json"|>, "Body" -> ExportString[<|"model" -> "Qwen/Qwen2.5-7B-Instruct", "messages" -> {<|"role" -> "user", "content" -> input|>}, "max_tokens" -> 1000, "temperature" -> 0.6|>, "JSON"]|>]]; StringCases[response["Body"], RegularExpression["\"content\":\s*\"(.*?)\""] -> "$1"] // StringReplace[#, "\n" -> ""] &//Print
I noticed ImportString with JSON option does not work. Then how do you extract the desired output string with or without using regular expression rules?