|
| 1 | +--- |
| 2 | +applies_to: |
| 3 | + stack: ga |
| 4 | + serverless: ga |
| 5 | +navigation_title: Using OpenAI compatible models |
| 6 | +--- |
| 7 | + |
| 8 | +# Using OpenAI compatible models with the {{infer-cap}} API |
| 9 | + |
| 10 | +{{es}} enables you to use LLMs through the {{infer}} API, supporting providers such as Amazon Bedrock, Cohere, Google AI, HuggingFace, OpenAI, and more, as a service. |
| 11 | +It also allows you to use models deployed in your local environment that have an OpenAI compatible API. |
| 12 | + |
| 13 | +This page shows you how to connect local models to {{es}} using Ollama. |
| 14 | + |
| 15 | +[Ollama](https://ollama.com/) enables you to download and run LLM models on your own infrastructure. |
| 16 | +For a list of available models compatible with Ollama, refer to this [page](https://ollama.com/library). |
| 17 | + |
| 18 | +Using Ollama ensures that your interactions remain private, as the models run on your infrastructure. |
| 19 | + |
| 20 | +## Overview |
| 21 | + |
| 22 | +In this tutorial, you learn how to: |
| 23 | + |
| 24 | +* download and run Ollama, |
| 25 | +* use ngrok to expose your local web server hosting Ollama over the internet |
| 26 | +* connect your local LLM to Playground |
| 27 | + |
| 28 | +## Download and run Ollama |
| 29 | + |
| 30 | +1. [Download Ollama](https://ollama.com/download). |
| 31 | +2. Install Ollama using the downloaded file. |
| 32 | +Enable the command line tool for Ollama during installation. |
| 33 | +3. Choose a model from the [list of supported LLMs](https://ollama.com/library). |
| 34 | +This tutorial uses `llama 3.2`. |
| 35 | +4. Run the following command: |
| 36 | + ```shell |
| 37 | + ollama pull llama3.2 |
| 38 | + ``` |
| 39 | + |
| 40 | +### Test the installed model |
| 41 | + |
| 42 | +After installation, test the model. |
| 43 | + |
| 44 | +1. Run `ollama run llama3.2` and ask a question, for example, "Are you working?" |
| 45 | +If the model is installed successfully, you receive a valid response. |
| 46 | +2. When the model is running, an API endpoint is enabled by default on port `11434`. |
| 47 | +To test it, make a request to the API using the following command: |
| 48 | + ```shell |
| 49 | + curl http://localhost:11434/api/generate -d '{ |
| 50 | + "model": "llama3.2", |
| 51 | + "prompt": "What is the capital of France?" |
| 52 | + }' |
| 53 | + ``` |
| 54 | + |
| 55 | + Refer to the API [documentation](https://github.com/ollama/ollama/blob/main/docs/api.md) to learn more. |
| 56 | + The API returns a response similar to this: |
| 57 | + ```json |
| 58 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.500614Z","response":"The","done":false} |
| 59 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.519131Z","response":" capital","done":false} |
| 60 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.537432Z","response":" of","done":false} |
| 61 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.556016Z","response":" France","done":false} |
| 62 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.574815Z","response":" is","done":false} |
| 63 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.592967Z","response":" Paris","done":false} |
| 64 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.611558Z","response":".","done":false} |
| 65 | + {"model":"llama3.2","created_at":"2025-03-26T10:07:05.630715Z","response":"","done":true,"done_reason":"stop","context":[128006,9125,128007,271,38766,1303,33025,2696,25,6790,220,2366,18,271,128009,128006,882,128007,271,3923,374,279,6864,315,9822,30,128009,128006,78191,128007,271,791,6864,315,9822,374,12366,13],"total_duration":2232589542,"load_duration":1052276792,"prompt_eval_count":32,"prompt_eval_duration":1048833625,"eval_count":8,"eval_duration":130808916} |
| 66 | + ``` |
| 67 | + |
| 68 | +## Expose the endpoint using ngrok |
| 69 | + |
| 70 | +Since the created endpoint only works locally, it cannot be accessed from external services (for example, your Elastic Cloud instance). |
| 71 | +[ngrok](https://ngrok.com/) enables you to expose a local port with a public URL. |
| 72 | + |
| 73 | +::::{warning} |
| 74 | +Exposing a local endpoint to the internet can introduce security risks. Anyone with the public URL may be able to send requests to your service. Avoid exposing sensitive data or functionality, and consider using authentication or access restrictions to limit who can interact with the endpoint. |
| 75 | +:::: |
| 76 | + |
| 77 | +1. Create an ngrok account and follow the [official setup guide](https://dashboard.ngrok.com/get-started/setup). |
| 78 | +2. After installing and configuring the ngrok agent, expose the Ollama port by running: |
| 79 | + ```shell |
| 80 | + ngrok http 11434 --host-header="localhost:11434" |
| 81 | + ``` |
| 82 | + The command returns a public link that works as long as ngrok and the Ollama server are running locally: |
| 83 | + ```shell |
| 84 | + Session Status online |
| 85 | + Account [email protected] (Plan: Free) |
| 86 | + Version 3.18.4 |
| 87 | + Region United States (us) |
| 88 | + Latency 561ms |
| 89 | + Web Interface http://127.0.0.1:4040 |
| 90 | + Forwarding https://your-ngrok-endpoint.ngrok-free.app -> http://localhost:11434 |
| 91 | + |
| 92 | + |
| 93 | + Connections ttl opn rt1 rt5 p50 p90 |
| 94 | + 0 0 0.00 0.00 0.00 0.00 |
| 95 | + ``` |
| 96 | + |
| 97 | +3. Copy the ngrok-generated URL from the `Forwarding` line. |
| 98 | +4. Test the endpoint again using the new URL: |
| 99 | + ```shell |
| 100 | + curl https://your-ngrok-endpoint.ngrok-free.app/api/generate -d '{ |
| 101 | + "model": "llama3.2", |
| 102 | + "prompt": "What is the capital of France?" |
| 103 | + }' |
| 104 | + ``` |
| 105 | + The response should be similar to the previous one. |
| 106 | + |
| 107 | +## Connecting the local LLM to Playground |
| 108 | + |
| 109 | +:::{include} ../_snippets/connect-local-llm-to-playground.md |
| 110 | +::: |
| 111 | + |
| 112 | +## Using the local LLM with the {{infer}} API |
| 113 | + |
| 114 | +:::{include} ../_snippets/use-local-llm-inference-api.md |
| 115 | +::: |
| 116 | + |
| 117 | +## Further reading |
| 118 | + |
| 119 | +* [Using Ollama with the {{infer}} API](https://www.elastic.co/search-labs/blog/ollama-with-inference-api#expose-endpoint-to-the-internet-using-ngrok): A more comprehensive, end-to-end guide to using Ollama with {{es}}. |
0 commit comments