# Custom model

## Overview

Integrate your own LLMs to Datasaur with custom models, and use them for exploration in Sandbox or for evaluation.

## Integration

While Datasaur offers direct integrations with some providers, you can also connect to models hosted on various third-party platforms or your own infrastructure using the "Custom model" feature.

{% hint style="info" %}
To simplify integration, Datasaur's Custom Model connection adheres to the widely adopted API structure defined by OpenAI for its completions or chat completions endpoints. This means that if your self-hosted model or third-party serving framework exposes an endpoint that mimics the OpenAI API format, connecting it to Datasaur is straightforward.
{% endhint %}

#### API specification

To make things easy, Datasaur connects to these custom models using a common method – an API structure that looks just like the one used by OpenAI (specifically, the "Chat Completions" API).

Think of it like using a standard plug: if your model hosting tool (like TGI) provides this standard API interface (an OpenAI-compatible API), Datasaur can plug right in.

#### How the API connection works

{% hint style="info" %}
Please note that the `streaming` option is currently disabled for custom models.
{% endhint %}

When Datasaur uses your custom model, here’s basically what happens:

1. **Datasaur Sends a Request:** Datasaur sends information to your model's address using a standard web method (`POST`). This request goes to a specific path, usually `/v1/chat/completions`, added to the main address you provide. The request includes:

   * **Extra Info (Headers):** Tells the server the data is in JSON format. If you added an API Key in Datasaur, it sends that key for security (`Authorization: Bearer YOUR_API_KEY`).
   * **The Actual Data (JSON Body):** This contains the important bits:
     * `model`: The name of the specific model you want to use.
     * `messages`: The conversation history, including instructions ("system" message) and the user's input ("user" message).
     * Optional settings like `temperature` (for creativity) or `max_tokens` (to limit response length).

   *Example Request Data:*

   ```json
   {
     "model": "tgi",
     "messages": [
       {
         "role": "system",
         "content": "You are a helpful assistant."
       },
       {
         "role": "user",
         "content": "Explain what an LLM is in simple terms."
       }
     ],
     "temperature": 0.7,
     "max_tokens": 150
   }
   ```
2. **Your Model Sends a Response:** If everything works, your model hosting tool sends back a success message (`200 OK`) with its own JSON data, including:

   * `id`: A unique ID for this conversation turn.
   * `model`: Which model actually answered.
   * `choices`: An array (usually just one item) containing the model's reply:
     * `message`: The actual text generated by the model (`content`) and its role (`assistant`).
   * `usage` (Optional): Information about how many "pieces" of text (tokens) were used for the prompt and the answer.

   *Example Response Data:*

   ```json
   {
     "id": "chatcmpl-randomid12345",
     "object": "chat.completion",
     "created": 1713867000, // Timestamp
     "model": "your-model-name",
     "choices": [
       {
         "index": 0,
         "message": {
           "role": "assistant",
           "content": "An LLM, or Large Language Model, is like a super-smart computer program that's read tons of text, so it can understand and write text almost like a human!"
         },
         "finish_reason": "stop" // Why the model stopped writing
       }
     ],
     "usage": {
       "prompt_tokens": 30,
       "completion_tokens": 55,
       "total_tokens": 85
     }
   }

   ```

#### Text Generation Inference (TGI) & Huggingface

* **Main Address (Base URL):** Put the address where your TGI server is running. This might look like `http://your-tgi-server-ip:8080/v1` or maybe use port `80` if using certain hosting like Hugging Face Inference Endpoints. Add `/v1` at the end.
* **Model Name (in Request Data):** TGI usually runs one main model at a time. You might just need to put `"tgi"` as the model name, or use the specific Hugging Face name the model was started with (like `"NousResearch/Nous-Hermes-2"`). Check your TGI setup. [Learn more about TGI](https://huggingface.co/docs/text-generation-inference/en/basic_tutorials/consuming_tgi#curl).
* **API Key:** If you're using TGI, especially through services like Hugging Face, you'll likely **need** an API Key or Token. Get this key from your TGI provider (like your Hugging Face Access Token) and put it in the API Key field in Datasaur's Custom Model settings.

## Connect custom models

To connect a custom model:

1. Navigate to the **Models catalog** page, then click **Manage providers**.
2. Select **Custom model**.

   <figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-cadc239a2dba3daaf8ec7df6be5af622d2a38531%2FModels%20-%20Manage%20providers%20dialog%20-%20initial.png?alt=media" alt=""><figcaption><p>Custom models option</p></figcaption></figure>
3. Input your model credentials. The required credentials are:

   1. **Endpoint URL**: The endpoint URL of your model.
   2. **API key**: The API key of your model.
   3. **Model name**: Your desired model name to be used in LLM Labs.

   <figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-5e669b773ba0b9e5a046af0ce2c93217d8fcfc5d%2FModels%20-%20Manage%20providers%20dialog%20-%20Custom%20language%20model%20-%20Initial.png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
If you are adding a custom model from an LLM provider like Hugging Face, you only need to input the endpoint URL without the `/v1/chat/completions` suffix.
{% endhint %}

5. Once you’ve added your credentials, click the **Add custom model** button, and your custom model will be available in LLM Labs.

   <figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-a1259983cb98c7e5492b5b041f225ccfc66b523f%2FModels%20-%20Custom%20language%20model%20-%20connected.png?alt=media" alt=""><figcaption><p>Models available</p></figcaption></figure>

## Manage custom models

To manage your custom models, click the three-dots icon on the model card. From there, you can:

* View details
* Try in Sandbox
* Edit
* Delete

<figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-eaeaf2656cd4f218fc66cac1d206af6e3869f81d%2FModels%20-%20Custom%20language%20model%20-%20more%20menu.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Try in Sandbox

Click **Try in Sandbox** and you'll be automatically taken to a Sandbox project. This allows you to use it as a base model and test how it works with additional instructions and various prompts. [Learn more about Sandbox](https://docs.datasaur.ai/llm-projects/sandbox)

<figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-122fdbf12ce824a36a604925d77e3211f40826ef%2FSandbox%20-%20custom%20models%20-%20initial.png?alt=media" alt=""><figcaption><p>Try in Sandbox</p></figcaption></figure>

#### Edit custom model

Click **Edit** to modify the endpoint URL, API key, and model name. Once you've updated the model credentials, click **Save custom model** to save your changes.

<figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-1d48290fe2e6ca5e2c05a155ded4bf9d75cae2be%2FModels%20-%20Manage%20providers%20dialog%20-%20Custom%20language%20model%20-%20edit.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Delete custom model

Click **Delete** to delete your model from LLM Labs. Confirm the deletion by clicking the **Delete custom model** button.

<figure><img src="https://448889121-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MbjY0HseEqu7LtYAt4d%2Fuploads%2Fgit-blob-839b87cdbf247162d7e2c4370c28a6c26cebe59a%2FModels%20-%20Manage%20providers%20dialog%20-%20Custom%20language%20model%20-%20delete.png?alt=media" alt=""><figcaption><p>Delete custom model</p></figcaption></figure>
