Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wolframalpha example #1

Merged
merged 4 commits into from
May 16, 2024
Merged

wolframalpha example #1

merged 4 commits into from
May 16, 2024

Conversation

gilcu3
Copy link
Contributor

@gilcu3 gilcu3 commented May 16, 2024

Added script to use wolframalpha as a function call. It depends on tungsten, which is a simple bash script that can query the engine.

By default, I am redirecting err to /dev/null as tungsten may output warnings (such as not using API_KEY), but still return the expected response.

Tested with: .role %functions% factor 12442515125122111

PS: as discussed in sigoden/aichat#507 (comment)

@sigoden
Copy link
Owner

sigoden commented May 16, 2024

Wolfram Alpha already provides API, why do you need a third-party tool? Moreover, this tool has not been updated for a long time and does not have many stars.

I haven't used Wolfram Alpha, but I asked it in gpt? Here is the reply:

#!/usr/bin/env bash
set -e

# @describe Takes in a query string and returns search results from Wolfram Alpha.
# Use it to answer user questions that require computation, detailed facts, data analysis, or complex queries.
# This ensures accurate and precise answers.
#
# @option --query! The query to search for.

main() {
  local query="${argc_args[--query]}"
  local appid=$(cat ~/.wolfram_appid 2>/dev/null)
  if [[ -z "${appid}" ]]; then
    echo "Error: Wolfram Alpha App ID not found."
    echo "Please create a file named '.wolfram_appid' in your home directory and paste your App ID into it."
    exit 1
  fi
  local url="http://api.wolframalpha.com/v2/query?input=${query// /%20}&appid=${appid}&output=json"
  curl -s "${url}" | jq -r '.queryresult.pods[].subpods[].plaintext'
}

eval "$(argc --argc-eval "$0" "$@")"

I feel like it can be used with a little modification. @gilcu3

Note: argc have # @env to define and validate environment variable.

# @env DDG_MAX_RESULTS=5 The max results to return.
# @option --query! The query to search for.
main() {
ddgr -n $DDG_MAX_RESULTS --json "$argc_query" | \
jq -r '. as $input |
reduce range(0; length) as $i ([]; . + [ $input[$i] | .index = $i ]) |
.[] | "### \(.index+1). \u001b]8;;\(.url)\u001b\\\(.title)\u001b]8;;\u001b\\\n\(.abstract)\n"
'
}

@gilcu3
Copy link
Contributor Author

gilcu3 commented May 16, 2024

Agree that probably this does not require external tools. The problem is that handling all cases could get much more complicated than what you show, and our script would basically reproduce what tungsten has already done.

@gilcu3
Copy link
Contributor Author

gilcu3 commented May 16, 2024

Just pushed another version with your suggestions. We lost a few features like colorized output, or the possibility of using it without an API key. Feel free to modify further to your liking, I think that will be way more efficient now that we have a base script.

@sigoden
Copy link
Owner

sigoden commented May 16, 2024

The function name should always be in the format <verb>_<noun>. @gilcu3

#!/usr/bin/env bash
set -e

# @describe Get an answer to a question using Wolfram Alpha. Input should the the query in English.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input should the the query in English.

Why did you specifically point it out? Is it really only possible to use English?

Copy link
Contributor Author

@gilcu3 gilcu3 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is the case, WolframAlpha natural language processing features are minimal as far I know.
PS: although the website now lists English, Spanish and Japanese

Copy link
Owner

@sigoden sigoden May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's best not to limit. Each words occupies tokens.

If users use other languages and feel that the effect is not good, they will not use it, and there is no need to point it out here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those lines there actually make it useful for people using other languages. They are supposed to make the AI translate the query before passing it to WolframAlpha. I think a few words is a good price to pay for better accessibility.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding of this sentence is that search_wolframalpha only supports English. If you input other languages, it cannot be processed. Bye bye.
This does not tell LLM to translate into English.

Remove this sentence here, add more comments on --query

-- # @option --query! The query to search for.
++ # @option --query! The query to search for. Preferably in English.

Copy link
Contributor Author

@gilcu3 gilcu3 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, I am misunderstanding something then. What exactly is the input on the LLM when using functions? I thought it was the @describe text, not the @option text.

PS: I just tested my intuition and it seems to be correct. The LLM (at least the latest gpt-4o) translated a query in hungarian tényező a szám 12141124125125125 into
> call search_wolframalpha --query 'factor the number 12141124125125125' Yes.
Inputting the hungarian directly to WolframAlpha (website) gives nonsensical results.

Without that sentence therefore the AI may not translate anything and miss results.

PS2: Thinking about it more carefully, maybe the @describe field is used in 2 contexts, and those two are hard to conflate. It is what the user sees when figuring out what a function does, but it is also what the AI sees when trying to do the same.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've tested it, I'll defer to your judgment. Go ahead and fix the grammar, and I'm ready to merge.

-- Input should the the query in English.
++ Input should be the query in English.

@sigoden sigoden merged commit 1a24e82 into sigoden:main May 16, 2024
1 check passed
@gilcu3 gilcu3 deleted the wolframalpha_example branch May 16, 2024 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants