> For the complete documentation index, see [llms.txt](https://docs.aios.foundation/aios-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aios.foundation/aios-docs/aios-kernel/llm-cores/hugging-face-backend.md).

# Hugging Face Backend

The Hugging Face Local Backend allows running models locally using the Hugging Face Transformers library.

The HF Local Backend is initialized as a class instance:

```python
case "huggingface":
    self.llms.append(HfLocalBackend(
        model_name=config.name,
        max_gpu_memory=config.max_gpu_memory,
        eval_device=config.eval_device
    ))
```

It handles loading and running Hugging Face models locally, with options for GPU memory allocation.

**Standard Text Input**

For standard text requests, the backend uses the `generate()` method:

```python
completed_response = model.generate(**completion_kwargs)
return completed_response, True
```

**Tool Calls**

As huggingface models do not natively support tool calls, the adapter merges tool information into messages before generation and decodes tool calls after generation.&#x20;

```python
if tools:
    new_messages = merge_messages_with_tools(messages, tools)
    completion_kwargs["messages"] = new_messages

completed_response = model.generate(**completion_kwargs)

# During processing
if tools:
    if isinstance(model, HfLocalBackend):
        if finished:
            tool_calls = decode_hf_tool_calls(completed_response)
            tool_calls = double_underscore_to_slash(tool_calls)
            return LLMResponse(
                response_message=None,
                tool_calls=tool_calls,
                finished=finished
            )
```

The [`merge_messages_with_tools()`](https://github.com/agiresearch/AIOS/blob/main/aios/llm_core/utils.py) function formats the tool information into the prompt, and [`decode_hf_tool_calls()`](https://github.com/agiresearch/AIOS/blob/main/aios/llm_core/utils.py) extracts tool calls from the text response.

**JSON-Formatted Responses**

JSON formatting is handled by merging the response format into the messages:

```python
elif message_return_type == "json":
    new_messages = merge_messages_with_response_format(messages, response_format)
    completion_kwargs["messages"] = new_messages
```

The [`merge_messages_with_response_format()`](https://github.com/agiresearch/AIOS/blob/main/aios/llm_core/utils.py) function likely adds instructions for the model to respond in JSON format.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aios.foundation/aios-docs/aios-kernel/llm-cores/hugging-face-backend.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
