|
| 1 | +# Pawa AI Python SDK |
| 2 | + |
| 3 | +[](https://github.com/Sartify/pawa-ai-python/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +Official Python library for the [Pawa AI API](https://docs.pawa-ai.com). |
| 6 | + |
| 7 | +Pawa AI provides African-built small language models for chat, voice, embeddings, document parsing, agents, and knowledge bases. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install pawa-ai |
| 13 | +``` |
| 14 | + |
| 15 | +## Quickstart |
| 16 | + |
| 17 | +Set your API key: |
| 18 | + |
| 19 | +```bash |
| 20 | +export PAWA_AI_API_KEY="your_api_key_here" |
| 21 | +``` |
| 22 | + |
| 23 | +Get your key from the [Builders Dashboard](https://builder.pawa-ai.com/dashboard?page=keys). |
| 24 | + |
| 25 | +### Chat |
| 26 | + |
| 27 | +```python |
| 28 | +from pawa_ai import PawaAI |
| 29 | + |
| 30 | +client = PawaAI() |
| 31 | + |
| 32 | +response = client.chat.create( |
| 33 | + model="pawa-v1-ember-20240924", |
| 34 | + messages=[ |
| 35 | + { |
| 36 | + "role": "user", |
| 37 | + "content": [{"type": "text", "text": "Hello! How can I use AI in my app?"}], |
| 38 | + } |
| 39 | + ], |
| 40 | + stream=False, |
| 41 | +) |
| 42 | + |
| 43 | +print(response.text) # typed ChatCompletion response |
| 44 | +print(response.usage) # token usage when available |
| 45 | +``` |
| 46 | + |
| 47 | +### Streaming |
| 48 | + |
| 49 | +```python |
| 50 | +with client.chat.create( |
| 51 | + model="pawa-v1-ember-20240924", |
| 52 | + messages=[ |
| 53 | + {"role": "user", "content": [{"type": "text", "text": "Explain RAG in simple terms"}]} |
| 54 | + ], |
| 55 | + stream=True, |
| 56 | +) as stream: |
| 57 | + for delta in stream.text_deltas(): |
| 58 | + print(delta, end="", flush=True) |
| 59 | + |
| 60 | + # Or collect the full response after streaming |
| 61 | + completion = stream.collect() |
| 62 | + print(completion.text) |
| 63 | +``` |
| 64 | + |
| 65 | +Async streaming: |
| 66 | + |
| 67 | +```python |
| 68 | +stream = await client.chat.create(..., stream=True) |
| 69 | +text = await stream.collect_text() |
| 70 | +``` |
| 71 | + |
| 72 | +### Text-to-Speech |
| 73 | + |
| 74 | +```python |
| 75 | +audio = client.voice.text_to_speech.create( |
| 76 | + model="pawa-tts-v1-20250704", |
| 77 | + text="Hello, this is Pawa AI speaking!", |
| 78 | + voice="liora", |
| 79 | +) |
| 80 | + |
| 81 | +with open("output.mp3", "wb") as f: |
| 82 | + f.write(audio) |
| 83 | +``` |
| 84 | + |
| 85 | +### Embeddings |
| 86 | + |
| 87 | +```python |
| 88 | +response = client.vectors.create( |
| 89 | + model="pawa-embeddings-v1-20241001", |
| 90 | + sentences=["Embed this sentence.", "And this one too."], |
| 91 | + lang="multi", |
| 92 | +) |
| 93 | + |
| 94 | +embeddings = response.embeddings |
| 95 | +``` |
| 96 | + |
| 97 | +Pass `raw=True` on any resource method to get the original JSON dict instead of typed models. |
| 98 | + |
| 99 | +### Retries with exponential backoff |
| 100 | + |
| 101 | +```python |
| 102 | +from pawa_ai import PawaAI, RetryConfig |
| 103 | + |
| 104 | +client = PawaAI( |
| 105 | + retry_config=RetryConfig( |
| 106 | + max_retries=3, |
| 107 | + initial_delay=0.5, |
| 108 | + max_delay=8.0, |
| 109 | + exponential_base=2.0, |
| 110 | + jitter=0.1, |
| 111 | + ) |
| 112 | +) |
| 113 | +``` |
| 114 | + |
| 115 | +Retries automatically apply to rate limits (429), server errors (500/502/503/504), and connection failures. The SDK respects `Retry-After` response headers when present. |
| 116 | + |
| 117 | +### Async |
| 118 | + |
| 119 | +```python |
| 120 | +import asyncio |
| 121 | +from pawa_ai import AsyncPawaAI |
| 122 | + |
| 123 | +async def main(): |
| 124 | + async with AsyncPawaAI() as client: |
| 125 | + response = await client.chat.create( |
| 126 | + model="pawa-v1-ember-20240924", |
| 127 | + messages=[ |
| 128 | + {"role": "user", "content": [{"type": "text", "text": "Habari yako?"}]} |
| 129 | + ], |
| 130 | + ) |
| 131 | + print(response["data"]) |
| 132 | + |
| 133 | +asyncio.run(main()) |
| 134 | +``` |
| 135 | + |
| 136 | +## API coverage |
| 137 | + |
| 138 | +| Resource | Methods | |
| 139 | +|----------|---------| |
| 140 | +| `client.chat` | `create`, `completions` | |
| 141 | +| `client.models` | `list`, `retrieve` | |
| 142 | +| `client.voice.text_to_speech` | `create` | |
| 143 | +| `client.voice.speech_to_text` | `create`, `transcribe` | |
| 144 | +| `client.vectors` | `create`, `embeddings` | |
| 145 | +| `client.documents` | `parse` | |
| 146 | +| `client.agents` | `create`, `update`, `delete`, `list`, `retrieve` | |
| 147 | +| `client.agents.chat` | `create` | |
| 148 | +| `client.storage.knowledge_base` | CRUD, `list_files`, `semantic_retrieval` | |
| 149 | +| `client.transcribe.workspaces` | CRUD, transcription management | |
| 150 | + |
| 151 | +## Error handling |
| 152 | + |
| 153 | +```python |
| 154 | +from pawa_ai import PawaAI, AuthenticationError, RateLimitError, ChatCompletion |
| 155 | + |
| 156 | +client = PawaAI() |
| 157 | + |
| 158 | +try: |
| 159 | + completion: ChatCompletion = client.chat.create(model="pawa-v1-ember-20240924", messages=[...]) |
| 160 | +except AuthenticationError as e: |
| 161 | + print(f"Auth failed: {e.message}") |
| 162 | +except RateLimitError as e: |
| 163 | + print(f"Rate limited: {e.status_code}") |
| 164 | +``` |
| 165 | + |
| 166 | +## Documentation |
| 167 | + |
| 168 | +- [Pawa AI Docs](https://docs.pawa-ai.com) |
| 169 | +- [API Reference](https://docs.pawa-ai.com/api-reference/introduction) |
| 170 | +- [Quickstart Guide](https://docs.pawa-ai.com/get-started/quickstart) |
| 171 | + |
| 172 | +## License |
| 173 | + |
| 174 | +MIT |
0 commit comments