A Python client library for SeekGPT and other OpenAI-compatible LLM APIs.
Project description
SeekGPT Python Client Library
seekgpt is a Python library designed to provide a simple and unified interface for interacting with various Large Language Model (LLM) APIs, focusing on compatibility with the OpenAI API standard. It defaults to connecting to https://api.seekgpt.org/v1 but can be easily configured for other services.
Features
- Connect to the default SeekGPT API (
https://api.seekgpt.org/v1). - Connect to any OpenAI-compatible API endpoint (OpenAI, Ollama, vLLM, Anyscale, Together AI, etc.) using
SeekGPT. - Simple
chatinterface following thechat/completionsstandard. - Support for streaming responses.
- Handles API key authentication (Bearer token).
- Basic error handling and custom exceptions.
- Configurable API base URL, API key, default model, and timeout.
- Uses environment variables for configuration (
SEEKGPT_API_KEY,SEEKGPT_API_BASE, etc.).
Installation
pip install seekgpt
Quick Start
Connecting to SeekGPT (Default)
Make sure you have your SeekGPT API key set as an environment variable:
export SEEKGPT_API_KEY="your-seekgpt-api-key"
# Optional: Set a default model
# export SEEKGPT_DEFAULT_MODEL="seekgpt-model-name"
Then use the SeekGPT:
from seekgpt import SeekGPT
# Client automatically picks up SEEKGPT_API_KEY from environment
# You can also pass it directly: SeekGPTClient(api_key="your-key")
client = SeekGPT(api_key="", api_base="https://api.seekgpt.org/v1")
try:
response = client.chat(
model="SeekGPT-mini", # Or rely on SEEKGPT_DEFAULT_MODEL env var
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the capital of France?"}
]
)
print(response['choices'][0]['message']['content'])
# Streaming example
stream = client.chat(
model="SeekGPT-mini",
messages=[{"role": "user", "content": "Tell me a short story."}],
stream=True
)
print("\nStreaming response:")
for chunk in stream:
# Process each line (chunk) of the stream
# Note: Parsing SSE data might require additional logic depending on the exact format
print(chunk, end="") # Raw SSE line, may need parsing
print()
except Exception as e:
print(f"An error occurred: {e}")
Connecting to OpenAI
export OPENAI_API_KEY="your-openai-api-key"
from seekgpt import SeekGPT
# SeekGPT will fallback to OPENAI_API_KEY if SEEKGPT_API_KEY is not set
client = SeekGPT(
api_base="https://api.openai.com/v1",
default_model="gpt-4o" # Set a default model for this client instance
)
response = client.chat(
messages=[{"role": "user", "content": "Hello OpenAI!"}]
)
print(response['choices'][0]['message']['content'])
Connecting to Ollama (Local)
Ensure Ollama is running (usually at http://localhost:11434).
from seekgpt import SeekGPT, APIConnectionError
# Ollama typically doesn't require an API key for local access,
# but some libraries expect *something*, so we pass a dummy key.
# The client automatically detects localhost and won't raise an Auth error if key is None.
client = SeekGPT(
api_base="http://localhost:11434/v1",
api_key="ollama", # Dummy key often needed, but can also try api_key=None
default_model="llama3" # Specify the local model you want to use
)
try:
response = client.chat(
messages=[{"role": "user", "content": "Hi Ollama! Write a haiku about code."}]
)
print(response['choices'][0]['message']['content'])
except APIConnectionError as e:
print(f"Could not connect to Ollama. Is the server running? Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
Connecting to Other OpenAI-Compatible Services (vLLM, Anyscale, Together AI, etc.)
Set the appropriate API key environment variable (e.g., ANYSCALE_API_KEY, TOGETHER_API_KEY) or pass the api_key directly. Use the SeekGPT and provide the correct api_base for the service.
# Example for Anyscale Endpoints
# export ANYSCALE_API_KEY="your-anyscale-key"
from seekgpt import SeekGPT
client = SeekGPT(
api_base="https://api.endpoints.anyscale.com/v1",
# SeekGPT will try ANYSCALE_API_KEY if SEEKGPT_API_KEY/OPENAI_API_KEY are not set
# Or pass explicitly: api_key="your-anyscale-key"
default_model="mistralai/Mixtral-8x7B-Instruct-v0.1"
)
# ... use client.chat(...) ...
Configuration
The clients can be configured via:
- Environment Variables:
SEEKGPT_API_KEY: API Key (used bySeekGPTClientandSeekGPTpriority).SEEKGPT_API_BASE: API Base URL (used bySeekGPTifapi_baseargument is not provided). Defaults tohttps://api.seekgpt.org/v1.SEEKGPT_DEFAULT_MODEL: Default model name.OPENAI_API_KEY,ANTHROPIC_API_KEY, etc.: Fallback keys used bySeekGPTifSEEKGPT_API_KEYis missing.SEEKGPT_LOGLEVEL: Set logging level (e.g.,DEBUG,INFO,WARNING). Default isWARNING.
- Client Initialization Arguments: Pass
api_key,api_base,default_model,timeoutdirectly when creatingSeekGPTClientorSeekGPT.
Connecting to Non-OpenAI-Compatible APIs
This library focuses on the OpenAI API standard (/v1/chat/completions). For services with significantly different APIs, authentication methods, or response structures, using their official Python SDKs is strongly recommended:
- Google Generative AI (Gemini): Use the
google-generativeaiorgoogle-cloud-aiplatformlibraries. - Anthropic (Claude): Use the
anthropiclibrary. It requires specific headers (x-api-key,anthropic-version) and has a different request/response structure. - Cohere: Use the
coherelibrary.
While you could potentially adapt SeekGPT._request or create new client classes within this library for them, it often involves reimplementing logic already handled well by their official SDKs.
Error Handling
The library defines custom exceptions inheriting from SeekGPTError:
AuthenticationError: For 401/403 errors.APIConnectionError: For network issues (timeout, connection refused).InvalidRequestError: For 400/422/429 errors (bad request, rate limit).APIError: For other non-2xx API errors.
Wrap API calls in try...except blocks to handle potential issues.
Contributing
[Details on how to contribute - optional]
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file seekgpt-0.1.3.tar.gz.
File metadata
- Download URL: seekgpt-0.1.3.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
400f778e7eaf5b408f8b6bd526384eb80a5e62a2da6f1ca93ce874f2d9372f13
|
|
| MD5 |
f8e591d8a7eb22e912b3290054cb4f52
|
|
| BLAKE2b-256 |
559661ae4dd4846d62b4217841eb0be8ae63e19096ba291cec659405d2c55a3e
|
File details
Details for the file seekgpt-0.1.3-py3-none-any.whl.
File metadata
- Download URL: seekgpt-0.1.3-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e20664c82f1b1ebef5b4ddc18b6e4f24a6430f010486d3f537fa6b09c9f83ab9
|
|
| MD5 |
103646ce451a060da0d353fab2f18fe9
|
|
| BLAKE2b-256 |
b2f64bb224958c198cac12707c73efe58572681f290b88b826d50c0f34967b74
|