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

# Model servers

export const Comments = ({children}) => {
  return <div class="my-4 px-5 py-4 overflow-hidden rounded-2xl flex gap-3 border border-zinc-500/20 bg-zinc-50/50 dark:border-zinc-500/30 dark:bg-zinc-500/10" data-callout-type="comments">
      <div class="w-4">
        <svg width="14" height="14" viewBox="0 0 640 640" fill="currentColor" xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" aria-label="Comments">
            <path d="M320 112C434.9 112 528 205.1 528 320C528 434.9 434.9 528 320 528C205.1 528 112 434.9 112 320C112 205.1 205.1 112 320 112zM320 576C461.4 576 576 461.4 576 320C576 178.6 461.4 64 320 64C178.6 64 64 178.6 64 320C64 461.4 178.6 576 320 576zM280 400C266.7 400 256 410.7 256 424C256 437.3 266.7 448 280 448L360 448C373.3 448 384 437.3 384 424C384 410.7 373.3 400 360 400L352 400L352 312C352 298.7 341.3 288 328 288L280 288C266.7 288 256 298.7 256 312C256 325.3 266.7 336 280 336L304 336L304 400L280 400zM320 256C337.7 256 352 241.7 352 224C352 206.3 337.7 192 320 192C302.3 192 288 206.3 288 224C288 241.7 302.3 256 320 256z" />
        </svg>
      </div>
      <div class="text-sm prose min-w-0 w-full">
        {children}
      </div>
    </div>;
};

export const TroubleshootSolution = ({children}) => <>
    <hr className="my-3 w-full" />
    <details className="mt-3">
      <summary className="cursor-pointer font-semibold text-base mb-1">
        Solution
      </summary>
      <div className="mt-2 ml-4" data-component-part="step-content">
        {children}
      </div>
    </details>
  </>;

export const TroubleshootCause = ({children}) => <details className="mt-3 mb-2">
    <summary className="cursor-pointer font-semibold text-base mb-1">
      Cause
    </summary>
    <div className="mt-2 ml-4" data-component-part="step-content">
      {children}
    </div>
  </details>;

export const TroubleshootTitle = ({children}) => <>
    <p className="m-0 font-semibold text-xl leading-tight mb-2" role="heading" aria-level={3}>
      {children}
    </p>
    <hr className="my-3 w-full" />
  </>;

export const Troubleshoot = ({children}) => <div className="callout my-4 px-5 py-4 overflow-hidden rounded-2xl flex gap-3 border troubleshoot-admonition dark:troubleshoot-admonition" data-callout-type="troubleshoot">
    <div className="mt-0.5 w-4">
      <svg width="14" height="14" viewBox="0 0 640 640" fill="currentColor" className="w-4 h-4" aria-label="Troubleshoot">
        <path d="M541.4 162.6C549 155 561.7 156.9 565.5 166.9C572.3 184.6 576 203.9 576 224C576 312.4 504.4 384 416 384C398.5 384 381.6 381.2 365.8 376L178.9 562.9C150.8 591 105.2 591 77.1 562.9C49 534.8 49 489.2 77.1 461.1L264 274.2C258.8 258.4 256 241.6 256 224C256 135.6 327.6 64 416 64C436.1 64 455.4 67.7 473.1 74.5C483.1 78.3 484.9 91 477.4 98.6L388.7 187.3C385.7 190.3 384 194.4 384 198.6L384 240C384 248.8 391.2 256 400 256L441.4 256C445.6 256 449.7 254.3 452.7 251.3L541.4 162.6z" />
      </svg>
    </div>
    <div className="prose min-w-0 w-full">{children}</div>
  </div>;

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 your application's AI workflows 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.

<Tip>
  Prefer working on the command line? Use the [Anaconda AI CLI](/docs/cli-reference/anaconda-ai/getting-started) to manage your servers.
</Tip>

## Understanding the API server

