This commit is contained in:
2023-11-22 07:58:29 +00:00
parent 99281e8d3e
commit d374ed245e
4 changed files with 122 additions and 39 deletions

99
src/llmfunction.jl Normal file
View File

@@ -0,0 +1,99 @@
module llmfunction
export wikisearch
using HTTP, JSON3
#------------------------------------------------------------------------------------------------100
"""
Search wikipedia.
Args:
query (string): The query to search for
Returns:
string: The search result text from wikipedia
```jldoctest
julia> using HTTP, JSON3
julia> result = wikisearch("AMD")
"Advanced Micro Devices, Inc., commonly abbreviated as AMD, is an ..."
```
"""
function wikisearch(phrase::T) where {T<:AbstractString}
url = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=$(replace(phrase, " " => "%20"))&exintro=1&explaintext=1"
response = HTTP.get(url)
json_data = JSON3.read(String(response.body))
page_id = first(keys(json_data["query"]["pages"]))
if page_id == "-1"
return "Sorry, I couldn't find any Wikipedia page for the given phrase."
end
result = nothing
try
result = json_data["query"]["pages"][page_id]["extract"]
catch
result = "No info available."
end
if result == ""
result = "No info available."
end
return result
end
end # end module