Skip to main content

OpenAI client for the datapizza-ai framework

Project description

DataPizza AI - OpenAI-Like Client

A versatile client for DataPizza AI that supports OpenAI-compatible APIs, including local models through Ollama, Together AI, and other OpenAI-compatible services.

Installation

pip install datapizza-ai-clients-openai-like

Quick Start

With Ollama (Local Models)

from datapizza.clients.openai_like import OpenAILikeClient

# Create client for Ollama
client = OpenAILikeClient(
    api_key="",  # Ollama doesn't require an API key
    model="gemma2:2b",
    system_prompt="You are a helpful assistant.",
    base_url="http://localhost:11434/v1",
)

response = client.invoke("What is the capital of France?")
print(response.content)

With Together AI

import os
from datapizza.clients.openai_like import OpenAILikeClient

client = OpenAILikeClient(
    api_key=os.getenv("TOGETHER_API_KEY"),
    model="meta-llama/Llama-2-7b-chat-hf",
    system_prompt="You are a helpful assistant.",
    base_url="https://api.together.xyz/v1",
)

response = client.invoke("Explain quantum computing")
print(response.content)

With OpenRouter

import os
from datapizza.clients.openai_like import OpenAILikeClient

client = OpenAILikeClient(
    api_key=os.getenv("OPENROUTER_API_KEY"),
    model="google/gemma-7b-it",
    system_prompt="You are a helpful assistant.",
    base_url="https://openrouter.ai/api/v1",
)

response = client.invoke("What is OpenRouter?")
print(response.content)

With Other OpenAI-Compatible Services

import os
from datapizza.clients.openai_like import OpenAILikeClient

client = OpenAILikeClient(
    api_key=os.getenv("YOUR_API_KEY"),
    model="your-model-name",
    system_prompt="You are a helpful assistant.",
    base_url="https://your-service-url/v1",
)

response = client.invoke("Your question here")
print(response.content)

Features

  • OpenAI-Compatible: Works with any service that implements the OpenAI API standard
  • Local Models: Perfect for running with Ollama for privacy and cost control
  • Memory Support: Built-in memory adapter for conversation history
  • Streaming: Support for real-time streaming responses
  • Structured Outputs: Generate structured data with Pydantic models
  • Tool Calling: Function calling capabilities where supported

Supported Services

  • Ollama - Local model inference
  • Together AI - Cloud-based model hosting
  • OpenRouter - Access a variety of models through a single API
  • Perplexity AI - Search-augmented models
  • Groq - Fast inference API
  • Any OpenAI-compatible API

Advanced Usage

With Memory

from datapizza.clients.openai_like import OpenAILikeClient
from datapizza.memory import Memory

client = OpenAILikeClient(
    api_key="",
    model="llama3.1:8b",
    base_url="http://localhost:11434/v1",
)

memory = Memory(client=client)
memory.add("I'm working on a Python project about machine learning.")
response = memory.query("What libraries should I use?")

Streaming Responses

client = OpenAILikeClient(
    api_key="",
    model="gemma2:7b",
    base_url="http://localhost:11434/v1",
)

for chunk in client.stream("Tell me a story about AI"):
    print(chunk.content, end="", flush=True)

Structured Outputs

from pydantic import BaseModel
from datapizza.clients.openai_like import OpenAILikeClient

class Person(BaseModel):
    name: str
    age: int
    occupation: str

client = OpenAILikeClient(
    api_key="",
    model="llama3.1:8b",
    base_url="http://localhost:11434/v1",
)

response = client.invoke(
    "Generate a person profile",
    response_format=Person
)
print(response.parsed)  # Person object

Configuration Options

Parameter Description Default
api_key API key for the service Required (empty string for Ollama)
model Model name to use Required
base_url Base URL for the API Required
system_prompt System message for the model None
temperature Sampling temperature (0-2) 0.7
max_tokens Maximum tokens in response None
timeout Request timeout in seconds 30

Ollama Setup

  1. Install Ollama from ollama.ai
  2. Pull a model: ollama pull gemma2:2b
  3. Start Ollama: ollama serve
  4. Use with DataPizza AI as shown in the examples above

Popular Ollama Models

  • gemma2:2b - Lightweight, fast responses
  • gemma2:7b - Balanced performance
  • llama3.1:8b - High quality, more resource intensive
  • codellama:7b - Specialized for coding tasks
  • mistral:7b - Good general purpose model

Error Handling

from datapizza.clients.openai_like import OpenAILikeClient
from datapizza.core.clients.exceptions import ClientError

try:
    client = OpenAILikeClient(
        api_key="",
        model="nonexistent-model",
        base_url="http://localhost:11434/v1",
    )
    response = client.invoke("Hello")
except ClientError as e:
    print(f"Client error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Contributing

Contributions are welcome! Please see our Contributing Guide for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

datapizza_ai_clients_openai_like-0.0.8.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file datapizza_ai_clients_openai_like-0.0.8.tar.gz.

File metadata

File hashes

Hashes for datapizza_ai_clients_openai_like-0.0.8.tar.gz
Algorithm Hash digest
SHA256 c36d2794d4f5843422409315b8ee315ffd1aaef95d3c6250ed72e201c2210805
MD5 515bbe73a29200c3fc9264cd6db323c8
BLAKE2b-256 4a0fda9dcd78c52d1fa294cf98acde52d0f6d0cf6f96d5c15934963d78d3d0a7

See more details on using hashes here.

File details

Details for the file datapizza_ai_clients_openai_like-0.0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for datapizza_ai_clients_openai_like-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6ae971cb7d9df465e300d027ceb166467fca94b4c1c54898375c632a53e58949
MD5 4a77a88e8f54a9d409585dec5a108ebd
BLAKE2b-256 4166e9f53dce1cee25a587739dbe1ac7b6ca13f740bc405a1c2c0c30fee11f31

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