آموزش Agent-بخش 9: کتابخانه Dummy Agent

این پست ها به صورت مستقل از فریمورک طراحی شده است، هدف اصلی تمرکز بر مفاهیم agent های هوش مصنوعی بوده و فعلا نمیخواهیم در مورد جزئیات مربوط به یک فریمورک خاص بحث کنیم.
همچنین، میخواهیم خوانندگان بتوانند مفاهیمی که در این آموزشها فرا میگیرند را در پروژههای خودشان و با استفاده از هر فریمورکی که دوست دارند، به کار بگیرند.
بنابراین،فعلا، از یک کتابخانه dummy agent و یک serverless API ساده برای دسترسی به موتور LLM خود استفاده میکنیم.
احتمالاً این ابزارها در محیط تولید (production) مورد استفاده قرار نمیگیرند، اما به عنوان نقطه شروع خوبی برای درک نحوه عملکرد agents کمک خواهند کرد.
پس از این بخش، آماده خواهید بود تا یک Agent ساده با استفاده از smolagents ایجاد کنید.
در بخشهای بعدی، همچنین از کتابخانههای دیگر AI Agent مثل LangGraph، LangChain و LlamaIndex استفاده خواهیم کرد.
برای ساده نگه داشتن فرآیند، از یک تابع پایتونی ساده به عنوان Tool و Agent استفاده میکنیم.
از پکیجهای داخلی پایتون مثل datetime و os استفاده خواهیم کرد تا بتوانید این موارد را در هر محیطی امتحان کنید.
همچنین شما میتوانید فرآیند را در این notebook دنبال کنید و کدها را خودتان اجرا کنید.
Serverless API
در اکوسیستم Hugging Face، یک ویژگی کاربردی به نام Serverless API وجود دارد که به شما اجازه میدهد به راحتی inference را روی بسیاری از مدلها اجرا کنید. نیازی به نصب یا استقرار (deployment) وجود ندارد.
import os from huggingface_hub import InferenceClient ## You need a token from https://hf.co/settings/tokens, ensure that you select 'read' as the token type. If you run this on Google Colab, you can set it up in the "settings" tab under "secrets". Make sure to call it "HF_TOKEN" os.environ["HF_TOKEN"]="hf_xxxxxxxxxxxxxx" client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct") # if the outputs for next cells are wrong, the free model may be overloaded. You can also use this public endpoint that contains Llama-3.2-3B-Instruct # client = InferenceClient("https://jc26mwg228mkj8dw.us-east-1.aws.endpoints.huggingface.cloud")
output = client.text_generation( "The capital of France is", max_new_tokens=100, ) print(output)
خروجی:
Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris. The capital of France is Paris.
همانطور که در بخش LLM مشاهده شد، اگر فقط عملیات decoding را انجام دهیم، مدل تنها زمانی متوقف خواهد شد که یک EOS token را پیشبینی کند. اما این اتفاق در اینجا رخ نمیدهد، زیرا این مدل یک مدل conversational (chat) است و ما قالب (template) چت مورد انتظار آن را اعمال نکردهایم.
اگر اکنون special tokenهای مرتبط با مدل Llama-3.2-3B-Instruct که در حال استفاده هستیم را اضافه کرده، رفتار مدل تغییر میکند و اکنون EOS مورد انتظار را تولید میکند.
prompt="""<|begin_of_text|><|start_header_id|>user<|end_header_id|> The capital of France is<|eot_id|><|start_header_id|>assistant<|end_header_id|>""" output = client.text_generation( prompt, max_new_tokens=100, ) print(output)
خروجی:
The capital of France is Paris.
استفاده از متد chat راهی بسیار راحتتر و قابلاعتمادتر برای اعمال قالبهای (templates) چت است.
output = client.chat.completions.create( messages=[ {"role": "user", "content": "The capital of France is"}, ], stream=False, max_tokens=1024, ) print(output.choices[0].message.content)
خروجی:
Paris.
متد chat به عنوان روش توصیهشده (RECOMMENDED) برای استفاده در جهت تضمین انتقال روان بین مدلها شناخته میشود، اما از آنجایی که این notebook صرفاً آموزشی است، ما همچنان از متد text_generation
استفاده خواهیم کرد تا بهتر جزئیات را درک کنیم.
Dummy Agent
در بخشهای قبلی مشاهده کردیم که هسته اصلی یک کتابخانه agent اضافه کردن اطلاعات به system prompt است.
این system prompt کمی پیچیدهتر از چیزی است که قبلاً دیدیم، اما در حال حاضر شامل موارد زیر است:
- اطلاعات مربوط به ابزارها (tools)
- دستورالعملهای چرخهای (Thought → Action → Observation)
(this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $JSON_BLOB must be formatted as markdown and only use a SINGLE action at a time.)
از آنجایی که در حال اجرای متد text_generation هستیم، باید prompt را به صورت دستی اعمال کنیم.
prompt=f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|> {SYSTEM_PROMPT} <|eot_id|><|start_header_id|>user<|end_header_id|> What's the weather in London ? <|eot_id|><|start_header_id|>assistant<|end_header_id|> """
ما همچنین میتوانیم این کار را به صورت زیر انجام دهیم، که دقیقاً همان چیزی است که در داخل متد chat اتفاق میافتد:
messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": "What's the weather in London ?"}, ] from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct") tokenizer.apply_chat_template(messages, tokenize=False,add_generation_prompt=True)
پرامپت اکنون به این صورت است:
<|begin_of_text|><|start_header_id|>system<|end_header_id|> Answer the following questions as best you can. You have access to the following tools: get_weather: Get the current weather in a given location The way you use the tools is by specifying a json blob. Specifically, this json should have an `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here). The only values that should be in the "action" field are: get_weather: Get the current weather in a given location, args: {"location": {"type": "string"}} example use : {{ "action": "get_weather", "action_input": {"location": "New York"} }} ALWAYS use the following format: Question: the input question you must answer Thought: you should always think about one action to take. Only one action at a time in this format: Action: $JSON_BLOB (inside markdown cell) Observation: the result of the action. This Observation is unique, complete, and the source of truth. ... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $JSON_BLOB must be formatted as markdown and only use a SINGLE action at a time.) You must always end your output with the following format: Thought: I now know the final answer Final Answer: the final answer to the original input question Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. <|eot_id|><|start_header_id|>user<|end_header_id|> What's the weather in London ? <|eot_id|><|start_header_id|>assistant<|end_header_id|>
حالا دیکد کنیم:
output = client.text_generation( prompt, max_new_tokens=200, ) print(output)
خروجی:
Thought: I will check the weather in London. Action: ``` { "action": "get_weather", "action_input": {"location": "London"} } ``` Observation: The current weather in London is mostly cloudy with a high of 12°C and a low of 8°C.
آیا مشکلی مشاهده میکنید؟
پاسخ مدل دچار hallucination یا توهم شده است. حالا بیایید روی مرحله Observation متوقف شویم تا مدل پاسخ واقعی تابع را hallucinate نکند.
output = client.text_generation( prompt, max_new_tokens=200, stop=["Observation:"] # Let's stop before any actual function is called ) print(output)
خروجی:
Thought: I will check the weather in London. Action: ``` { "action": "get_weather", "action_input": {"location": "London"} } ``` Observation:
حالا بیایید یک تابع نمونه برای get weather ایجاد کنیم. در مسائل دنیای واقعی، احتمالاً به یک API روی اینترنت متصل خواهید شد.
# Dummy function def get_weather(location): return f"the weather in {location} is sunny with low temperatures. \n" get_weather('London')
خروجی:
'the weather in London is sunny with low temperatures. \n'
بیایید پرامپت اصلی، خروجی تولید شده تا لحظه اجرای تابع، و نتیجه اجرای تابع را به عنوان یک مشاهده (Observation) را با هم ترکیب کنیم و سپس تولید متن را ادامه دهیم.
new_prompt = prompt + output + get_weather('London') final_output = client.text_generation( new_prompt, max_new_tokens=200, ) print(final_output)
پرامپت جدید به صورت زیر خواهد شد:
<|begin_of_text|><|start_header_id|>system<|end_header_id|> Answer the following questions as best you can. You have access to the following tools: get_weather: Get the current weather in a given location The way you use the tools is by specifying a json blob. Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here). The only values that should be in the "action" field are: get_weather: Get the current weather in a given location, args: {"location": {"type": "string"}} example use : {{ "action": "get_weather", "action_input": {"location": "New York"} }} ALWAYS use the following format: Question: the input question you must answer Thought: you should always think about one action to take. Only one action at a time in this format: Action: $JSON_BLOB (inside markdown cell) Observation: the result of the action. This Observation is unique, complete, and the source of truth. ... (this Thought/Action/Observation can repeat N times, you should take several steps when needed. The $JSON_BLOB must be formatted as markdown and only use a SINGLE action at a time.) You must always end your output with the following format: Thought: I now know the final answer Final Answer: the final answer to the original input question Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. <|eot_id|><|start_header_id|>user<|end_header_id|> What's the weather in London ? <|eot_id|><|start_header_id|>assistant<|end_header_id|> Thought: I will check the weather in London. Action: ``` { "action": "get_weather", "action_input": {"location": {"type": "string", "value": "London"} } ``` Observation:the weather in London is sunny with low temperatures.
خروجی:
Final Answer: The weather in London is sunny with low temperatures.
در این پست ما یاد گرفتیم چگونه میتوانیم Agentها را از ابتدا با استفاده از کد پایتون ایجاد کنیم، و دیدیم که این فرآیند چقدر طولانی و خستهکننده میتواند باشد. خوشبختانه، بسیاری از کتابخانههای Agent بخش زیادی از کارهای سنگین را به جای شما انجام میدهند و کار را سادهتر میکنند.
حال، آماده هستیم تا اولین Agent واقعی خود را با استفاده از کتابخانه smolagents ایجاد کنیم. در پست بعدی همراه کلاس ویژن باشید…
دیدگاهتان را بنویسید