The API server is the core component of Anaconda Desktop that enables you to interact with your locally downloaded LLMs through API calls. Access the API server by selecting <Icon icon="server" iconType="regular" /> **Model Servers** from the left-hand navigation.

<Tip>
  Hover over a <Icon icon="circle-info" iconType="regular" /> tooltip to view information about the API Server's configurable <Tooltip tip="Model parameters are the weights and biases it learns during training. The more parameters a model has, the better its ability to learn, but the more tightly it will conform to its training data.">parameters</Tooltip>.
</Tip>

### 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 remains within the same machine, keeping your data secure and private. This is common for developing applications.

<Tip>
  `localhost` and `127.0.0.1` are semantically identical.
</Tip>

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 (that is, 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/).

<Tip>
  Your base URL for communicating with the API server combines the server address and port number from the **Model Servers** page. For example, `http://localhost:8080/`.
</Tip>

### API key

Establish an API key that must be passed as an `Authorization: Bearer` token in the header of requests made to your server. You can choose any unique string you want as the API key, but the value you enter on the **Model Servers** page must match what you use in the `Authorization: Bearer` header.

<Accordion title="Testing your API key">
  To test an API key:

  1. Select <Icon icon="server" iconType="regular" /> **Model Servers** from the left-hand navigation.

  2. Enter your API key in the **API Key** field.

  3. [Load a model into the API server](#loading-a-model).

       <Note>
         You must use a `text-generation` type model.
       </Note>

  4. Open [Anaconda Prompt](/docs/reference/glossary#anaconda-prompt) (Terminal on macOS/Linux) and run the following command:

       <CodeGroup>
         ```sh Anaconda Prompt theme={null}
         curl --request POST ^
             --url http://localhost:8080/completion ^
             --header 'Authorization: Bearer <API_KEY>' ^
             --header "Content-Type: application/json" ^
             --data '{
                 "prompt": "Hello, how are you?"
             }'
         ```

         ```sh Anaconda PowerShell Prompt theme={null}
         curl --request POST `
             --url http://localhost:8080/completion `
             --header 'Authorization: Bearer <API_KEY>' `
             --header "Content-Type: application/json" `
             --data '{
                 "prompt": "Hello, how are you?"
             }'
         ```

         ```sh Terminal  theme={null}
         curl --request POST \
             --url http://localhost:8080/completion \
             --header 'Authorization: Bearer <API_KEY>' \
             --header "Content-Type: application/json" \
             --data '{
                 "prompt": "Hello, how are you?"
             }'
         ```
       </CodeGroup>

       <Comments>
         Replace \<API\_KEY> with the value you entered on the **Model Servers** page.
       </Comments>

  This command will reach out to the local API server's `/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.
</Accordion>

### Loading a model

Use the **Model** dropdown to select the model you want to load into the API server and the **File** dropdown to choose the quantization level to interact with.

<Frame>
  <img src="https://mintcdn.com/anaconda-29683c67/mqCqnMn3Be56PnQf/images/desktop_server_model.png?fit=max&auto=format&n=mqCqnMn3Be56PnQf&q=85&s=5f243f6dc716a23afa5d02cb34099d2b" alt="Servers page showing model and file dropdown buttons" width="3460" height="1652" data-path="images/desktop_server_model.png" />
</Frame>

