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

# Search for documents

> Perform a similarity search on a Telnyx Storage Bucket, returning the most similar `num_docs` document chunks to the query.

Currently the only available distance metric is cosine similarity which will return a `distance` between 0 and 1.
The lower the distance, the more similar the returned document chunks are to the query.
A `certainty` will also be returned, which is a value between 0 and 1 where the higher the certainty, the more similar the document.
You can read more about Weaviate distance metrics here: [Weaviate Docs](https://weaviate.io/developers/weaviate/config-refs/distances)

If a bucket was embedded using a custom loader, such as `intercom`, the additional metadata will be returned in the 
`loader_metadata` field.



## OpenAPI

````yaml https://telnyx-openapi-ng.s3.us-east-1.amazonaws.com/ai/analytics.yml post /ai/embeddings/similarity-search
openapi: 3.1.0
info:
  title: Telnyx AI Analytics API
  version: 2.0.0
  description: API for AI conversations, insights, embeddings, and clusters.
  contact:
    email: support@telnyx.com
servers:
  - url: https://api.telnyx.com/v2
security:
  - bearerAuth: []
paths:
  /ai/embeddings/similarity-search:
    post:
      tags:
        - Embeddings
      summary: Search for documents
      description: >-
        Perform a similarity search on a Telnyx Storage Bucket, returning the
        most similar `num_docs` document chunks to the query.


        Currently the only available distance metric is cosine similarity which
        will return a `distance` between 0 and 1.

        The lower the distance, the more similar the returned document chunks
        are to the query.

        A `certainty` will also be returned, which is a value between 0 and 1
        where the higher the certainty, the more similar the document.

        You can read more about Weaviate distance metrics here: [Weaviate
        Docs](https://weaviate.io/developers/weaviate/config-refs/distances)


        If a bucket was embedded using a custom loader, such as `intercom`, the
        additional metadata will be returned in the 

        `loader_metadata` field.
      operationId: PostEmbeddingSimilaritySearch
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EmbeddingSimilaritySearchRequest'
        required: true
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/EmbeddingSimilaritySearchResponse'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      x-codeSamples:
        - lang: JavaScript
          source: |-
            import Telnyx from 'telnyx';

            const client = new Telnyx({
              apiKey: process.env['TELNYX_API_KEY'], // This is the default and can be omitted
            });

            const response = await client.ai.embeddings.similaritySearch({
              bucket_name: 'bucket_name',
              query: 'query',
            });

            console.log(response.data);
        - lang: Python
          source: |-
            import os
            from telnyx import Telnyx

            client = Telnyx(
                api_key=os.environ.get("TELNYX_API_KEY"),  # This is the default and can be omitted
            )
            response = client.ai.embeddings.similarity_search(
                bucket_name="bucket_name",
                query="query",
            )
            print(response.data)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/team-telnyx/telnyx-go\"\n\t\"github.com/team-telnyx/telnyx-go/option\"\n)\n\nfunc main() {\n\tclient := telnyx.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\tresponse, err := client.AI.Embeddings.SimilaritySearch(context.TODO(), telnyx.AIEmbeddingSimilaritySearchParams{\n\t\tBucketName: \"bucket_name\",\n\t\tQuery:      \"query\",\n\t})\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", response.Data)\n}\n"
        - lang: Java
          source: >-
            package com.telnyx.sdk.example;


            import com.telnyx.sdk.client.TelnyxClient;

            import com.telnyx.sdk.client.okhttp.TelnyxOkHttpClient;

            import
            com.telnyx.sdk.models.ai.embeddings.EmbeddingSimilaritySearchParams;

            import
            com.telnyx.sdk.models.ai.embeddings.EmbeddingSimilaritySearchResponse;


            public final class Main {
                private Main() {}

                public static void main(String[] args) {
                    TelnyxClient client = TelnyxOkHttpClient.fromEnv();

                    EmbeddingSimilaritySearchParams params = EmbeddingSimilaritySearchParams.builder()
                        .bucketName("bucket_name")
                        .query("query")
                        .build();
                    EmbeddingSimilaritySearchResponse response = client.ai().embeddings().similaritySearch(params);
                }
            }
        - lang: Ruby
          source: >-
            require "telnyx"


            telnyx = Telnyx::Client.new(api_key: "My API Key")


            response = telnyx.ai.embeddings.similarity_search(bucket_name:
            "bucket_name", query: "query")


            puts(response)
        - lang: PHP
          source: >-
            <?php


            require_once dirname(__DIR__) . '/vendor/autoload.php';


            use Telnyx\Client;

            use Telnyx\Core\Exceptions\APIException;


            $client = new Client(apiKey: getenv('TELNYX_API_KEY') ?: 'My API
            Key');


            try {
              $response = $client->ai->embeddings->similaritySearch(
                bucketName: 'bucket_name', query: 'query', numOfDocs: 0
              );

              var_dump($response);
            } catch (APIException $e) {
              echo $e->getMessage();
            }
        - lang: CLI
          source: |-
            telnyx ai:embeddings similarity-search \
              --api-key 'My API Key' \
              --bucket-name bucket_name \
              --query query
components:
  schemas:
    EmbeddingSimilaritySearchRequest:
      properties:
        bucket_name:
          type: string
          title: Bucket Name
        query:
          type: string
          title: Query
        num_of_docs:
          type: integer
          title: Num Of Docs
          default: 3
      type: object
      required:
        - bucket_name
        - query
      title: EmbeddingSimilaritySearchRequest
    EmbeddingSimilaritySearchResponse:
      properties:
        data:
          items:
            $ref: '#/components/schemas/EmbeddingSimilaritySearchDocument'
          type: array
          title: Data
      type: object
      required:
        - data
      title: EmbeddingSimilaritySearchResponse
    HTTPValidationError:
      title: HTTPValidationError
      type: object
      properties:
        detail:
          title: Detail
          type: array
          items:
            $ref: '#/components/schemas/ValidationError'
    EmbeddingSimilaritySearchDocument:
      properties:
        document_chunk:
          type: string
          title: Document Chunk
        distance:
          type: number
          title: Distance
        metadata:
          $ref: '#/components/schemas/EmbeddingMetadata'
      type: object
      required:
        - document_chunk
        - distance
        - metadata
      title: EmbeddingSimilaritySearchDocument
      description: |-
        Example document response from embedding service
        {
          "document_chunk": "your status? This is Vanessa Bloome...",
          "distance": 0.18607724,
          "metadata": {
            "source": "https://us-central-1.telnyxstorage.com/scripts/bee_movie_script.txt",
            "checksum": "343054dd19bab39bbf6761a3d20f1daa",
            "embedding": "openai/text-embedding-ada-002",
            "filename": "bee_movie_script.txt",
            "certainty": 0.9069613814353943,
            "loader_metadata": {}
          }
        }
    ValidationError:
      title: ValidationError
      required:
        - loc
        - msg
        - type
      type: object
      properties:
        loc:
          title: Location
          type: array
          items:
            anyOf:
              - type: string
              - type: integer
        msg:
          title: Message
          type: string
        type:
          title: Error Type
          type: string
    EmbeddingMetadata:
      properties:
        source:
          type: string
          title: Source
        checksum:
          type: string
          title: Checksum
        embedding:
          type: string
          title: Embedding
        filename:
          type: string
          title: Filename
        certainty:
          type: number
          title: Certainty
        loader_metadata:
          type: object
          title: Loader Metadata
          additionalProperties: true
      type: object
      required:
        - source
        - checksum
        - embedding
        - filename
      title: EmbeddingMetadata
  securitySchemes:
    bearerAuth:
      scheme: bearer
      type: http

````