==========
API server
==========

The built-in API server enables you to run open-source large language models (LLMs) locally on your own machine. It facilitates direct interaction with your models through API calls, providing a powerful tool for testing and deploying models without the need for external cloud services. By hosting your own API server locally, you have full control over the model's behavior and maintain the privacy and security of your data.

Understanding the API server
============================

The API server is the core component of Anaconda AI Navigator that enables you to interact with your locally downloaded LLMs through API calls.

.. tip::

    Hover over a tooltip |circle-info| to view information about the API Server's configurable parameters. 

Server address
--------------

Your local server address, often referred to as ``localhost`` or ``127.0.0.1``, is the default address for the API server. It is a loopback address, meaning any network communication sent to this address will stay within the same machine, keeping your data secure and private. This is common for developing applications.

If you are working in an office network and would like to make your server accessible to other devices within the same network, you can set the server address to your machine's local IP address. These are typically private networks, meaning they're not routable on the public internet. 

Setting the server address to ``0.0.0.0`` configures it to accept connections on all network interfaces. This can expose your server to the public internet if your network is configured to allow external connections and route traffic from external sources to your server.

Server port
-----------

The server port tells the built-in API server where to listen for incoming traffic (i.e. where to listen and pick up API requests).

For more information, see `Cloudflare's article on ports <https://www.cloudflare.com/learning/network-layer/what-is-a-computer-port/>`_. 

API Key
-------

Establish an API Key that must be passed as an ``Authentication: Bearer`` token in the header of requests made to your server. 

.. dropdown:: Testing your API Key
    :color: primary
    :animate: fade-in

    To test an API Key: 
    
    #. Select |cloud-api| **API Server** from the left-hand navigation.
    #. Enter your API Key in AI Navigator.
    #. :ref:`Load a model into the API server <load_model>`.

        .. note:: 

            You must use a ``text-generation`` type model. 

    #. Open Anaconda Prompt (Terminal on macOS/Linux) and run the following command:

        .. tab-set::

            .. tab-item:: Anaconda Prompt

                .. code-block:: none

                    # Replace <API_KEY> with the value you entered into AI Navigator
                    curl --request POST ^
                         --url http://localhost:8080/completion ^
                         --header 'Authorization: Bearer <API_KEY>' ^
                         --header "Content-Type: application/json" ^
                         --data '{
                             "prompt": "Hello, how are you?"
                         }'

            .. tab-item:: Anaconda PowerShell Prompt

                .. code-block:: none

                    # Replace <API_KEY> with the value you entered into AI Navigator
                    curl --request POST `
                         --url http://localhost:8080/completion `
                         --header 'Authorization: Bearer <API_KEY>' `
                         --header "Content-Type: application/json" `
                         --data '{
                             "prompt": "Hello, how are you?"
                         }'

            .. tab-item:: Terminal

                .. code-block:: none

                    # Replace <API_KEY> with the value you entered into AI Navigator
                    curl --request POST \
                         --url http://localhost:8080/completion \
                         --header 'Authorization: Bearer <API_KEY>' \
                         --header "Content-Type: application/json" \
                         --data '{
                             "prompt": "Hello, how are you?"
                         }'

    This command will reach out to the local API servers ``/completion`` endpoint to interact with the model you have loaded. If you receive a response, your API Key is working. If you receive a 401 error, double check your command and try again. 

Viewing API server logs
=======================

To view your server logs:

#. Open Anaconda AI Navigator.
#. Select |cloud-api| **API Server** from the left-hand navigation. 

The server logs provide information for the following metrics:
    * **System information** - Provides information about your system's hardware capabilities. 
    * **Build information** - Provides information about the version of the API server you are using. 
    * **Chat template** - Shows the sequence of messaging defined for the system.
    * **Server listening** - Displays the server address and port number being listened to.
    * **Slot information** - Displays the number of available slots for the server. Each slot is able to manage one user API request at a time.
    * **Task information** - Displays information such as time spent processing and responding to a request, the request task ID, and which slot fulfilled the task.

