Код: Выделить всё
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
matching with {email_address}.
else if matching with sender {email_address}
else if matching with the {name}t
else if 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
Код: Выделить всё
[
{
'user': {
'email': 'abc@gmail.com',
'name': 'David Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22086,
'phone': null,
'email': 'abc@gmail.com',
'name': 'David Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
},
{
'user': {
'email': 'def@gmail.com',
'name': 'John Smith',
'organization_id': 1,
'phone': null
},
'user_id': 22099,
'phone': null,
'email': 'def@gmail.com',
'name': 'John Smith',
'created': '2024-08-11T04:58:46.798058Z',
'modified': '2024-09-07T17:21:50.186396Z',
'updated_by': 2
}
]
Код: Выделить всё
> Entering new AgentExecutor chain...
I need to find the student ID for a student named 'Smith' with the email 'abc@gmail.com'. 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': 'abc@gmail.com', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': 'abc@gmail.com','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': 'def@gmail.com','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': 'def@gmail.com','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 'abc@gmail.com'. 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': 'abc@gmail.com', 'name': 'David Smith','organization_id': 1,'phone': null}, 'user_id': 22086,'phone': null,'email': 'abc@gmail.com','name': 'David Smith','created': '2024-08-11T04:58:46.798058Z','modified': '2024-09-07T17:21:50.186396Z','updated_by': 2},{'user': {'email': 'def@gmail.com','name': 'John Smith','organization_id': 1,'phone': null},'user_id': 22099,'phone': null,'email': 'def@gmail.com','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 'abc@gmail.com'. 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
......
Подробнее здесь: https://stackoverflow.com/questions/790 ... -llm-model