[ad_1]
Chúng tôi sẽ thiết lập hai bộ mô hình, llama3–70b-8192 của Groq và gpt-3.5-turbo của OpenAI, để kiểm tra hai truy vấn. Để so sánh, chúng tôi sẽ so sánh chúng với phản hồi LLM nguyên bản mà không sử dụng tác nhân. Sau đây là các đoạn mã:
from lightrag.elements.agent import ReActAgent
from lightrag.core import Generator, ModelClientType, ModelClient
from lightrag.utils import setup_envsetup_env()
# Outline instruments
def multiply(a: int, b: int) -> int:
"""
Multiply two numbers.
"""
return a * b
def add(a: int, b: int) -> int:
"""
Add two numbers.
"""
return a + b
def divide(a: float, b: float) -> float:
"""
Divide two numbers.
"""
return float(a) / b
llama3_model_kwargs = {
"mannequin": "llama3-70b-8192", # llama3 70b works higher than 8b right here.
"temperature": 0.0,
}
gpt_model_kwargs = {
"mannequin": "gpt-3.5-turbo",
"temperature": 0.0,
}
def test_react_agent(model_client: ModelClient, model_kwargs: dict):
instruments = (multiply, add, divide)
queries = (
"What's the capital of France? and what's 465 occasions 321 then add 95297 after which divide by 13.2?",
"Give me 5 phrases rhyming with cool, and make a 4-sentence poem utilizing them",
)
# outline a generator with out instruments for comparability
generator = Generator(
model_client=model_client,
model_kwargs=model_kwargs,
)
react = ReActAgent(
max_steps=6,
add_llm_as_fallback=True,
instruments=instruments,
model_client=model_client,
model_kwargs=model_kwargs,
)
# print(react)
for question in queries:
print(f"Question: {question}")
agent_response = react.name(question)
llm_response = generator.name(prompt_kwargs={"input_str": question})
print(f"Agent response: {agent_response}")
print(f"LLM response: {llm_response}")
print("")
Cấu trúc của React, bao gồm các đối số khởi tạo và hai thành phần chính: tool_manager
Và planner
được hiển thị bên dưới.
ReActAgent(
max_steps=6, add_llm_as_fallback=True,
(tool_manager): ToolManager(Instruments: (FunctionTool(fn: , async: False, definition: FunctionDefinition(func_name='multiply', func_desc='multiply(a: int, b: int) -> intnn Multiply two numbers.n ', func_parameters={'sort': 'object', 'properties': {'a': {'sort': 'int'}, 'b': {'sort': 'int'}}, 'required': ('a', 'b')})), FunctionTool(fn: , async: False, definition: FunctionDefinition(func_name='add', func_desc='add(a: int, b: int) -> intnn Add two numbers.n ', func_parameters={'sort': 'object', 'properties': {'a': {'sort': 'int'}, 'b': {'sort': 'int'}}, 'required': ('a', 'b')})), FunctionTool(fn: , async: False, definition: FunctionDefinition(func_name='divide', func_desc='divide(a: float, b: float) -> floatnn Divide two numbers.n ', func_parameters={'sort': 'object', 'properties': {'a': {'sort': 'float'}, 'b': {'sort': 'float'}}, 'required': ('a', 'b')})), FunctionTool(fn: .llm_tool at 0x11384b740>, async: False, definition: FunctionDefinition(func_name='llm_tool', func_desc="llm_tool(enter: str) -> strnI reply any enter question with llm's world information. Use me as a fallback instrument or when the question is straightforward.", func_parameters={'sort': 'object', 'properties': {'enter': {'sort': 'str'}}, 'required': ('enter')})), FunctionTool(fn: .end at 0x11382fa60>, async: False, definition: FunctionDefinition(func_name='end', func_desc='end(reply: str) -> strnFinish the duty with reply.', func_parameters={'sort': 'object', 'properties': {'reply': {'sort': 'str'}}, 'required': ('reply')}))), Further Context: {})
(planner): Generator(
model_kwargs={'mannequin': 'llama3-70b-8192', 'temperature': 0.0},
(immediate): Immediate(
template:
{# function/process description #}
You're a useful assistant.
Reply the consumer's question utilizing the instruments offered beneath with minimal steps and most accuracy.
{# REACT directions #}
Every step you'll learn the earlier Thought, Motion, and Statement(execution results of the motion) after which present the following Thought and Motion.
{# Instruments #}
{% if instruments %}You accessible instruments are:
{# instruments #}
{% for instrument in instruments %}
{{ loop.index }}.
{{instrument}}
------------------------
{% endfor %}
{% endif %}
{# output format and examples #}
{{output_format_str}}
{# Job specification to show the agent how one can assume utilizing 'divide and conquer' technique #}
- For easy queries: Straight name the ``end`` motion and supply the reply.
- For complicated queries:
- Step 1: Learn the consumer question and probably divide it into subqueries. And get began with the primary subquery.
- Name one accessible instrument at a time to unravel every subquery/subquestion.
- At step 'end', be part of all subqueries solutions and end the duty.
Keep in mind:
- Motion should name one of many above instruments with title. It cannot be empty.
- You'll at all times finish with 'end' motion to complete the duty. The reply will be the ultimate reply or failure message.
-----------------
Consumer question:
{{ input_str }}
{# Step Historical past #}
{% if step_history %}
{% for historical past in step_history %}
Step {{ loop.index }}.
"Thought": "{{historical past.motion.thought}}",
"Motion": "{{historical past.motion.motion}}",
"Statement": "{{historical past.statement}}"
------------------------
{% endfor %}
{% endif %}
You:, prompt_kwargs: {'instruments': ('func_name: multiplynfunc_desc: "multiply(a: int, b: int) -> intnn Multiply two numbers.n "nfunc_parameters:n sort: objectn properties:n a:n sort: intn b:n sort: intn required:n - an - bn', 'func_name: addnfunc_desc: "add(a: int, b: int) -> intnn Add two numbers.n "nfunc_parameters:n sort: objectn properties:n a:n sort: intn b:n sort: intn required:n - an - bn', 'func_name: dividenfunc_desc: "divide(a: float, b: float) -> floatnn Divide two numbers.n "nfunc_parameters:n sort: objectn properties:n a:n sort: floatn b:n sort: floatn required:n - an - bn', "func_name: llm_toolnfunc_desc: 'llm_tool(enter: str) -> strnn I reply any enter question with llm''s world information. Use me as a fallback tooln or when the question is straightforward.'nfunc_parameters:n sort: objectn properties:n enter:n sort: strn required:n - inputn", "func_name: finishnfunc_desc: 'end(reply: str) -> strnn End the duty with reply.'nfunc_parameters:n sort: objectn properties:n reply:n sort: strn required:n - answern"), 'output_format_str': 'Your output must be formatted as a regular JSON occasion with the next schema:n```n{n "thought": "Why the perform is named (Non-compulsory(str)) (optionally available)",n "motion": "FuncName() Legitimate perform name expression. Instance: "FuncName(a=1, b=2)" Observe the info sort specified within the perform parameters.e.g. for Kind object with x,y properties, use "ObjectType(x=1, y=2) (str) (required)"n}n```nExamples:n```n{n "thought": "I've completed the duty.",n "motion": "end(reply="last reply: 'reply'")"n}n________n```n-Be certain that to at all times enclose the JSON output in triple backticks (```). Please don't add something aside from legitimate JSON output!n-Use double quotes for the keys and string values.n-DO NOT mistaken the "properties" and "sort" within the schema because the precise fields within the JSON output.n-Observe the JSON formatting conventions.'}, prompt_variables: ('input_str', 'instruments', 'step_history', 'output_format_str')
)
(model_client): GroqAPIClient()
(output_processors): JsonOutputParser(
data_class=FunctionExpression, examples=(FunctionExpression(thought='I've completed the duty.', motion='end(reply="last reply: 'reply'")')), exclude_fields=None, return_data_class=True
(output_format_prompt): Immediate(
template: Your output must be formatted as a regular JSON occasion with the next schema:
```
{{schema}}
```
{% if instance %}
Examples:
```
{{instance}}
```
{% endif %}
-Be certain that to at all times enclose the JSON output in triple backticks (```). Please don't add something aside from legitimate JSON output!
-Use double quotes for the keys and string values.
-DO NOT mistaken the "properties" and "sort" within the schema because the precise fields within the JSON output.
-Observe the JSON formatting conventions., prompt_variables: ('instance', 'schema')
)
(output_processors): JsonParser()
)
)
)
Bây giờ, hãy chạy hàm thử nghiệm để xem tác nhân hoạt động như thế nào.
test_react_agent(ModelClientType.GROQ(), llama3_model_kwargs)
test_react_agent(ModelClientType.OPENAI(), gpt_model_kwargs)
Đại lý của chúng tôi sẽ hiển thị các bước cốt lõi cho các nhà phát triển thông qua bản in màu, bao gồm input_query, các bước và câu trả lời cuối cùng. Bản in của truy vấn đầu tiên với llama3 được hiển thị bên dưới (không có màu ở đây):
2024-07-10 16:48:47 - (react.py:287:name) - input_query: What's the capital of France? and what's 465 occasions 321 then add 95297 after which divide by 13.22024-07-10 16:48:48 - (react.py:266:_run_one_step) - Step 1:
StepOutput(step=1, motion=FunctionExpression(thought="Let's break down the question into subqueries and begin with the primary one.", motion='llm_tool(enter="What's the capital of France?")'), perform=Operate(thought=None, title='llm_tool', args=(), kwargs={'enter': 'What's the capital of France?'}), statement='The capital of France is Paris!')
_______
2024-07-10 16:48:49 - (react.py:266:_run_one_step) - Step 2:
StepOutput(step=2, motion=FunctionExpression(thought="Now, let's transfer on to the second subquery.", motion='multiply(a=465, b=321)'), perform=Operate(thought=None, title='multiply', args=(), kwargs={'a': 465, 'b': 321}), statement=149265)
_______
2024-07-10 16:48:49 - (react.py:266:_run_one_step) - Step 3:
StepOutput(step=3, motion=FunctionExpression(thought="Now, let's add 95297 to the consequence.", motion='add(a=149265, b=95297)'), perform=Operate(thought=None, title='add', args=(), kwargs={'a': 149265, 'b': 95297}), statement=244562)
_______
2024-07-10 16:48:50 - (react.py:266:_run_one_step) - Step 4:
StepOutput(step=4, motion=FunctionExpression(thought="Now, let's divide the consequence by 13.2.", motion='divide(a=244562, b=13.2)'), perform=Operate(thought=None, title='divide', args=(), kwargs={'a': 244562, 'b': 13.2}), statement=18527.424242424244)
_______
2024-07-10 16:48:50 - (react.py:266:_run_one_step) - Step 5:
StepOutput(step=5, motion=FunctionExpression(thought="Now, let's mix the solutions of each subqueries.", motion='end(reply="The capital of France is Paris! and the results of the mathematical operation is 18527.424242424244.")'), perform=Operate(thought=None, title='end', args=(), kwargs={'reply': 'The capital of France is Paris! and the results of the mathematical operation is 18527.424242424244.'}), statement='The capital of France is Paris! and the results of the mathematical operation is 18527.424242424244.')
_______
2024-07-10 16:48:50 - (react.py:301:name) - reply:
The capital of France is Paris! and the results of the mathematical operation is 18527.424242424244.
Đối với truy vấn thứ hai, bản in:
2024-07-10 16:48:51 - (react.py:287:name) - input_query: Give me 5 phrases rhyming with cool, and make a 4-sentence poem utilizing them
2024-07-10 16:48:52 - (react.py:266:_run_one_step) - Step 1:
StepOutput(step=1, motion=FunctionExpression(thought="I want to seek out 5 phrases that rhyme with 'cool'.", motion='llm_tool(enter="What are 5 phrases that rhyme with 'cool'?")'), perform=Operate(thought=None, title='llm_tool', args=(), kwargs={'enter': "What are 5 phrases that rhyme with 'cool'?"}), statement='Listed here are 5 phrases that rhyme with "cool":nn1. Rulen2. Tooln3. Fooln4. Pooln5. College')
_______
2024-07-10 16:49:00 - (react.py:266:_run_one_step) - Step 2:
StepOutput(step=2, motion=FunctionExpression(thought='Now that I've the rhyming phrases, I have to create a 4-sentence poem utilizing them.', motion='llm_tool(enter="Create a 4-sentence poem utilizing the phrases 'rule', 'instrument', 'idiot', 'pool', and 'faculty'.")'), perform=Operate(thought=None, title='llm_tool', args=(), kwargs={'enter': "Create a 4-sentence poem utilizing the phrases 'rule', 'instrument', 'idiot', 'pool', and 'faculty'."}), statement="Here's a 4-sentence poem utilizing the phrases 'rule', 'instrument', 'idiot', 'pool', and 'faculty':nnIn the classroom, we study to rule,nWith a pencil as our trusty instrument.nBut if we're not cautious, we could be a idiot,nAnd find yourself swimming within the faculty pool.")
_______
2024-07-10 16:49:12 - (react.py:266:_run_one_step) - Step 3:
StepOutput(step=3, motion=FunctionExpression(thought='I've the poem, now I want to complete the duty.', motion='end(reply="Listed here are 5 phrases that rhyme with 'cool': rule, instrument, idiot, pool, faculty. Here's a 4-sentence poem utilizing the phrases: Within the classroom, we study to rule, With a pencil as our trusty instrument. But when we're not cautious, we could be a idiot, And find yourself swimming within the faculty pool.")'), perform=Operate(thought=None, title='end', args=(), kwargs={'reply': "Listed here are 5 phrases that rhyme with 'cool': rule, instrument, idiot, pool, faculty. Here's a 4-sentence poem utilizing the phrases: Within the classroom, we study to rule, With a pencil as our trusty instrument. But when we're not cautious, we could be a idiot, And find yourself swimming within the faculty pool."}), statement="Listed here are 5 phrases that rhyme with 'cool': rule, instrument, idiot, pool, faculty. Here's a 4-sentence poem utilizing the phrases: Within the classroom, we study to rule, With a pencil as our trusty instrument. But when we're not cautious, we could be a idiot, And find yourself swimming within the faculty pool.")
_______
2024-07-10 16:49:12 - (react.py:301:name) - reply:
Listed here are 5 phrases that rhyme with 'cool': rule, instrument, idiot, pool, faculty. Here's a 4-sentence poem utilizing the phrases: Within the classroom, we study to rule, With a pencil as our trusty instrument. But when we're not cautious, we could be a idiot, And find yourself swimming within the faculty pool.
Sự so sánh giữa tác nhân và phản ứng LLM thông thường được thể hiện dưới đây:
Reply with agent: The capital of France is Paris! and the results of the mathematical operation is 18527.424242424244.
Reply with out agent: GeneratorOutput(information="I would be completely satisfied that can assist you with that!nnThe capital of France is Paris.nnNow, let's sort out the maths downside:nn1. 465 × 321 = 149,485n2. Add 95,297 to that consequence: 149,485 + 95,297 = 244,782n3. Divide the consequence by 13.2: 244,782 ÷ 13.2 = 18,544.09nnSo, the reply is eighteen,544.09!", error=None, utilization=None, raw_response="I would be completely satisfied that can assist you with that!nnThe capital of France is Paris.nnNow, let's sort out the maths downside:nn1. 465 × 321 = 149,485n2. Add 95,297 to that consequence: 149,485 + 95,297 = 244,782n3. Divide the consequence by 13.2: 244,782 ÷ 13.2 = 18,544.09nnSo, the reply is eighteen,544.09!", metadata=None)
Đối với truy vấn thứ hai, sự so sánh được hiển thị bên dưới:
Reply with agent: Listed here are 5 phrases that rhyme with 'cool': rule, instrument, idiot, pool, faculty. Here's a 4-sentence poem utilizing the phrases: Within the classroom, we study to rule, With a pencil as our trusty instrument. But when we're not cautious, we could be a idiot, And find yourself swimming within the faculty pool.
Reply with out agent: GeneratorOutput(information='Listed here are 5 phrases that rhyme with "cool":nn1. rulen2. tooln3. fooln4. pooln5. schoolnnAnd this is a 4-sentence poem utilizing these phrases:nnIn the summer time warmth, I prefer to be cool,nFollowing the rule, I take a dip within the pool.nI'm not a idiot, I do know simply what to do,nI seize my instrument and head again to highschool.', error=None, utilization=None, raw_response='Listed here are 5 phrases that rhyme with "cool":nn1. rulen2. tooln3. fooln4. pooln5. schoolnnAnd this is a 4-sentence poem utilizing these phrases:nnIn the summer time warmth, I prefer to be cool,nFollowing the rule, I take a dip within the pool.nI'm not a idiot, I do know simply what to do,nI seize my instrument and head again to highschool.', metadata=None)
Tác nhân ReAct đặc biệt hữu ích khi trả lời các truy vấn đòi hỏi các khả năng như tính toán hoặc lý luận và lập kế hoạch phức tạp hơn. Tuy nhiên, sử dụng nó cho các truy vấn chung có thể là quá mức cần thiết vì có thể cần nhiều bước hơn mức cần thiết để trả lời truy vấn.
Bản mẫu
Điều đầu tiên bạn muốn tùy chỉnh là bản thân mẫu. Bạn có thể thực hiện việc này bằng cách chuyển mẫu của riêng bạn cho trình xây dựng của tác nhân. Chúng tôi đề xuất bạn sửa đổi mẫu mặc định của chúng tôi: DEFAULT_REACT_AGENT_SYSTEM_PROMPT
.
Ví dụ về định dạng đầu ra tốt hơn
Thứ hai, examples
trong constructor cho phép bạn cung cấp nhiều ví dụ hơn để thực thi định dạng đầu ra chính xác. Ví dụ, nếu chúng ta muốn nó học cách gọi đúng hàm nhân, chúng ta có thể truyền vào một danh sách FunctionExpression
các trường hợp có định dạng đúng. Classmethod from_function
có thể được sử dụng để tạo ra một FunctionExpression
thể hiện của một hàm và các đối số của nó.
from lightrag.core.varieties import FunctionExpression
# generate an instance of calling multiply with key-word arguments
example_using_multiply = FunctionExpression.from_function(
func=multiply,
thought="Now, let's multiply two numbers.",
a=3,
b=4,
)
examples = (example_using_multiply)# move it to the agent
Chúng ta có thể hình dung cách thức điều này được chuyển đến lời nhắc của trình lập kế hoạch thông qua:
react.planner.print_prompt()
Ví dụ trên sẽ được định dạng như sau:
Your output must be formatted as a regular JSON occasion with the next schema:
```
{
"thought": "Why the perform is named (Non-compulsory(str)) (optionally available)",
"motion": "FuncName() Legitimate perform name expression. Instance: "FuncName(a=1, b=2)" Observe the info sort specified within the perform parameters.e.g. for Kind object with x,y properties, use "ObjectType(x=1, y=2) (str) (required)"
}
```
Examples:
```
{
"thought": "Now, let's multiply two numbers.",
"motion": "multiply(a=3, b=4)"
}
________
{
"thought": "I've completed the duty.",
"motion": "end(reply="last reply: 'reply'")"
}
________
```
-Be certain that to at all times enclose the JSON output in triple backticks (```). Please don't add something aside from legitimate JSON output!
-Use double quotes for the keys and string values.
-DO NOT mistaken the "properties" and "sort" within the schema because the precise fields within the JSON output.
-Observe the JSON formatting conventions.
Lớp con ReActAgent
Nếu bạn muốn tùy chỉnh tác nhân hơn nữa, bạn có thể phân lớp ReActAgent
và ghi đè các phương pháp bạn muốn thay đổi.
(1) Một khảo sát về các tác nhân tự chủ dựa trên mô hình ngôn ngữ lớn: Paitesanshi/LLM-Khảo sát-Đại lý
(2) Phản ứng: https://arxiv.org/abs/2210.03629
[ad_2]
Source link