Skip to main content

No project description provided

Project description

ContextForce SDK Documentation

Overview

The ContextForceClient class provides a Python interface to interact with the ContextForce API. Below are the available methods, how to use them, and details on the headers automatically set by the SDK.

Installation

pip install contextforce-python

Initialization

Example

from contextforce_python import ContextForceClient

# api key is not required for free users. Get it when you want to have more free token and better rate limit
client = ContextForceClient(api_key='your_api_key')

Methods

1. extract_content

Extracts content from a given page URL or list of URLs. The content can be returned in Markdown or JSON format.

Parameters

  • urls: A string (single URL) or a list of URLs.
  • result_format: The format of the result, either 'markdown' (default) or 'json'.
  • include_links: Boolean to include links in the output (default False).
  • include_images: Boolean to include images in the output (default False).

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Include-Links: Set to 'true' if include_links is True.
  • CF-Include-Images: Set to 'true' if include_images is True.

Example Usage

# Convert an online article into markdown
result = client.extract_content("https://www.nbcnews.com/select/shopping/best-puppy-food-rcna151536")

2. extract_pdf

Extracts content from a PDF URL or file content. The content can be returned in Markdown or JSON format.

Parameters

  • pdf_source: A string (PDF URL) or bytes (PDF file content).
  • result_format: The format of the result, either 'markdown' (default) or 'json'.
  • model: Optional model to use, e.g., 'gpt-4o-mini', 'claude-3.5'.
  • openai_api_key: Optional OpenAI API key if model is 'gpt-4o-mini'.
  • claude_api_key: Optional Claude API key if model is 'claude-3.5'.

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Model: Set to the model name if model is specified.
  • CF-OpenAI-API-Key: Set to the OpenAI API key if model is 'gpt-4o-mini'.
  • CF-Claude-API-Key: Set to the Claude API key if model is 'claude-3.5'.
  • Content-Type: Set to 'multipart/form-data' for file uploads.
  • CF-Content-Type: Set to 'application/pdf' when uploading PDF content.

Example Usage

# Convert the PDF to markdown without using OCR LLM feature
result = client.extract_pdf("https://arxiv.org/pdf/2210.05189")

# Convert the PDF to markdown and use gpt-4o-mini to handle the OCR for pages with special elements like formula, table and image
result = client.extract_pdf("https://arxiv.org/pdf/2210.05189", model="gpt-4o-mini", openai_api_key="sk-xxxxxx")

3. extract_product

Extracts product information from a given product page URL or list of URLs. The content is returned in JSON format by default.

Parameters

  • urls: A string (single URL) or a list of URLs.
  • result_format: The format of the result, either 'json' (default) or 'markdown'.
  • include_reviews: Optional boolean to include product reviews in the output.

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Include-Reviews: Set to 'true' if include_reviews is True.

Example Usage

# Extract Amazon product info and return the result in json 
result = client.extract_product("https://www.amazon.com/dp/B001VIWHMY")

4. search_google

Performs a Google search based on a query.

Parameters

  • query: The search query.
  • result_format: The format of the result, either 'json' (default) or 'markdown'.
  • follow_links: Optional boolean to follow links on the search results (default True).
  • top_n: Optional integer to specify the number of top pages to crawl if follow_links is True (default 5).

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Follow-Links: Set to 'true' if follow_links is True.
  • CF-Top-N: Set to the value of top_n.

Example Usage

# Get Google SERP result only
result = client.search_google("best dog food")

# Get Google SERP result and convert the top N pages into markdown
result = client.search_google("best dog food", result_format="json", follow_links=True, top_n=5)

5. search_amazon

Performs an Amazon search based on a query.

Parameters

  • query: The search query.
  • result_format: The format of the result, either 'json' (default) or 'markdown'.
  • follow_links: Optional boolean to follow links on the search results (default True).
  • top_n: Optional integer to specify the number of top pages to crawl if follow_links is True (default 5).

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Follow-Links: Set to 'true' if follow_links is True.
  • CF-Top-N: Set to the value of top_n.

Example Usage

# Get the amazon search result
result = client.search_amazon("dog food")

# Get the amazon search result and follow the top N products to get the detail info in json
result = client.search_amazon("dog food", follow_links=True, top_n=5)

6. search_youtube

Performs a YouTube search based on a query.

Parameters

  • query: The search query.
  • result_format: The format of the result, either 'json' (default) or 'markdown'.
  • follow_links: Optional boolean to follow links on the search results (default True).
  • top_n: Optional integer to specify the number of top pages to crawl if follow_links is True (default 5).

Headers Set by SDK

  • Authorization: Set to Bearer {api_key}.
  • Accept: Set to 'application/json' if result_format is 'json'.
  • CF-Follow-Links: Set to 'true' if follow_links is True.
  • CF-Top-N: Set to the value of top_n.

Example Usage

# Get the youtube search result based on the keyword
result = client.search_youtube("how to train my dog")

# Get the youtube search result and follow the top N links to get the video info
result = client.search_youtube("how to train my dog", follow_links=True, top_n=5)

This documentation provides detailed information on how to use each function within the ContextForceClient SDK and the headers automatically set by the SDK for each function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

contextforce_python-0.2.9.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

contextforce_python-0.2.9-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file contextforce_python-0.2.9.tar.gz.

File metadata

  • Download URL: contextforce_python-0.2.9.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/22.2.0

File hashes

Hashes for contextforce_python-0.2.9.tar.gz
Algorithm Hash digest
SHA256 07893cc7ed88247cee659ccfecf6997b5438e77174a39033538bc7372e55b05f
MD5 28a32575d85dec0df5dc16a552292e85
BLAKE2b-256 1f5e93ffc9128fb67493ae26022ceece44d31a3644fcb3fa5e430c04a231afce

See more details on using hashes here.

File details

Details for the file contextforce_python-0.2.9-py3-none-any.whl.

File metadata

File hashes

Hashes for contextforce_python-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 7a41d36128c13aa1745b5fba24e0956578e4c8b1ca31fc0b6cab143fb58f597c
MD5 f7f83e8ee43fa6bab915024c5b503f69
BLAKE2b-256 035707fe312ea26236b84b4807cd75c535ae9de0087088491239506ae8506996

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page