Elasticsearch open inference API for Google AI Studio

Elasticsearch open inference API adds support for Google AI Studio

Elasticsearch's open inference API supports the Gemini Developer API. When using Google AI Studio, developers can chat with data in their Elasticsearch indexes, run experiments, and build applications using Google Cloud’s models, such as Gemini 1.5 Flash. AI Studio is where Google releases the latest models from Google DeepMind and is the fastest way to start building with Gemini.

In this blog, we will create a new Google AI Studio project, create an Elasticsearch inference API endpoint to use Gemini 1.5 Flash, and implement a sample chat app to estimate how many ducks fit on an American football field! (Because why not?)

AI Studio API key

To get started, we need to create an API key for AI Studio. Head over to ai.google.dev/aistudio and click “Sign In to Google AI Studio.”

If you aren’t already logged in, you will be prompted to do so. Once logged in, you are presented with two options: using AI Studio in the browser to test prompts with Gemini or creating an API key. We will create an API key to allow Elasticsearch to connect to AI Studio.

You are prompted to accept Google Cloud’s terms and conditions the first time you create an API key. If you use a personal account, you will be given the option to create an API key in a new project. You may not see that option if you use an enterprise account, depending on your access roles. Either way, you can select an existing project to create the key.

Select an existing project or create a new project.

Copy the generated API key someplace safe for use in the next section.

Elasticsearch Inference API

We will use Python to configure the Inference API to connect to Google AI Studio and test the chat completion with Gemini.

Create the Inference Endpoint

Create an Elasticsearch connection.

es = Elasticsearch(
    elasticsearch_url, 
    api_key=elasticsearch_api_key
)

Create the Inference Endpoint to connect to Google AI Studio. For this blog, we will use the Gemini 1.5 Flash model. For a list of available models, consult the Gemini docs.

inference_id = "google_ai_studio"

model_config = {
    "service": "googleaistudio",
    "service_settings": {
        "api_key": google_ai_studio_api_key,
        "model_id": google_ai_studio_model_id
    }
}

# Create the endpoint
create_endpoint = es.inference.put(
      inference_id=inference_id, 
      task_type="completion",
      body=model_config
  )

Confirm the endpoint was created.

inf_info = es.inference.get(inference_id=inference_id)
print(inf_info.body)

The output should be similar to:

{'endpoints': [{'model_id': 'google_ai_studio',
   'inference_id': 'google_ai_studio',
   'task_type': 'completion',
   'service': 'googleaistudio',
   'service_settings': {'model_id': 'gemini-1.5-flash',
    'rate_limit': {'requests_per_minute': 360}},
   'task_settings': {}}]}

Chat time!

That's all it takes to create an Elasticsearch API Endpoint to access Google AI Studio! With that done, you can start using it.

We will ask it to estimate how many ducks fit on an American football field. Why? Why not.

response = es.inference.inference(     
    inference_id=inference_id, 
    body={"input": "hello"}
  )

print(response.body['completion'][0]['result'])

Response

It's impossible to give an exact number of ducks that could fit on an American football field without some crucial information:

* **Duck size:**  Ducks come in various sizes. Are we talking about mallards, Muscovy ducks, or something else? 
* **Duck behavior:** Ducks aren't neatly arranged like bricks. They'll move around, jostle, and likely try to escape. This makes packing them tightly impossible.
* **Field conditions:** Are we talking about a dry field, or one with mud and water? This impacts how ducks can stand and move.

**Here's a more helpful approach:**

* **Area:** An American football field is 100 yards long and 53 1/3 yards wide, for a total area of 5,333 square yards.
* **Duck size:** Let's assume a typical mallard duck is about 2 feet long and 1 foot wide. This gives us a rough area of 2 square feet per duck.
* **Packing density:** Even if we could perfectly pack ducks, we'd need to allow for some space between them. Let's be generous and assume we can fit 4 ducks per square yard. 

**Calculation:**

* 5,333 square yards * 4 ducks/square yard = **21,332 ducks**

**However, this is a highly unrealistic estimate.** In reality, you'd likely be able to fit far fewer ducks due to their movement and the need for space.

**It's more fun to imagine the chaos of trying to fit that many ducks on a field!** 🦆🦆🦆

Simple and powerful, at the same time

With the addition of Google AI Studio, the Elastic open inference API provides access to a growing choice of powerful generative AI capabilities for developers. Google AI Studio is designed to enable simple, quick generative AI experiments to test your best ideas.

Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!

Elasticsearch is packed with new features to help you build the best search solutions for your use case. Dive into our sample notebooks to learn more, start a free cloud trial, or try Elastic on your local machine now.

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as your are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself