Metaphor Python API Client

0.1.23 · active · verified Thu Apr 16

Metaphor-python is a Python client library for interacting with the Metaphor API, a search engine optimized for Large Language Models (LLMs). It enables neural search and content retrieval, providing clean HTML content from search results. The library is currently at version 0.1.23 and is under active development, indicated by its 'Alpha' status on PyPI. While no explicit release cadence is documented, updates are released as new features and improvements to the Metaphor API are made available.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Metaphor client using an API key from an environment variable and performs a basic search query, printing the title, URL, and a snippet of the content for the top results.

import os
from metaphor_python import Metaphor

# Ensure your Metaphor API key is set as an environment variable
# e.g., export METAPHOR_API_KEY="your_metaphor_api_key_here"
api_key = os.environ.get('METAPHOR_API_KEY')

if not api_key:
    print("Error: METAPHOR_API_KEY environment variable not set.")
else:
    try:
        metaphor = Metaphor(api_key=api_key)
        # Perform a search
        results = metaphor.search("latest developments in AI agents", num_results=3, use_autoprompt=True)

        if results.contents:
            print("Search Results:")
            for i, item in enumerate(results.contents):
                print(f"---\nResult {i+1}:\nTitle: {item.title}\nURL: {item.url}\nExtract: {item.extract[:200]}...")
        else:
            print("No search results found.")

    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →