add reflection related function

This commit is contained in:
2023-11-26 05:17:00 +00:00
parent c4a823d46e
commit 4ff3e7c15b
2 changed files with 250 additions and 11 deletions

View File

@@ -2,7 +2,8 @@ module interface
export agentReact, agentReflex,
addNewMessage, clearMessage, removeLatestMsg, conversation
addNewMessage, clearMessage, removeLatestMsg, conversation, writeEvaluationGuidlines,
grading, analyze, selfReflext
using JSON3, DataStructures, Dates, UUIDs, HTTP
using CommUtils, GeneralUtils
@@ -34,9 +35,12 @@ using ..type, ..utils
#------------------------------------------------------------------------------------------------100
"""
add new message to agent
# Example
""" Add new message to agent.
Args:
Return:
```jldoctest
julia> addNewMessage(agent1, "user", "Where should I go to buy snacks")
````
@@ -648,6 +652,8 @@ function work(a::agentReflex, usermsg::String)
end
end
"""
Actor function.
@@ -707,10 +713,237 @@ function actor(a::agentReflex, plan::T) where {T<:AbstractString}
return actorState, msgToUser
end
""" Write evaluation guideline.
Args:
a, one of ChatAgent's agent.
shorttermMemory, a short term memory that logs what happened.
Return:
An evaluation guideline used to guage AI's work.
# Example
```jldoctest
julia> using ChatAgent, CommUtils
julia> agent = ChatAgent.agentReflex("Jene")
julia> shorttermMemory =
"
user: What's AMD latest product?
assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD.
1. Search for \"AMD latest product\" using wikisearch tool.
2. Identify the most recent product release mentioned in the search results.
3. Provide the user with the name of the latest product.
Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic.
Act 1: wikisearch
ActInput 1: \"AMD latest product\"
Obs 1: No info available.
"
julia> evaluationGuideLine = ChatAgent.writeEvaluationRules(agent, shorttermMemory)
```
"""
Evaluator
function writeEvaluationGuidlines(a::agentReflex, shorttermMemory::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer.
wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question.
Your work:
$shorttermMemory
Your job are:
1. Write an evaluation guideline for your work in order to be able to evaluate your respond.
2. What the respond should be?
<|im_end|>
"""
respond = sendReceivePrompt(a, prompt)
return respond
end
""" Determine a score out of 10 according to evaluation guideline.
Args:
a, one of ChatAgent's agent.
guidelines, an evaluation guideline.
shorttermMemory, a short term memory that logs what happened.
Return:
A score out of 10 based on guideline.
# Example
```jldoctest
julia> using ChatAgent, CommUtils
julia> agent = ChatAgent.agentReflex("Jene")
julia> shorttermMemory =
"
user: What's AMD latest product?
assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD.
1. Search for \"AMD latest product\" using wikisearch tool.
2. Identify the most recent product release mentioned in the search results.
3. Provide the user with the name of the latest product.
Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic.
Act 1: wikisearch
ActInput 1: \"AMD latest product\"
Obs 1: No info available."
julia> guideline = "\nEvaluation Guideline:\n1. Check if the user's question has been understood correctly.\n2. Evaluate the steps 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 respond should include:\n1. A clear understanding of the user's question.\n2. The steps 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)
2
```
"""
# function evaluator(a::agentReflex, plan::T)
function grading(a, guidelines::T, shorttermMemory::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer.
wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question.
Evaluation guidelines:
$guidelines
Your work:
$shorttermMemory
You job are:
1. Evaluate your work using the evaluation guidelines.
2. Give yourself a score out of 10 for your work.
Use the following format to answer: Score {}/10.
<|im_end|>
"""
respond = sendReceivePrompt(a, prompt)
score = parse(Int, respond[end-4:end-3])
return score
end
""" Analize work.
Args:
a, one of ChatAgent's agent.
Return:
A report of analized work.
# Example
```jldoctest
julia> using ChatAgent, CommUtils
julia> agent = ChatAgent.agentReflex("Jene")
julia> shorttermMemory =
"
user: What's AMD latest product?
assistant: Plan 1: To provide the user with information about AMD's latest product, I will search for the most recent product release from AMD.
1. Search for \"AMD latest product\" using wikisearch tool.
2. Identify the most recent product release mentioned in the search results.
3. Provide the user with the name of the latest product.
Thought 1: The user wants to know about the latest AMD products, so I should use the wikisearch tool to find information on this topic.
Act 1: wikisearch
ActInput 1: \"AMD latest product\"
Obs 1: No info available."
julia> report = analyze(agent, shorttermMemory)
```
"""
function analyze(a, shorttermMemory::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer.
wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question.
Your work:
$shorttermMemory
Use the following steps to analize your work in detail.
1. What happened?
2. List all cause and effect relationships.
3. Analyze each relationship to figure it out why it behaved that way.
4. What could you do to improve the respond?
<|im_end|>
"""
respond = sendReceivePrompt(a, prompt)
return respond
end
""" Write a lesson drawn from evaluation.
Args:
a, one of ChatAgent's agent.
report, a report resulted from analyzing shorttermMemory
Return:
A lesson.
# Example
```jldoctest
julia> using ChatAgent, CommUtils
julia> agent = ChatAgent.agentReflex("Jene")
julia> report =
"What happened: I tried to search for AMD's latest product using the wikisearch tool,
but no information was available in the search results.
Cause and effect relationships:
1. Searching \"AMD latest product\" -> No info available.
2. Searching \"most recent product release\" -> No info available.
3. Searching \"latest product\" -> No info available.
Analysis of each relationship:
1. The search for \"AMD latest product\" did not provide any information because the wikisearch tool could not find relevant results for that query.
2. The search for \"most recent product release\" also did not yield any results, indicating that there might be no recent product releases available or that the information is not accessible through the wikisearch tool.
3. The search for \"latest product\" similarly resulted in no information being found, suggesting that either the latest product is not listed on the encyclopedia or it is not easily identifiable using the wikisearch tool.
Improvements: To improve the response, I could try searching for AMD's products on a different
source or search engine to find the most recent product release. Additionally, I could ask
the user for more context or clarify their question to better understand what they are
looking for."
julia> lesson = analyze(agent, report)
```
"""
function selfReflext(a, report::T) where {T<:AbstractString}
prompt =
"""
<|im_start|>system
You have access to the following tools:
chatbox: Useful for when you need to ask a customer for more context. Input should be a conversation to customer.
wikisearch: Useful for when you need to search an encyclopedia Input is keywords and not a question.
Your report:
$report
What lesson could be drawn from your report?.
<|im_end|>
"""
respond = sendReceivePrompt(a, prompt)
return respond
end
@@ -719,8 +952,6 @@ end
# end

View File

@@ -47,8 +47,16 @@ abstract type agent end
end
function agentReflex(
agentName::String,
mqttClientSpec::NamedTuple;
agentName::String;
mqttClientSpec::NamedTuple=(
clientName= "someclient", # name of this client
clientID= "$(uuid4())",
broker= "mqtt.yiem.ai",
pubtopic= (imgAI="img/api/v0.0.1/gpu/request",
txtAI="txt/api/v0.1.0/gpu/request"),
subtopic= (imgAI="agent/api/v0.1.0/img/respond",
txtAI="agent/api/v0.1.0/txt/respond"),
keepalive= 30,),
role::Symbol=:assistant,
roles::Dict=Dict(
:assistant =>
@@ -133,7 +141,7 @@ function agentReflex(
:to=> "llmAI",
:requestrespond=> "request",
:sendto=> "", # destination topic
:replyTo=> "chatbothub/llm/respond", # requester ask responder to send reply to this topic
:replyTo=> "agent/api/v0.1.0/txt/respond", # requester ask responder to send reply to this topic
:repondToMsgId=> "", # responder is responding to this msg id
:taskstatus=> "", # "complete", "fail", "waiting" or other status
:timestamp=> Dates.now(),