Spaces:
Running
Running
pip install transformers[agents] | |
Build your LLM engine by defining a llm_engine method which accepts a list of messages and returns text. This callable also needs to accept a stop argument that indicates when to stop generating. | |
thon | |
from huggingface_hub import login, InferenceClient | |
login("") | |
client = InferenceClient(model="meta-llama/Meta-Llama-3-70B-Instruct") | |
def llm_engine(messages, stop_sequences=["Task"]) -> str: | |
response = client.chat_completion(messages, stop=stop_sequences, max_tokens=1000) | |
answer = response.choices[0].message.content | |
return answer | |
You could use any llm_engine method as long as: | |
1. it follows the messages format for its input (List[Dict[str, str]]) and returns a str | |
2. it stops generating outputs at the sequences passed in the argument stop | |
You also need a tools argument which accepts a list of Tools. You can provide an empty list for tools, but use the default toolbox with the optional argument add_base_tools=True. | |
Now you can create an agent, like [CodeAgent], and run it. For convenience, we also provide the [HfEngine] class that uses huggingface_hub.InferenceClient under the hood. | |
thon | |
from transformers import CodeAgent, HfEngine | |
llm_engine = HfEngine(model="meta-llama/Meta-Llama-3-70B-Instruct") | |
agent = CodeAgent(tools=[], llm_engine=llm_engine, add_base_tools=True) | |
agent.run( | |
"Could you translate this sentence from French, say it out loud and return the audio.", | |
sentence="Où est la boulangerie la plus proche?", | |
) | |
This will be handy in case of emergency baguette need! | |
You can even leave the argument llm_engine undefined, and an [HfEngine] will be created by default. | |
thon | |
from transformers import CodeAgent | |
agent = CodeAgent(tools=[], add_base_tools=True) | |
agent.run( | |
"Could you translate this sentence from French, say it out loud and give me the audio.", | |
sentence="Où est la boulangerie la plus proche?", | |
) | |
Note that we used an additional sentence argument: you can pass text as additional arguments to the model. | |
You can also use this to indicate the path to local or remote files for the model to use: | |
from transformers import ReactCodeAgent | |
agent = ReactCodeAgent(tools=[], llm_engine=llm_engine, add_base_tools=True) | |
agent.run("Why does Mike not know many people in New York?", audio="https://huggingface.co./datasets/huggingface/documentation-images/resolve/main/transformers/recording.mp3") | |
The prompt and output parser were automatically defined, but you can easily inspect them by calling the system_prompt_template on your agent. | |
python | |
print(agent.system_prompt_template) | |
It's important to explain as clearly as possible the task you want to perform. | |
Every [~Agent.run] operation is independent, and since an agent is powered by an LLM, minor variations in your prompt might yield completely different results. | |
You can also run an agent consecutively for different tasks: each time the attributes agent.task and agent.logs will be re-initialized. | |
Code execution | |
A Python interpreter executes the code on a set of inputs passed along with your tools. | |
This should be safe because the only functions that can be called are the tools you provided (especially if it's only tools by Hugging Face) and the print function, so you're already limited in what can be executed. | |
The Python interpreter also doesn't allow imports by default outside of a safe list, so all the most obvious attacks shouldn't be an issue. | |
You can still authorize additional imports by passing the authorized modules as a list of strings in argument additional_authorized_imports upon initialization of your [ReactCodeAgent] or [CodeAgent]: | |
from transformers import ReactCodeAgent | |
agent = ReactCodeAgent(tools=[], additional_authorized_imports=['requests', 'bs4']) | |
agent.run("Could you get me the title of the page at url 'https://huggingface.co./blog'?") | |
() | |
'Hugging Face – Blog' | |
The execution will stop at any code trying to perform an illegal operation or if there is a regular Python error with the code generated by the agent. |