Find out the closest matching using LLM model

I have following code to find out the account that match the most with the passed in name and email.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật