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

Add agent feedback policy #404

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,849 changes: 1,414 additions & 1,435 deletions src/poetry.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/sherpa_ai/actions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def __str__(self):

return json.dumps(tool_desc, indent=4)

def __repr__(self):
return self.__str__()


class BaseRetrievalAction(BaseAction, ABC):
resources: list[ActionResource] = Field(default_factory=list)
Expand Down
3 changes: 0 additions & 3 deletions src/sherpa_ai/memory/belief.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ def get_context(self, token_counter: Callable[[str], int], max_tokens=4000):
if token_counter(context) > max_tokens:
break

if context == "":
exit(1)

return context

def get_internal_history(
Expand Down
3 changes: 1 addition & 2 deletions src/sherpa_ai/output_parsers/citation_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from sherpa_ai.output_parsers.base import BaseOutputProcessor
from sherpa_ai.output_parsers.validation_result import ValidationResult


# download the punkt tokenizer. This is necessary for the sent_tokenize in NLTK.
# The download will only happen once and the result will be cached.
nltk.download("punkt")
nltk.download("punkt_tab")


class CitationValidation(BaseOutputProcessor):
Expand Down
19 changes: 17 additions & 2 deletions src/sherpa_ai/policies/agent_feedback_policy.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from loguru import logger

from sherpa_ai.agents.base import BaseAgent
from sherpa_ai.events import Event, EventType
from sherpa_ai.memory import Belief
from sherpa_ai.policies import ReactPolicy

AGENT_FEEDBACK_DESCRIPTION = """You are an intelligent assistant helping the user to complete their task. You have the following task to complete:
{task}

Context of you work is as follows:
{context}

You have the following options to continue completing the this task:

{options}

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we don’t have any context for the question, how can the generated question be useful? For example, if I ask ‘What is the biggest?’ the question should specify ‘biggest what?’ or something similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Eyobyb I added the current task and context to the prompt. Could you check with the test case you have and also commit your test case to the test suite?

Use polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise.
Use polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise.

If you think the context is not enough to choose an action. Ask the user for more information.
"""


Expand All @@ -26,12 +35,18 @@ class Config:

def select_action(self, belief: Belief, **kwargs):
actions = belief.actions

task = belief.current_task.content
context = belief.get_context(self.llm.get_num_tokens)
options = "\n".join(
[f"{i+1}. {action.name}" for i, action in enumerate(actions)]
)

agent_feedback_prompt = self.agent_feedback_description.format(options=options)
agent_feedback_prompt = self.agent_feedback_description.format(
task=task, context=context, options=options
)
question = self.llm.predict(agent_feedback_prompt)
logger.info(f"Question to the user: {question}")
self.agent.shared_memory.add_event(Event(EventType.task, "Agent", question))
result = self.agent.run()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\n\\nContext of you work is as follows:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "To clarify, are you asking about the size of Jupiter, Mars, and Earth in terms of diameter, volume, or mass? Let me know how you'd like to proceed!", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest?, Jupyter, Mars or Earth?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\n\\n\\n**History of Previous Actions**:\\n\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"What is the biggest planet Jupiter Mars or Earth?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\n\\nContext of you work is as follows:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "Would you like me to perform a Google search to find out which is the biggest: Jupiter, Mars, or Earth?", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest?, Jupyter, Mars or Earth?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\nGoogle search the question\\n\\n\\n**History of Previous Actions**:\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"What is the biggest planet Jupiter Mars or Earth?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\n\\nContext of you work is as follows:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\nGoogle search the question\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "Would you like me to perform a Google search to find out which is the biggest: Jupiter, Mars, or Earth?", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest?, Jupyter, Mars or Earth?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\nGoogle search the question\\nGoogle search the question\\n\\n\\n**History of Previous Actions**:\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"What is the biggest planet Jupiter Mars or Earth?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are a **question answering assistant** who solves user questions and offers a detailed solution.\\n\\nYour name is QA Agent.\\n\\nContext: What is the biggest?, Jupyter, Mars or Earth?\\nGoogle search the question\\nGoogle search the question\\nGoogle search the question\\n\\n\\nAction - Result History:\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'What is the biggest planet Jupiter Mars or Earth?'}\\nJupiter is the biggest planet\\n\\nGiven the context and the action-result history, please complete the task mentioned. Include any links you used from the context and history in the result.\\nTask: What is the biggest?, Jupyter, Mars or Earth?\\nResult:\\n", "agent": "human"}], "output": "The biggest among Jupiter, Mars, and Earth is **Jupiter**. \\n\\nJupiter is the largest planet in our solar system, significantly larger than both Mars and Earth. To provide some context:\\n\\n- **Jupiter** has a diameter of about 86,881 miles (139,822 kilometers) and is primarily composed of gas, making it a gas giant.\\n- **Earth** has a diameter of about 7,918 miles (12,742 kilometers).\\n- **Mars** has a diameter of about 4,221 miles (6,779 kilometers).\\n\\nThus, in terms of size, Jupiter is the clear winner. \\n\\nFor further information, you can refer to the following links:\\n- [NASA - Jupiter](https://solarsystem.nasa.gov/planets/jupiter/overview/)\\n- [NASA - Mars](https://solarsystem.nasa.gov/planets/mars/overview/)\\n- [NASA - Earth](https://solarsystem.nasa.gov/planets/earth/overview/) \\n\\nIf you have any more questions or need further clarification, feel free to ask!", "llm_name": "gpt-4o-mini"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest ?\\n\\nContext of you work is as follows:\\nWhat is the biggest ?\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "Could you please provide more details about what you're looking for? What specific topic or item do you want to know the biggest of?", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest ?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\n**History of Previous Actions**:\\n\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"Which planet is the biggest: Earth, Mars, or Jupiter?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest ?\\n\\nContext of you work is as follows:\\nWhat is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "Jupiter is the biggest planet among Earth, Mars, and itself. Would you like to know more about the planets or anything else?", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest ?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\n**History of Previous Actions**:\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"Which planet is the biggest: Earth, Mars, or Jupiter?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are an intelligent assistant helping the user to complete their task. You have the following task to complete:\\nWhat is the biggest ?\\n\\nContext of you work is as follows:\\nWhat is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\nYou have the following options to continue completing the this task:\\n\\n1. Google Search\\n\\nUse polite and engaging language, and ask the user what they want you to help them next. Not need to greet and keep it short and concise. \\n\\nIf you think the context is not enough to choose an action. Ask the user for more information.\\n", "agent": "human"}], "output": "Jupiter is the biggest planet among Earth, Mars, and Jupiter. How can I assist you further?", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "\\n\\n\\n\\n**Task Description**: What is the biggest ?\\n\\n**Possible Actions**:\\n{\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"string\"\\n },\\n \"usage\": \"Get answers from Google Search\"\\n}\\n\\n**Task Context**:\\nWhat is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\n**History of Previous Actions**:\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\n\\nYou should only respond in JSON format as described below without any extra text.\\nResponse Format:\\n{\\n \"command\": {\\n \"name\": \"tool/command name you choose\",\\n \"args\": {\\n \"arg name\": \"value\"\\n }\\n }\\n}\\nEnsure the response can be parsed by Python json.loads\\n\\nFollow the described format strictly.\\n\\n", "agent": "human"}], "output": "{\\n \"command\": {\\n \"name\": \"Google Search\",\\n \"args\": {\\n \"query\": \"Which planet is the biggest: Earth, Mars, or Jupiter?\"\\n }\\n }\\n}", "llm_name": "gpt-4o-mini"}
{"input": [{"text": "You are a **question answering assistant** who solves user questions and offers a detailed solution.\\n\\nYour name is QA Agent.\\n\\nContext: What is the biggest ?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\nWhich planet is biggest, Earth, Mars or Jupiter?\\n\\n\\nAction - Result History:\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\nGoogle Search{'query': 'Which planet is the biggest: Earth, Mars, or Jupiter?'}\\nJupiter is the biggest planet\\n\\nGiven the context and the action-result history, please complete the task mentioned. Include any links you used from the context and history in the result.\\nTask: What is the biggest ?\\nResult:\\n", "agent": "human"}], "output": "The biggest planet among Earth, Mars, and Jupiter is Jupiter. \\n\\nJupiter is the largest planet in our solar system, significantly larger than both Earth and Mars. To provide some context:\\n\\n- **Jupiter**: It has a diameter of about 86,881 miles (139,822 kilometers) and is primarily composed of gas, making it a gas giant.\\n- **Earth**: The diameter of Earth is about 7,917.5 miles (12,742 kilometers).\\n- **Mars**: Mars has a diameter of about 4,212 miles (6,779 kilometers).\\n\\nIn summary, Jupiter is the largest of the three planets mentioned. \\n\\nFor further information, you can refer to resources like NASA's planetary fact sheets or educational websites about the solar system.", "llm_name": "gpt-4o-mini"}
Loading