Files
ChatAgent/src/llmfunction.jl
2024-01-13 04:37:05 +00:00

219 lines
6.4 KiB
Julia

module llmfunction
export wikisearch, winestock
using HTTP, JSON3, URIs
using GeneralUtils
using ..type, ..utils
#------------------------------------------------------------------------------------------------100
"""
Search wikipedia.
Arguments:
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(a::agentReflex, phrase::T) where {T<:AbstractString}
phrase = phrase[1] == " " ? phrase[2:end] : phrase
# prepare input phrase
if occursin("\"", phrase)
phrase = GeneralUtils.getStringBetweenCharacters(phrase, "\"", "\"")
end
phrase = replace(phrase, "\n"=>"")
url = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=extracts&titles=$(replace(phrase, " " => "%20"))&exintro=1&explaintext=1"
@show url
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"]
wiki = result
@show wiki
catch
result = "No info available for your search query."
end
# if result == ""
# result = "No info available for your search query."
# else
# result = makeSummary(a, result)
# end
return result
end
function winestock(a::agentReflex, phrase::T) where {T<:AbstractString}
result =
"""
I found the following wines
{
1: {
Chateau: Louis Latou,
name: Corton-Charlamagne,
grape variety: chardonnay,
year: 2014,
price: 55 USD,
stock ID: ws-114,
Description: Corton-Charlemagne 2018 is a powerful, complex wine. Its nose is intense, with notes of white stone fruits such as white peach, fresh hazelnut, vanilla, and almond paste. The wine is full-bodied for the palate, and the vanilla is complemented by aromas of fresh almond and lime blossom. The experience ends with a very fine aromatic aftertaste that has subtle saline notes.,
},
2: {
Chateau: Beaucastel,
name: Malbec Argentino,
grape variety: riesling,
year: 2016,
price: 39 USD,
stock ID: ed-23,
Description: The quintessence of Château de Beaucastel, Hommage à Jacques Perrin delights us every year, and the 2019 vintage is no exception. To the eye it offers a splendid deep red color, verging on black. Full of power and supremely elegant, the nose is of magnificent aromatic complexity with notes of black fruit and spices that offer all the characteristic expression of Mourvèdre. Perfectly balanced by an incredible freshness, the mouth is eminently elegant with intense and complex aromas of great subtlety, a full, refined texture, subtle tannins of great finesse, and infinite length. A great classic Hommage à Jacques Perrin.,
}
}
"""
return result
end
"""
Arguments:
Return:
Example:
```jldoctest
julia> using ChatAgent, CommUtils
julia> agent = ChatAgent.agentReflex("Jene")
julia> shorttermMemory = OrderedDict{String, Any}(
"user" => "What's the latest AMD GPU?",
"Plan 1:" => " To answer this question, I will need to search for the latest AMD GPU using the wikisearch tool.\n",
"Act 1:" => " wikisearch\n",
"Actinput 1:" => " amd gpu latest\n",
"Obs 1:" => "No info available for your search query.",
"Act 2:" => " wikisearch\n",
"Actinput 2:" => " amd graphics card latest\n",
"Obs 2:" => "No info available for your search query.")
julia> guideline = "\nEvaluation Guideline:\n1. Check if the user's question has been understood correctly.\n2. Evaluate the tasks taken to provide the information requested by the user.\n3. Assess whether the correct tools were used for the task.\n4. Determine if the user's request was successfully fulfilled.\n5. Identify any potential improvements or alternative approaches that could be used in the future.\n\nThe response should include:\n1. A clear understanding of the user's question.\n2. The tasks taken to provide the information requested by the user.\n3. An evaluation of whether the correct tools were used for the task.\n4. A confirmation or explanation if the user's request was successfully fulfilled.\n5. Any potential improvements or alternative approaches that could be used in the future."
julia> score = grading(agent, guideline, shorttermMemory)
```
"""
function winestockDB(a::agentReflex, query::T) where {T<:AbstractString}
@show query
prompt =
"""
<|system|>
<About yourself>
Your are a helpful assistant.
</About yourself>
<You have the following wine classification info>
Intensity level:
intensity = 1, light bodied
intensity = 2, light-medium bodied
intensity = 3, medium bodied
intensity = 4, medium-full bodied
intensity = 5, full bodied
Sweetness level:
sweetness = 1, dry
sweetness = 2, off-dry
sweetness = 3, semi-sweet
sweetness = 4, sweet
sweetness = 5, very sweet
Tannin level:
tannin = 1, low tannin
tannin = 2, low-medium tannin
tannin = 3, medium tannin
tannin = 4, medium-high tannin
tannin = 5, high tannin
Acidity level:
acidity = 1, low acidity
acidity = 2, low-medium acidity
acidity = 3, medium acidity
acidity = 4, medium-high acidity
acidity = 5, high acidity
</You have the following wine classification info>
<Your job>
Write SQL command using data from query.
</Your job>
<Example 1>
query: "{\"winestock\": {\"wine type\": \"white\", \"wine characteristics\": \"full-bodied | off-dry | low acidity | medium tannin\", \"price\": {\"max\": \"50\"}}}"
assistant: SELECT * FROM White WHERE intensity = 5 AND sweetness = 2 AND acidity = 1 AND tannin = 3 AND price <= 50;
</Example 1>
</s>
<|query|>
query: $query
</s>
<|assistant|>
"""
response = sendReceivePrompt(a, prompt, max_tokens=1024, temperature=0.4,
stopword=["/n/n", "END", "End", "Obs", "<|", "</"])
error("winestockDB done")
return result
end
end # end module