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

View File

@@ -10,6 +10,9 @@ module ChatAgent
include("type.jl")
using .type
include("llmfunction.jl")
using .llmfunction
include("interface.jl")
using .interface

View File

@@ -1037,40 +1037,21 @@ function chunktext(text::T, headers) where {T<:AbstractString}
return result
end
"""
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

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

View File

@@ -100,13 +100,6 @@ function agentReflex(
Begin!""",
),
tools::Dict=Dict(
:wikisearch=>Dict(
:name => "wikisearch",
:description => "Useful for when you need to search an encyclopedia",
:input => "Input is keywords and not a question.",
:output => "",
:func => wikisearch, # put function here
),
:chatbox=>Dict(
:name => "chatbox",
:description => "Useful for when you need to ask a customer for more context.",
@@ -114,6 +107,13 @@ function agentReflex(
:output => "" ,
:func => nothing,
),
# :wikisearch=>Dict(
# :name => "wikisearch",
# :description => "Useful for when you need to search an encyclopedia",
# :input => "Input is keywords and not a question.",
# :output => "",
# :func => wikisearch, # put function here
# ),
# :wineStock=>Dict(
# :name => "wineStock",
# :description => "useful for when you need to search for wine by your description, price, name or ID.",
@@ -153,7 +153,7 @@ function agentReflex(
newAgent.role = role
newAgent.roles = roles
newAgent.thinkingFormat = thinkingFormat
newAgent.name = agentName
newAgent.agentName = agentName
return newAgent
end