I have following code to find out the account that match the most with the passed in name and email.
<code>import json
from langchain.agents import initialize_agent, AgentType
from agents.llm_models.llm_factory import LLMModelFactory
from agents.tools.account_tool import account_info_tool
PROMPT = """
As a engineer with strong logic, I need you to help me find the account ID for the given {name} and {email_address}. The account details are obtained via a paginated API tool.
Once the account is found, return the corresponding ID in **strict JSON format**.
You have access to the following tool:
{tools}
To achieve the goal, you need to perform 2 steps:
1) call the tool with the input {name} to get a list of the accounts
2) Given the list of account info the tool returned from step 1, find the account matching the most based on th following rules (considering matching more from top to bottom):
if <parent_email> matching with {email_address}.
else if <email> matching with sender {email_address}
else if <name> matching with the {name}t
else if <parent_name> matching with the {name}
"""
llm_model = "llama3-groq-70b-8192-tool-use-preview"
api_key = "my_api_key"
llm = ChatGroq(
groq_api_key=api_key,
model_name=llm_model,
temperature=0
)
tools = [account_info_tool]
def find_account_id(email_address: str, name: str):
agent = initialize_agent(
tools = tools,
llm = LLMModelFactory.load_llm_model(),
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose = True
)
input_data = {
"name": name,
"email_address": email_address,
"tools": ", ".join([tool.name for tool in tools])
}
pm = PROMPT.format(**input_data)
result = agent.invoke(pm)
return result
</code>
<code>import json
from langchain.agents import initialize_agent, AgentType
from agents.llm_models.llm_factory import LLMModelFactory
from agents.tools.account_tool import account_info_tool
PROMPT = """
As a engineer with strong logic, I need you to help me find the account ID for the given {name} and {email_address}. The account details are obtained via a paginated API tool.
Once the account is found, return the corresponding ID in **strict JSON format**.
You have access to the following tool:
{tools}
To achieve the goal, you need to perform 2 steps:
1) call the tool with the input {name} to get a list of the accounts
2) Given the list of account info the tool returned from step 1, find the account matching the most based on th following rules (considering matching more from top to bottom):
if <parent_email> matching with {email_address}.
else if <email> matching with sender {email_address}
else if <name> matching with the {name}t
else if <parent_name> matching with the {name}
"""
llm_model = "llama3-groq-70b-8192-tool-use-preview"
api_key = "my_api_key"
llm = ChatGroq(
groq_api_key=api_key,
model_name=llm_model,
temperature=0
)
tools = [account_info_tool]
def find_account_id(email_address: str, name: str):
agent = initialize_agent(
tools = tools,
llm = LLMModelFactory.load_llm_model(),
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose = True
)
input_data = {
"name": name,
"email_address": email_address,
"tools": ", ".join([tool.name for tool in tools])
}
pm = PROMPT.format(**input_data)
result = agent.invoke(pm)
return result
</code>
import json
from langchain.agents import initialize_agent, AgentType
from agents.llm_models.llm_factory import LLMModelFactory
from agents.tools.account_tool import account_info_tool
PROMPT = """
As a engineer with strong logic, I need you to help me find the account ID for the given {name} and {email_address}. The account details are obtained via a paginated API tool.
Once the account is found, return the corresponding ID in **strict JSON format**.
You have access to the following tool:
{tools}
To achieve the goal, you need to perform 2 steps:
1) call the tool with the input {name} to get a list of the accounts
2) Given the list of account info the tool returned from step 1, find the account matching the most based on th following rules (considering matching more from top to bottom):
if <parent_email> matching with {email_address}.
else if <email> matching with sender {email_address}
else if <name> matching with the {name}t
else if <parent_name> matching with the {name}
"""
llm_model = "llama3-groq-70b-8192-tool-use-preview"
api_key = "my_api_key"
llm = ChatGroq(
groq_api_key=api_key,
model_name=llm_model,
temperature=0
)
tools = [account_info_tool]
def find_account_id(email_address: str, name: str):
agent = initialize_agent(
tools = tools,
llm = LLMModelFactory.load_llm_model(),
agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose = True
)
input_data = {
"name": name,
"email_address": email_address,
"tools": ", ".join([tool.name for tool in tools])
}
pm = PROMPT.format(**input_data)
result = agent.invoke(pm)
return result
The account_info_tool returns
<code>[
{
'user': {
'email': '[email protected]',
'name': 'David Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22086,
'phone': null,
'email': '[email protected]',
'name': 'David Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
},
{
'user': {
'email': '[email protected]',
'name': 'John Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22099,
'phone': null,
'email': '[email protected]',
'name': 'John Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
}
]
</code>
<code>[
{
'user': {
'email': '[email protected]',
'name': 'David Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22086,
'phone': null,
'email': '[email protected]',
'name': 'David Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
},
{
'user': {
'email': '[email protected]',
'name': 'John Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22099,
'phone': null,
'email': '[email protected]',
'name': 'John Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
}
]
</code>
[
{
'user': {
'email': '[email protected]',
'name': 'David Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22086,
'phone': null,
'email': '[email protected]',
'name': 'David Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
},
{
'user': {
'email': '[email protected]',
'name': 'John Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22099,
'phone': null,
'email': '[email protected]',
'name': 'John Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
}
]
I know it is single qoute so not a normal json, but this is from what the llm step printed out. Please see the process below:
<code>> Entering new AgentExecutor chain...
I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindAccountInfo' to get a list of accounts with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
......
</code>
<code>> Entering new AgentExecutor chain...
I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindAccountInfo' to get a list of accounts with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
......
</code>
> Entering new AgentExecutor chain...
I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindAccountInfo' to get a list of accounts with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
Action Input: {'name': 'Smith'}
Observation: [{'user': { 'email': '[email protected]', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': '[email protected]','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': '[email protected]','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': '[email protected]','name': 'John Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2}]
Thought:I need to find the student ID for a student named 'Smith' with the email '[email protected]'. I will use the tool 'FindStudentInfo' to get a list of students with the name 'Smith' and then filter the results based on the email and other matching criteria.
Action: FindStudentInfo
......
As you can see, it keep looping on the step 1 – using tools to find the accounts. But never apply the step 2 – find the matching based on the rules. Any suggestions?