Skip to content

Tender Model

Documentation for the Tender Class

Overview

The Tender class provides functionalities for managing tender documents in a MongoDB database. It includes methods for creating, retrieving, updating, and deleting tenders, as well as for performing semantic searches using embeddings from OpenAI’s API.

Class Methods

  1. create(tender_obj)

    • Creates a new tender document in the MongoDB database.
    • Parameters:
      • tender_obj: A dictionary containing the tender details.
    • Returns:
      • The result of the MongoDB insert operation.
  2. find_all(query={}, skip=0, limit=0, sort=None)

    • Retrieves tender documents based on a query.
    • Parameters:
      • query: MongoDB query to filter the documents.
      • skip: Number of documents to skip.
      • limit: Maximum number of documents to return.
      • sort: Sorting order.
    • Returns:
      • A list of tenders that match the query.
  3. find_by_id(id)

    • Retrieves a tender document by its MongoDB ObjectId.
    • Parameters:
      • id: The ObjectId of the tender document.
    • Returns:
      • The tender document if found.
  4. update_by_id(id, updated_tender_obj)

    • Updates a tender document by its MongoDB ObjectId.
    • Parameters:
      • id: The ObjectId of the tender document.
      • updated_tender_obj: A dictionary containing the fields to update.
    • Returns:
      • The result of the MongoDB update operation.
  5. delete_by_id(id)

    • Deletes a tender document by its MongoDB ObjectId.
    • Parameters:
      • id: The ObjectId of the tender document.
    • Returns:
      • The result of the MongoDB delete operation.
  6. count_tenders(filters)

    • Counts the number of tender documents based on filter conditions.
    • Parameters:
      • filters: MongoDB filters to apply.
    • Returns:
      • The count of tender documents that match the filters.
  7. get_embedding(query)

    • Retrieves an embedding vector for a given query string using OpenAI’s API.
    • Parameters:
      • query: The text string to get embedding for.
    • Returns:
      • The embedding vector.
  8. find_similar_documents(embedding, limit=10)

    • Finds documents similar to a given embedding.
    • Parameters:
      • embedding: The embedding vector to compare against.
      • limit: The number of similar documents to return.
    • Returns:
      • A list of documents that are similar to the given embedding.
  9. semantic_search(search_query)

    • Performs a semantic search to find tenders similar to the given search query.
    • Parameters:
      • search_query: The search query string.
    • Returns:
      • A list of similar tenders.

Usage Example

# Initialize the Tender class
tender_instance = Tender()
# To create a new tender
new_tender = {'agency': 'Dept. of Construction', 'amount': 50000, 'description': 'Construction project'}
tender_instance.create(new_tender)
# To find all tenders
all_tenders = tender_instance.find_all()
# To update a tender by id
tender_id = '507f1f77bcf86cd799439011' # Example ObjectId
tender_instance.update_by_id(tender_id, {'amount': 60000})
# To delete a tender by id
tender_instance.delete_by_id(tender_id)
# To perform a semantic search
similar_tenders = tender_instance.semantic_search("bridge construction project")

Key Features

  • Comprehensive Tender Management: Supports all CRUD operations for tender documents.
  • Semantic Search: Leverages machine learning to perform advanced searches based on document similarity.
  • Resilience: Implements error handling and retries for external API calls.