Skip to main content

Take your best shot

Project description

🎯 FewShots: The best few shots with LLMs

Python 3.8+ License: MIT

Ever wished your AI model had a better memory? Meet FewShot - the simple yet powerful library for managing and retrieving few-shot examples with style! 🧠✨

🌟 Features

  • 🎮 Easy to Use: Simple, intuitive API for managing your AI's example database
  • 🔄 Structured Output: Support for structured outputs

💡 Use Cases

  • 🤖 Enhance your chatbot with dynamic example retrieval
  • 📚 Build a self-improving knowledge base
  • 🎯 Implement context-aware few-shot learning

🛠️ Core Components

  • Shot: The fundamental unit representing an input-output pair with a unique ID (bring your own ID or let FewShots hash the inputs)
  • Embed: Converts inputs into vector embeddings for similarity search
  • Store: Manages storage and retrieval of examples
  • Client: Ties everything together with a clean, simple interface

🚀 Quick Start

from sentence_transformers import SentenceTransformer # Can also use OpenAI, etc.
from few_shots.client import FewShots
from few_shots.embed.transformers import TransformersEmbed
from few_shots.store.memory import MemoryStore

# Create a FewShot client
shots = FewShots(
    embed=TransformersEmbed(SentenceTransformer("all-MiniLM-L6-v2")),
    store=MemoryStore()
)

# Add some examples
shots.add(
    inputs="How do I make a pizza?",
    outputs="1. Make the dough 2. Add toppings 3. Bake at 450°F"
)

# Find similar examples
best_shots = shots.list("What's the recipe for pizza?", limit=1)
for distance, shot in results:
    print(f"Found match (distance: {distance:.2f}):")
    print(f"Q: {shot.inputs}")
    print(f"A: {shot.outputs}")

# Use with your LLM
from few_shots.utils.format import shots_to_messages

openai.chat.completions.create(
    ...,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        *shots_to_messages(best_shots),
        {"role": "user", "content": "What's the recipe for pizza?"},
    ]
)

🔧 Installation

pip install few-shots
rye add few-shots
poetry add few-shots

🎮 Usage Examples

Working with Structured Output I/O

# Add structured data
shots.add(
    inputs={"type": "greeting", "language": "English"},
    outputs={"text": "Hello, world!"}
)

# Search with similar inputs
best_shots = shots.list({"type": "greeting", "language": "English"})

Async Support

from few_shots.async_client import AsyncFewShot

shots = AsyncFewShots(embed=async_embedder, store=async_store)

# Add examples asynchronously
await shots.add(
    inputs="What's the weather like?",
    outputs="I don't have access to real-time weather data."
)

# Search asynchronously
best_shots = await shots.list("How's the weather today?", limit=1)

Using OpenAI / LiteLLM for Embeddings

The OpenAIEmbed and AsyncOpenAIEmbed classes are compatible with all OpenAI-compatible SDKs.

from few_shots import AsyncFewShots
from few_shots.embed.openai import OpenAIEmbed, AsyncOpenAIEmbed # Compatible with all OpenAI

from openai import OpenAI

shots = FewShots(
    embed=OpenAIEmbed(
        OpenAI().embeddings.create,
        model="...",
        **kwargs,
    ),
    store=MemoryStore()
)

from litellm import aembedding

shots = AsyncFewShots(
    embed=AsyncOpenAIEmbed(
        aembedding,
        model="...",
        **kwargs,
    ),
    store=MemoryStore()
)

Using different Vector Stores

from few_shots.store.pg import PGStore, AsyncPGStore
from few_shots.store.chroma import ChromaStore, AsyncChromaStore
from few_shots.store.qdrant import QdrantStore, AsyncQdrantStore
from few_shots.store.weaviate import WeaviateStore, AsyncWeaviateStore
from few_shots.store.turbopuffer import TurboPufferStore # Untested
from few_shots.store.milvus import MilvusStore # TODO

# check out the store's .setup method to see how to configure it
# this method creates the table, collection, indexes, etc. and is idempotent

🤝 Contributing

We love contributions! Feel free to:

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

📝 License

MIT License - feel free to use it in your projects!


Made with ❤️ by developers who believe in the power of learning from examples.

Remember: The best AI is the one that learns from experience! 🌟

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

few_shots-0.3.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

few_shots-0.3.0-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file few_shots-0.3.0.tar.gz.

File metadata

  • Download URL: few_shots-0.3.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for few_shots-0.3.0.tar.gz
Algorithm Hash digest
SHA256 685685188adff8d29c95ca9213139894aaa007f7bcc0c969bbc8cef4559e0174
MD5 a5dca62d1f0e29ee1128dc1b6ca217da
BLAKE2b-256 95b666299b8ca35b10609356212a90550f5e1a8a9891ceb46d471d3ab1ac886b

See more details on using hashes here.

File details

Details for the file few_shots-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: few_shots-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for few_shots-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0de53975987f415ab2be8dc8672335d2fc5f2b421971a0528702490e61c221df
MD5 4a1624616a0981dfd41a9794630fd488
BLAKE2b-256 b524ddae19ea6aa26ffefc6601307719260290b1162467d79dcf3cfb2f3a0a6f

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