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

# Query Relevant Chunks

> ### Description
Query a knowledge base for text chunks that are most semantically relevant to the query.

### Details
Given a query expressed as an embedding, this API runs a similarity search amongst the         embeddings indexed in the knowledge base to find the most relevant chunk embeddings. To use         this API, specify the `knowledge_base_id` of the knowledge base you want to query, pass in         the natural language `query` that you want to search for, specify the value `top_k`,         which is the number of similar chunks that will be returned, and specify whether you want         the returned chunks to `include_embeddings`.

Similarity search is used to efficiently find, retrieve, and rank chunks based on their         similarity to a given query, which is also expressed as an embedding. Similarity scores (         using the cosine similarity metric) are calculated between each chunk embedding and the         embedded query, and the chunks are ranked based on similarity score. The top-ranked chunks         are returned as the query results.

We are using the Hierarchical Navigable Small World (HNSW) algorithm to perform a k nearest         neighbors search in the vector space. This algorithm returns an estimate of the best k         nearest neighbors and is optimized for datasets with hundreds of thousands of vectors. You         can read more about the specifics of this algorithm [here](         https://opensearch.org/docs/1.0/search-plugins/knn/approximate-knn/).

#### Backwards Compatibility
V2 and V1 Knowledge Bases are entirely separate and not backwards compatible. Users who         have existing V1 knowledge bases will need to migrate their data to V2 knowledge bases.



## OpenAPI

````yaml https://app.stainlessapi.com/api/spec/documented/sgp/openapi.yml post /v4/knowledge-bases/{knowledge_base_id}/query
openapi: 3.1.0
info:
  title: EGP API V4
  description: >-
    This is the parent API for all EGP APIs. If you are looking for the EGP API,
    please go to https://api.egp.scale.com/docs.
  contact:
    name: Scale Generative AI Platform
    url: https://scale.com/genai-platform
  version: 0.1.0
servers:
  - url: https://api.egp.scale.com
security: []
tags:
  - name: Models
    description: Model API.
paths:
  /v4/knowledge-bases/{knowledge_base_id}/query:
    post:
      tags:
        - Knowledge Bases
      summary: Query Relevant Chunks
      description: >-
        ### Description

        Query a knowledge base for text chunks that are most semantically
        relevant to the query.


        ### Details

        Given a query expressed as an embedding, this API runs a similarity
        search amongst the         embeddings indexed in the knowledge base to
        find the most relevant chunk embeddings. To use         this API,
        specify the `knowledge_base_id` of the knowledge base you want to query,
        pass in         the natural language `query` that you want to search
        for, specify the value `top_k`,         which is the number of similar
        chunks that will be returned, and specify whether you want         the
        returned chunks to `include_embeddings`.


        Similarity search is used to efficiently find, retrieve, and rank chunks
        based on their         similarity to a given query, which is also
        expressed as an embedding. Similarity scores (         using the cosine
        similarity metric) are calculated between each chunk embedding and
        the         embedded query, and the chunks are ranked based on
        similarity score. The top-ranked chunks         are returned as the
        query results.


        We are using the Hierarchical Navigable Small World (HNSW) algorithm to
        perform a k nearest         neighbors search in the vector space. This
        algorithm returns an estimate of the best k         nearest neighbors
        and is optimized for datasets with hundreds of thousands of vectors.
        You         can read more about the specifics of this algorithm
        [here](        
        https://opensearch.org/docs/1.0/search-plugins/knn/approximate-knn/).


        #### Backwards Compatibility

        V2 and V1 Knowledge Bases are entirely separate and not backwards
        compatible. Users who         have existing V1 knowledge bases will need
        to migrate their data to V2 knowledge bases.
      operationId: POST-V4-/v2/knowledge-bases/{knowledge_base_id}/query
      parameters:
        - name: knowledge_base_id
          in: path
          required: true
          schema:
            type: string
            title: Knowledge Base Id
        - name: x-selected-account-id
          in: header
          required: false
          schema:
            anyOf:
              - type: string
              - type: 'null'
            title: Account ID Header
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QueryKnowledgeBaseRequestV2'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/QueryKnowledgeBaseResponseV2'
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
      security:
        - APIKeyHeader: []
      x-codeSamples:
        - lang: Python
          source: |-
            import os
            from scale_gp import SGPClient

            client = SGPClient(
                api_key=os.environ.get("SGP_API_KEY"),  # This is the default and can be omitted
            )
            response = client.knowledge_bases.query(
                knowledge_base_id="knowledge_base_id",
                query="query",
                top_k=1,
            )
            print(response.request_id)
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/stainless-sdks/sgp-go\"\n\t\"github.com/stainless-sdks/sgp-go/option\"\n)\n\nfunc main() {\n\tclient := sgp.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\tresponse, err := client.KnowledgeBases.Query(\n\t\tcontext.TODO(),\n\t\t\"knowledge_base_id\",\n\t\tsgp.KnowledgeBaseQueryParams{\n\t\t\tQuery: sgp.F(\"query\"),\n\t\t\tTopK:  sgp.F(int64(1)),\n\t\t},\n\t)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n\tfmt.Printf(\"%+v\\n\", response.RequestID)\n}\n"
components:
  schemas:
    QueryKnowledgeBaseRequestV2:
      properties:
        query:
          type: string
          title: Query
          description: >-
            The natural language query to be answered by referencing the data
            ingested into the knowledge base
        top_k:
          type: integer
          maximum: 10000
          exclusiveMinimum: 0
          title: Top K
          description: >-
            Number of chunks to return. Must be greater than 0 if specified. If
            not specified, all chunks will be returned.
        include_embeddings:
          type: boolean
          title: Include Embeddings
          description: Whether or not to include the embeddings for each chunk
          default: true
        metadata_filters:
          title: Metadata Filters
          description: Optional filter by metadata fields, encoded as a JSON object
          additionalProperties: true
          type: object
        wildcard_filters:
          title: Wildcard Filters
          description: >-
            Optional wildcard filter for fields. Only fields that are part of
            the metadata will be accepted.
          additionalProperties:
            $ref: '#/components/schemas/QueryWildcardFilterItem'
          type: object
        verbose:
          type: boolean
          title: Verbose
          description: Enable or disable verbose logging
          default: false
      type: object
      required:
        - query
        - top_k
      title: QueryKnowledgeBaseRequestV2
    QueryKnowledgeBaseResponseV2:
      properties:
        chunks:
          items:
            $ref: '#/components/schemas/ChunkV2'
          type: array
          title: Chunks
          description: >-
            An ordered list of the k most similar chunks and their similarity
            scores from most to least similar
        started_at:
          title: Started At
          description: Timestamp at which the query was begun by the server.
          type: string
          format: date-time
        completed_at:
          title: Completed At
          description: Timestamp at which the query was completed by the server.
          type: string
          format: date-time
        request_id:
          title: Request Id
          description: query request ID for verbose logging
          type: string
      type: object
      required:
        - chunks
      title: QueryKnowledgeBaseResponseV2
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    QueryWildcardFilterItem:
      properties:
        value:
          type: string
          title: Value
          description: Wildcard filter string
        case_insensitive:
          type: boolean
          title: Case Insensitive
          description: Check case on wildcard string
          default: false
      type: object
      required:
        - value
      title: QueryWildcardFilterItem
    ChunkV2:
      properties:
        chunk_id:
          type: string
          title: Chunk Id
          description: The unique ID of the chunk with embedding
        text:
          type: string
          title: Text
          description: The text associated with the chunk
        embedding:
          title: Embedding
          description: The vector embedding of the text associated with the chunk
          items:
            type: number
          type: array
        metadata:
          title: Metadata
          description: >-
            Any additional key value pairs of information stored by you on the
            chunk with embedding
          default: {}
          additionalProperties: true
          type: object
        user_supplied_metadata:
          title: User Supplied Metadata
          description: >-
            Any additional key value pairs of information returned from the
            custom chunking.
          default: {}
          additionalProperties: true
          type: object
        attachment_url:
          title: Attachment Url
          description: Original attachment URL from which this chunk got its data from
          type: string
        title:
          title: Title
          description: Title for this chunk, for example the file name
          type: string
        score:
          type: number
          title: Score
          description: >-
            A number between 0 and 1 representing how similar a chunk's
            embedding is to the query embedding. Higher numbers mean that this
            chunk with embedding is more similar.
      type: object
      required:
        - chunk_id
        - text
        - score
      title: ChunkV2
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
          additionalProperties: true
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      in: header
      name: x-api-key

````