Skip to content

Commit

Permalink
✨ ready
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tokyo committed Dec 9, 2024
1 parent 86ca1bd commit 6154025
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@
![Codecov Coverage](https://img.shields.io/codecov/c/github/A-Tokyo/ai-zero-shot-classifier.svg)
![Twitter Follow](https://img.shields.io/twitter/follow/ahmad_tokyo.svg?label=Follow%20@ahmad_tokyo)

[Demo](https://a-tokyo.github.io/ai-zero-shot-classifier)
---

## 🚀 Introduction

**ai-zero-shot-classifier** is a powerful JavaScript library that leverages advanced AI embeddings to perform zero-shot text classification. Whether you're dealing with unlabelled data or seeking to classify text against dynamic and user-defined labels, this library provides a seamless and efficient solution.
**ai-zero-shot-classifier** is a powerful, flexible JavaScript library designed to perform zero-shot text classification using pre-trained AI embeddings. The library supports multiple providers and models, enabling you to choose the best AI tools for your project, whether it's OpenAI's models or alternative providers like Groq.

---

## 🧐 Why ai-zero-shot-classifier?

### **The Problem**
In many real-world applications, acquiring labeled data for training classification models is time-consuming, expensive, and sometimes impractical. Traditional supervised learning approaches require vast amounts of annotated data, which isn't always feasible to obtain. Moreover, adapting models to new, unforeseen categories necessitates retraining, which can be resource-intensive.
Traditional text classification requires extensive labeled data and retraining models to adapt to new categories. This process can be costly, time-consuming, and impractical when dealing with constantly evolving datasets or dynamic categories.

### **The Innovation**
**ai-zero-shot-classifier** addresses these challenges by using pre-trained embeddings to classify text without any task-specific training. You simply define your labels dynamically, and the classifier matches your data with the closest label based on semantic similarity.
**ai-zero-shot-classifier** eliminates the need for labeled datasets by leveraging pre-trained AI embeddings. It allows for dynamic and task-specific labels, enabling real-time classification across various domains without retraining models. It supports multiple providers and their respective models, making it adaptable to diverse use cases.

---

## ✨ Features

- **Dynamic Labels**: Classify text using labels you define on the fly.
- **Multiple Similarity Functions**: Supports cosine similarity, dot product, and Euclidean distance.
- **Batch Processing**: Handles embeddings and similarity calculations efficiently using customizable batch sizes and concurrency.
- **Integration-Friendly**: Works seamlessly with OpenAI embeddings and can be easily integrated into your application.
- **Multi-Provider Support**: Works with providers like OpenAI and Groq, enabling integration with models such as GPT, Llama, and others.
- **Dynamic Labels**: Define your labels dynamically for each classification task.
- **Multiple Similarity Functions**: Supports cosine similarity, dot product, and Euclidean distance for flexible classification needs.
- **Batch Processing**: Efficiently handles large datasets with customizable batch sizes and concurrency.
- **Highly Configurable**: Adjustable settings for embeddings, similarity calculations, and more.
- **Seamless Integration**: Simple API designed for easy use in Node.js and browser environments.

---

## 📦 Installation

Expand All @@ -39,6 +47,8 @@ or
yarn add ai-zero-shot-classifier
```

---

## 🚀 Usage

### Basic Example
Expand All @@ -62,7 +72,9 @@ classify({ labels, data, config: { similarity: 'cosine' } })
});
```

### Configuration Options
---

## ⚙️ Configuration Options

| Option | Description | Default |
|----------------------------|---------------------------------------------------|------------------|
Expand All @@ -74,6 +86,8 @@ classify({ labels, data, config: { similarity: 'cosine' } })
| `comparingConcurrencyTop` | Concurrency for top-level comparisons | `10` |
| `comparingConcurrencyBottom` | Concurrency for bottom-level comparisons | `10` |

---

## 🛠️ Development

Clone the repository:
Expand All @@ -100,10 +114,14 @@ Run tests:
yarn test
```

---

## 🤝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

---

## 📄 License

This project is licensed under the MIT License.
65 changes: 39 additions & 26 deletions demo/src/Demo/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import classify from '../../../src/classify'; // Adjust the import path as neces
function Demo() {
// Configuration states
const [apiKey, setApiKey] = useState(''); // New state for API Key
const [provider, setProvider] = useState<'openai' | 'groq'>('openai'); // Provider selection
const [model, setModel] = useState('text-embedding-3-small'); // Model selection
const [labelsInput, setLabelsInput] = useState('Technology, Health, Finance'); // Comma-separated labels
const [similarity, setSimilarity] = useState('cosine'); // 'cosine', 'dot', 'euclidean'

Expand Down Expand Up @@ -38,9 +40,9 @@ Healthcare advancements are improving lives.`,
.map((data) => data.trim())
.filter((data) => data.length > 0);

setCodeString(`import classify from 'ai-zero-shot-classifier';
setCodeString(`import { classify } from 'ai-zero-shot-classifier';
/** Your OpenAI API Key */
/** Your Provider API Key */
const apiKey = '${apiKey}';
/** Labels for classification */
Expand All @@ -53,6 +55,8 @@ const data = [
/** Configuration (optional) */
const config = {
provider: '${provider}',
model: '${model}',
similarity: '${similarity}',
};
Expand All @@ -65,15 +69,14 @@ classify({ labels, data, config, apiKey })
console.error(error);
});
`);
}, [apiKey, labelsInput, dataInput, similarity]);
}, [apiKey, provider, model, labelsInput, dataInput, similarity]);

// Handle classification
const handleClassify = async () => {
setLoading(true);
setError('');
setResults([]);

// Parse labels and data
const labels = labelsInput
.split(',')
.map((label) => label.trim())
Expand All @@ -97,7 +100,7 @@ classify({ labels, data, config, apiKey })
}

if (!apiKey) {
setError('Please provide your OpenAI API Key.');
setError('Please provide your Provider API Key.');
setLoading(false);
return;
}
Expand All @@ -106,8 +109,10 @@ classify({ labels, data, config, apiKey })
const classificationResults = await classify({
labels,
data,
provider,
model,
config: { similarity },
apiKey, // Pass the API Key here
apiKey,
});

// Combine each data item with its classification result
Expand All @@ -129,24 +134,41 @@ classify({ labels, data, config, apiKey })
<article className="wrapper">
<header>
<h1>AI Zero-Shot Classifier Demo</h1>
<p>
Classify text data against predefined labels without training or
fine-tuning using AI.
</p>
<p>Classify text data against predefined labels using AI-powered embeddings.</p>
</header>
<div className="container">
<section className="config-section">
<h2>Configuration</h2>
<div className="config-group">
<label htmlFor="apiKey">OpenAI API Key:</label>
<label htmlFor="apiKey">Provider API Key:</label>
<input
type="password"
id="apiKey"
placeholder="Enter your OpenAI API Key"
placeholder="Enter your Provider API Key"
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
/>
</div>
<div className="config-group">
<label htmlFor="provider">Provider:</label>
<select
id="provider"
value={provider}
onChange={(e) => setProvider(e.target.value)}>
<option value="openai">OpenAI</option>
<option value="groq">Groq</option>
</select>
</div>
<div className="config-group">
<label htmlFor="model">Model:</label>
<input
type="text"
id="model"
placeholder="e.g., text-embedding-3-small or text-embedding-ada-002"
value={model}
onChange={(e) => setModel(e.target.value)}
/>
</div>
<div className="config-group">
<label htmlFor="labels">Labels (comma-separated):</label>
<input
Expand Down Expand Up @@ -192,16 +214,9 @@ classify({ labels, data, config, apiKey })
<ul>
{results.map((result, index) => (
<li key={index} className="result-item">
<p>
<strong>Text:</strong> {result.text}
</p>
<p>
<strong>Label:</strong> {result.label}
</p>
<p>
<strong>Confidence:</strong>{' '}
{result.confidence.toFixed(4)}
</p>
<p><strong>Text:</strong> {result.text}</p>
<p><strong>Label:</strong> {result.label}</p>
<p><strong>Confidence:</strong> {result.confidence.toFixed(4)}</p>
</li>
))}
</ul>
Expand All @@ -221,14 +236,12 @@ classify({ labels, data, config, apiKey })
<footer>
<p>
Built with{' '}
<span role="img" aria-label="love">
❤️
</span>{' '}
<span role="img" aria-label="love">❤️</span>{' '}
by <a href="https://ahmedtokyo.com">Ahmed Tokyo</a>
</p>
</footer>
</article>
);
}

export default Demo;
export default Demo;

0 comments on commit 6154025

Please sign in to comment.