<Troubleshoot>
  <TroubleshootTitle>
    ### Model won't load (exit code 3221225781)
  </TroubleshootTitle>

  <TroubleshootCause>
    The model you are trying to load requires a C++ runtime environment, but Microsoft does not include one in their operating systems by default.
  </TroubleshootCause>

  <TroubleshootSolution>
    Install the Latest [Microsoft Visual C++ Redistributable Version](https://learn.microsoft.com/en-us/cpp/windows/latest-supported-vc-redist?view=msvc-170#latest-microsoft-visual-c-redistributable-version).
  </TroubleshootSolution>
</Troubleshoot>

## Starting the server

To start the API server, click <Icon icon="play" iconType="solid" /> **Start**.

<Frame>
  <img src="https://mintcdn.com/anaconda-29683c67/mqCqnMn3Be56PnQf/images/desktop_server_start.png?fit=max&auto=format&n=mqCqnMn3Be56PnQf&q=85&s=d819f7978e4bcbc33f828a7a3b11ea66" alt="Servers page showing start button" width="3460" height="1602" data-path="images/desktop_server_start.png" />
</Frame>

To stop the API server, click <Icon icon="stop" iconType="light" /> **Stop**.

## Viewing API server logs

The API server records all incoming traffic for the currently running server and displays relevant information in the **Server Logs**. To view your server logs, click **Logs** on the **Model Servers** page.

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.

Once the server is stopped, the log is saved to the following location 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:

<Tabs>
  <Tab title="Windows">
    For Windows machines, you can find the logs here: `C:\Users\<USERNAME>\AppData\Roaming\anaconda-desktop\logs`
  </Tab>

  <Tab title="macOS">
    For macOS machines, you can find the logs here: `~/Library/Logs/anaconda-desktop/`
  </Tab>
</Tabs>

## Connecting applications to servers

Connecting your application to a model server requires setting two environment variables: the `base_url`, which directs your API calls to the server hosting your model, and the `api_key`, which authenticates your requests.

Follow the steps below to find these variables' values and save them as environment variables in your application.

<Steps>
  <Step title="Configure your server">
    From the **Model Servers** page:

    1. Note your **Server Address** and **Server Port** (the default is `http://localhost:8080`). This will be your `base_url`.
    2. (Optional) Set an **API Key**. You can use any unique string value. If you don't set an API key, you don't need to provide an `api_key` in your application.
    3. [Load a model](#loading-a-model) and click <Icon icon="play" iconType="solid" /> **Start**.
  </Step>

  <Step title="Configure your application">
    Set your credentials as environment variables:

    <CodeGroup>
      ```powershell Windows Powershell theme={null}
      $env:MODEL_SERVER_BASE_URL="<BASE_URL>"
      $env:ANACONDA_API_KEY="<API_KEY>"
      ```

      ```bash macOS/ Linux terminal theme={null}
      export MODEL_SERVER_BASE_URL="<BASE_URL>"
      export ANACONDA_API_KEY="<API_KEY>"
      ```
    </CodeGroup>

    <Comments>
      Replace \<BASE\_URL> with the server address and port you copied from the **Model Servers** page.<br />
      Replace \<API\_KEY> with the API key you created on the **Model Servers** page.
    </Comments>

    <Note>
      Setting environment variables is considered a best practice for security. However, for local testing and development, you can add the credentials directly in your code instead.

      ***

      Environment variables set directly in the terminal will only last as long as the terminal session is active. To make these environment variables permanent, follow your system's standard process for persisting environment variables.
    </Note>
  </Step>
</Steps>

### OpenAI API compatibility

Anaconda Desktop model servers are OpenAI API compatible for chat completions. See OpenAI's [Chat Completion API documentation](https://developers.openai.com/api/reference/chat-completions/overview) for more information on making chat completion requests.

<Note>
  Make sure the model server is [running](#starting-the-server) before making API requests.

  ***

  The `model` parameter is required but can be left as an empty string since the model is already loaded on the server.
</Note>

```python theme={null}
from openai import OpenAI
import os

# Initialize client with environment variables
client = OpenAI(
    base_url=os.environ.get('MODEL_SERVER_BASE_URL'),
    api_key=os.environ.get('ANACONDA_API_KEY')
    )

# Make a chat completion request example
response = client.chat.completions.create(
    model="",
    messages=[{"role": "user", "content": "Hello!"}],
    max_completion_tokens=150,
    temperature=0.7
)

print(response.choices[0].message.content)
```

<Comments>
  If you did not set an API key, change `api_key=os.environ.get('ANACONDA_API_KEY')` to `api_key=""`.
</Comments>
