Skip to main content

A community-maintained SDK (Standard Developer Kit) for XAI Grok models

Project description

Screenshot 2025-01-05 at 4 45 45 PM

Grok Community XDK

A community-maintained SDK (Standard Developer Kit) for XAI Grok models supporting usage with the Python Programming Language. This library provides convenient access to the xAI REST API from Python 3.11+ applications, with type definitions for all request parameters and response fields.

Installation

pip install xai-grok

Requirements

  • Python 3.11 or higher
  • pydantic
  • requests

Usage

from xai_grok import Grok

client = Grok(
    api_key="your-api-key-here"
)

# Example: Create a chat completion
response = client.chat_completions(
    ChatRequest(
        messages=[
            {"role": "user", "content": "Tell me about AI"}
        ],
        model="model-name-here"
    )
)
print(response.choices[0].message.content)

Available Endpoints

API Key Operations

  • api_key() - Retrieve information about the current API key

Chat and Completion Operations

  • chat_completions(request: ChatRequest) - Create chat completions

    • Input: ChatRequest object containing messages and model settings
    • Throws: InvalidRequestError, IncompleteRequestError
  • complete(request: CompleteRequest) - Generate completions

    • Input: CompleteRequest object with prompt and settings
    • Throws: InvalidRequestError, IncompleteRequestError
  • completions(request: SampleRequest) - Alternative completion endpoint

    • Input: SampleRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Embedding Operations

  • embedding_model(model_id: str) - Get details of a specific embedding model

    • Input: Model ID string
    • Throws: ModelNotFoundError
  • embedding_models() - List all available embedding models

  • embeddings(request: EmbeddingRequest) - Generate embeddings

    • Input: EmbeddingRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Language Model Operations

  • language_model(model_id: str) - Get details of a specific language model

    • Input: Model ID string
    • Throws: ModelNotFoundError
  • language_models() - List all available language models

Message Operations

  • messages(request: MessageRequest) - Send messages
    • Input: MessageRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Model Management

  • models() - List all available models

  • model(model_id: str) - Get details of a specific model

    • Input: Model ID string
    • Throws: ModelNotFoundError

Text Operations

  • tokenize_text(request: TokenizeTextRequest) - Tokenize input text
    • Input: TokenizeTextRequest object
    • Throws: InvalidRequestError

Request and Response Types

All request and response types are Pydantic models, providing type safety and validation. Refer to the schemas module for detailed type definitions.

Base URL

The API uses https://api.x.ai as the base URL for all endpoints.

Authentication

The API requires an authorized API key. Authentication-related errors are handled by specific error types:

  • NoAPIKeyProvidedError: Raised when no API key is provided in the Authorization header
  • InvalidAPIKeyProvidedError: Raised when an incorrect API key is provided

To avoid these errors:

  1. Obtain a valid API key from console.x.ai
  2. Include it in the client initialization:
client = Grok(api_key="your-api-key-here")

Note on Responses

All responses are parsed into their corresponding Pydantic models, providing type-safe access to response data. If the API response cannot be parsed into the expected type, a FailedToParseResponseError will be raised.

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

xai_grok-0.0.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

xai_grok-0.0.1-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file xai_grok-0.0.1.tar.gz.

File metadata

  • Download URL: xai_grok-0.0.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for xai_grok-0.0.1.tar.gz
Algorithm Hash digest
SHA256 81cf1753abff558b5fbe2e57dcfe66d00479c68ae958bb3a66d1fcaefa8f9794
MD5 f202fc5bc768fe8b77a2a89829b1b6f7
BLAKE2b-256 63f97298acdc2fd3bcc93338b52f256ac56c7b34d3447fe9fe31e071a62850c5

See more details on using hashes here.

File details

Details for the file xai_grok-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: xai_grok-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for xai_grok-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 78eaacdc959bd6b9bb6403f90d70654c97f0f78b7f5fbb2d44c176f415aedfb8
MD5 db786862f50e0f65a1e24cbe0f0afe97
BLAKE2b-256 b5ca24af581d53a1764d5a06e6db1e85f7533f0dee65490913f052000fd29a2f

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