> ## Documentation Index
> Fetch the complete documentation index at: https://docs.obiguard.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Hugging Face

Obiguard provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications,
including all the text generation models supported by [Hugging Face's Inference endpoints](https://huggingface.co/docs/api-inference/index).

With Obiguard, you can take advantage of features like fast AI gateway access, observability, prompt management, and more,
all while ensuring the secure management of your LLM API keys through a [virtual key](/virtual-keys) system.

<Note>
  Provider Slug. `Hugging Face`
</Note>

## Obiguard SDK Integration with Hugging Face

Obiguard provides a consistent API to interact with models from various providers. To integrate Hugging Face with Obiguard:

### 1. Install the Obiguard SDK

Add the Obiguard SDK to your application to interact with Hugging Face's API through Obiguard's gateway.

<Tabs>
  <Tab title="Python SDK">
    ```sh theme={null}
    pip install obiguard
    ```
  </Tab>
</Tabs>

### 2. Initialize Obiguard with the Virtual Key

To use Hugging Face with Obiguard, [get your Hugging Face Access token from here](https://huggingface.co/settings/tokens), then add it to Obiguard to create the virtual key.

<Tabs>
  <Tab title="Python SDK">
    ```py theme={null}
    from obiguard import Obiguard

    client = Obiguard(
      obiguard_api_key="sk-obg***", # Your Obiguard API key # Replace with your Obiguard API key
      virtual_key="VIRTUAL_KEY", # Replace with your virtual key for Hugging Face
      huggingface_base_url="Hugging Face_DEDICATED_URL" # Optional: Use this if you have a dedicated server hosted on Hugging Face
    )
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```py theme={null}
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, createHeaders

    client = OpenAI(
      api_key="Hugging Face_ACCESS_TOKEN",
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=createHeaders(
        obiguard_api_key="sk-obg***", # Your Obiguard API key
        provider="Hugging Face",
        huggingface_base_url="Hugging Face_DEDICATED_URL"
      )
    )
    ```
  </Tab>
</Tabs>

### 3. Invoke Chat Completions with Hugging Face

Use the Obiguard instance to send requests to Hugging Face. You can also override the virtual key directly in the API call if needed.

<Tabs>
  <Tab title="Python SDK">
    ```py theme={null}
    chat_completion = client.chat.completions.create(
      messages = [{"role": 'user', "content": 'Say this is a test'}],
      model = 'meta-llama/meta-llama-3.1-8b-instruct', # make sure your model is hot
    )

    print(chat_completion.choices[0].message.content)
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```py theme={null}
    chat_completion = client.chat.completions.create(
      messages = [{"role": 'user', "content": 'Say this is a test'}],
      model = 'meta-llama/meta-llama-3.1-8b-instruct', # make sure your model is hot
    )

    print(chat_completion.choices[0].message.content)
    ```
  </Tab>
</Tabs>

## [Using Virtual Keys](/virtual-keys)

Virtual Keys serve as Obiguard's unified authentication system for all LLM interactions,
simplifying the use of multiple providers and Obiguard features within your application.
For self-hosted LLMs, you can configure custom authentication requirements including authorization keys, bearer tokens, or any other headers needed to access your model.

1. Navigate to [Virtual Keys](/virtual-keys) in your Obiguard dashboard
2. Click **"Add Key"** and enable the **"Local/Privately hosted provider"** toggle
3. Configure your deployment:

* Select the matching provider API specification (typically `OpenAI`)
* Enter your model's base URL in the `Custom Host` field
* Add required authentication headers and their values

You can now use this virtual key in your requests:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    client = Obiguard(
      obiguard_api_key="vk-obg***",  # Your Obiguard virtual key
      virtual_key="YOUR_SELF_HOSTED_LLM_VIRTUAL_KEY"
    )

    response = client.chat.completions.create(
      model="your-self-hosted-model-name",
      messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello!"}
      ]
    )
    ```
  </Tab>
</Tabs>

For more information about managing self-hosted LLMs with Obiguard, see [Bring Your Own LLM](/integrations/llms/byollm).

## Next Steps

The complete list of features supported in the SDK are available on the link below.

<Card title="Obiguard SDK Client" icon="code" href="/api-reference/sdk/python">
  Learn more about the Obiguard SDK Client
</Card>
