Notebooks
E
Elastic
Inference Alibabacloud Ai Search

Inference Alibabacloud Ai Search

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIintegrationschatlogvectordatabasenotebooksPythonsearchgenaistackvectoralibabacloud-ai-searchelasticsearch-labslangchainapplications

Semantic Search using the Inference API with the AlibabaCloudSearch service

Learn how to use the Inference API for semantic search.

Requirements

For this example, you will need:

  • An Elastic deployment:
  • Elasticsearch 8.16 or above
  • A valid API key for the AlibabaCloud AI Search is required to use the Inference API with the AlibabaCloud AI Search service.

Install packages and connect with Elasticsearch Client

To get started, we'll need to connect to our Elastic deployment using the Python client (version 8.15.0 or above).

First we need to pip install the following packages:

  • elasticsearch
[ ]

Next, we need to import the modules we need.

🔐 NOTE: getpass enables us to securely prompt the user for credentials without echoing them to the terminal, or storing it in memory.

[6]

Now we can instantiate the Python Elasticsearch client.

First we prompt the user for their user name, password and elastic host. Then we create a client object that instantiates an instance of the Elasticsearch class.

[7]

Confirm that the client has connected with this test:

[ ]

Refer to the documentation to learn how to connect to a self-managed deployment.

Read this page to learn how to connect using API keys.

Create the inference endpoint

Let's create the inference endpoint by using the Create inference API.

You'll need a valid API key for AlibabaCloud AI Search for this. You can create one by referring to the API keys management.

[ ]

Create an ingest pipeline with an inference processor

Create an ingest pipeline with an inference processor by using the put_pipeline method. Reference the inference endpoint created above as the model_id to infer against the data that is being ingested in the pipeline.

[10]
ObjectApiResponse({'acknowledged': True})

Let's note a few important parameters from that API call:

  • inference: A processor that performs inference using a machine learning model.
  • model_id: Specifies the ID of the inference endpoint to be used. In this example, the model ID is set to cohere_embeddings.
  • input_output: Specifies input and output fields.
  • input_field: Field name from which the dense_vector and sparse_vectorrepresentation is created.
  • output_field: Field name which contains inference results.

Create index

The mapping of the destination index – the index that contains the embeddings and sparse_embeddings that the model will create based on your input text – must be created. The destination index must have a field with the dense_vector field type and a field with the sparse_vector field type to index the output of the AlibabaCloud AI Search model.

Let's create an index named alibaba-text-embeddings-notebook-test with the mappings we need.

[11]
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'alibaba-text-embeddings-notebook-test'})

Insert Documents

[12]
Done indexing documents into `alibaba-text-embeddings-notebook-test` index!

Semantic search

knn search

[14]
Score: 0.6912112
Title: Pulp Fiction
Plot: The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.

Score: 0.66019344
Title: The Usual Suspects
Plot: A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which began when five criminals met at a seemingly random police lineup.

Score: 0.64070797
Title: The Dark Knight
Plot: When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.

sparse vector search

[15]
Score: 0.1208359
Title: The Godfather
Plot: An organized crime dynasty's aging patriarch transfers control of his clandestine empire to his reluctant son.

Score: 0.050445557
Title: Goodfellas
Plot: The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen Hill and his mob partners Jimmy Conway and Tommy DeVito in the Italian-American crime syndicate.

NOTE: The value of model_id in the query_vector_builder and the value of inference_id in the sparse_vector must match the value of inference_id you created in the first step.