The API server records all incoming traffic for the currently running server and displays relevant information in the **Server Logs**.

Once the server is stopped, the log is saved to ``~/Library/Logs/ai-navigator`` with a timestamped filename (for example ``<YYYY><MM><DD><HH><MM><SS>_api-server.log``), so you can efficiently locate specific server logs if necessary. 

.. |cloud-api| raw:: html

    <svg class="inline" width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><g clip-path="url(#clip0_2642_98)"><path d="M16.9687 8.71875C18.7187 9.25 20 10.8438 20 12.75C20 15.125 18.0938 17 15.75 17H4.5C2 17 0 15 0 12.5C0 10.5625 1.25 8.90625 3 8.28125C2.96875 5.375 5.3125 3 8.25 3C9.96872 3 11.5 3.875 12.4687 5.1875C12.7813 5.09375 13.125 5 13.5 5C15.4062 5 17 6.59375 17 8.5C17 8.59375 16.9687 8.65625 16.9687 8.71875ZM15.75 15.5C17.25 15.5 18.5 14.2813 18.5 12.75C18.5 11.5625 17.6875 10.5 16.5313 10.1562L15.4062 9.8125L15.4687 8.625C15.5313 7.40625 14.4375 6.15625 12.9062 6.625L11.875 6.9375L11.25 6.0625C10.5313 5.09375 9.43752 4.5 8.25 4.5C6.1875 4.5 4.53125 6.15625 4.5 8.1875C4.5 8.3125 4.5 8.125 4.5 9.34375L3.5 9.71875C2.28125 10.125 1.5 11.25 1.5 12.5C1.5 14.1562 2.84375 15.5 4.5 15.5H15.75Z"/><path d="M5.40366 13.4H4.50366L6.14229 8.74545H7.18321L8.82413 13.4H7.92411L6.68093 9.7H6.64457L5.40366 13.4ZM5.43321 11.575H7.88775V12.2522H5.43321V11.575ZM9.43717 13.4V8.74545H11.1826C11.5402 8.74545 11.8402 8.81212 12.0826 8.94545C12.3266 9.07878 12.5107 9.26212 12.6349 9.49545C12.7607 9.72727 12.8236 9.99091 12.8236 10.2864C12.8236 10.5848 12.7607 10.85 12.6349 11.0818C12.5092 11.3136 12.3236 11.4962 12.0781 11.6295C11.8326 11.7614 11.5304 11.8273 11.1712 11.8273H10.0144V11.1341H11.0576C11.2668 11.1341 11.438 11.0978 11.5712 11.025C11.7046 10.9523 11.8031 10.8523 11.8668 10.725C11.9319 10.5977 11.9644 10.4515 11.9644 10.2864C11.9644 10.1212 11.9319 9.97576 11.8668 9.85C11.8031 9.72424 11.7039 9.62651 11.569 9.55681C11.4356 9.4856 11.2637 9.45 11.0531 9.45H10.2804V13.4H9.43717ZM14.3991 8.74545V13.4H13.556V8.74545H14.3991Z"/></g><defs><clipPath id="clip0_2642_98"><rect width="20" height="14" transform="translate(0 3)"/></clipPath></defs></svg>

.. |circle-info| raw:: html

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="1em" height="1em"><path d="M256 32a224 224 0 1 1 0 448 224 224 0 1 1 0-448zm0 480A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM208 352c-8.8 0-16 7.2-16 16s7.2 16 16 16h96c8.8 0 16-7.2 16-16s-7.2-16-16-16H272V240c0-8.8-7.2-16-16-16H216c-8.8 0-16 7.2-16 16s7.2 16 16 16h24v96H208zm48-168a24 24 0 1 0 0-48 24 24 0 1 0 0 48z"/></svg>