In the newly released version 11.1, Mathematica introduces three new functions: WebSearch, WebImageSearch and TextTranslation that requires Service Credits. While I haven't found much information about it, there are several characteristics I notice.
It is a separated service from Cloud Credit, and they do not share credits. (It can be proved by evaluating
$ServiceCreditsAvailable
and$CloudCreditsAvailable
)Functions require Service Credits consumes a certain amount of credits on each evaluation locally(a fundamental difference with cloud credits).
WebSearch
andWebImageSearch
consume approximately 3 credits per run, whileTextTranslation
consumes 1 credit per run (at least).These functions do not cache the content, even if the queries are the same. (screenshot below)
When writing scripts with such functions, sometimes a bug could cause evaluating expression repeatedly unintentionally. With this possibility, service credits can be used up quickly without notice. With the trend of Wolfram Language integrating more highly automated informational functions with external services, I expect to see more functions of this kind in the future.
So I think having some overall control writing/executing scripts with paid functions are necessary. I thought of function Once
to cache the result, in case there are identical calls, credits will be saved. The code illustrating this idea looks like this:
saveMoney[fun_] :=
Module[{code = DownValues[fun]},
DownValues[fun] =
Replace[code, {(h : (WebSearch | WebImageSearch |
TextTranslation))[a___] :> Once[h[a]]}, Infinity]]
However, it requires manually applying this function on each symbol defined. And this function has no control when the content string for calls are different. It is also possible to limit the number of times those functions can execute during an evaluation, but I have not tried to implement this yet.
What are some more effective methods(code) to control the unintended usages of those functions?