thinai 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- thinai-0.1.0/.gitignore +9 -0
- thinai-0.1.0/CHANGELOG.md +11 -0
- thinai-0.1.0/LICENSE +21 -0
- thinai-0.1.0/PKG-INFO +193 -0
- thinai-0.1.0/README.md +162 -0
- thinai-0.1.0/examples/openai_compat.py +14 -0
- thinai-0.1.0/examples/quickstart.py +18 -0
- thinai-0.1.0/examples/streaming.py +35 -0
- thinai-0.1.0/pyproject.toml +80 -0
- thinai-0.1.0/src/thinai/__init__.py +71 -0
- thinai-0.1.0/src/thinai/__main__.py +93 -0
- thinai-0.1.0/src/thinai/_base.py +138 -0
- thinai-0.1.0/src/thinai/_version.py +1 -0
- thinai-0.1.0/src/thinai/async_client.py +362 -0
- thinai-0.1.0/src/thinai/client.py +356 -0
- thinai-0.1.0/src/thinai/discovery.py +233 -0
- thinai-0.1.0/src/thinai/errors.py +54 -0
- thinai-0.1.0/src/thinai/py.typed +0 -0
- thinai-0.1.0/src/thinai/types.py +309 -0
- thinai-0.1.0/tests/__init__.py +0 -0
- thinai-0.1.0/tests/live/__init__.py +0 -0
- thinai-0.1.0/tests/live/test_live.py +130 -0
- thinai-0.1.0/tests/unit/__init__.py +0 -0
- thinai-0.1.0/tests/unit/conftest.py +69 -0
- thinai-0.1.0/tests/unit/test_async_client.py +71 -0
- thinai-0.1.0/tests/unit/test_base.py +89 -0
- thinai-0.1.0/tests/unit/test_client.py +265 -0
- thinai-0.1.0/tests/unit/test_discovery.py +70 -0
thinai-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `Thinai` (sync) and `AsyncThinai` (asyncio) clients for the phone's `/api/*` routes: `chat`, `generate`, `embed`, `models`, `running`, `show`.
|
|
8
|
+
- NDJSON streaming, tool calls, and generation metrics (tokens per second).
|
|
9
|
+
- LAN discovery with `discover()` / `adiscover()`, which scan the local /24 for the Thinai fingerprint. `Thinai()` connects automatically.
|
|
10
|
+
- `client.openai()`, which returns an OpenAI SDK client pointed at the phone's `/v1` API.
|
|
11
|
+
- `thinai` command line: `discover`, `models`, `chat`.
|
thinai-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thinai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
thinai-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: thinai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for Thinai: discover and use LLMs running on an Android phone over your local network.
|
|
5
|
+
Project-URL: Homepage, https://github.com/ATmega-Software-Technologies/thinai-python-sdk
|
|
6
|
+
Project-URL: Repository, https://github.com/ATmega-Software-Technologies/thinai-python-sdk
|
|
7
|
+
Project-URL: Issues, https://github.com/ATmega-Software-Technologies/thinai-python-sdk/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/ATmega-Software-Technologies/thinai-python-sdk/blob/main/CHANGELOG.md
|
|
9
|
+
Project-URL: Thinai app, https://play.google.com/store/apps/details?id=in.atmega.thinai
|
|
10
|
+
Author-email: Thinai <dev.atmega@gmail.com>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: android,gguf,lan,llm,local-llm,offline,ollama,sdk
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Requires-Dist: httpx<1,>=0.27
|
|
28
|
+
Provides-Extra: openai
|
|
29
|
+
Requires-Dist: openai>=1.0; extra == 'openai'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# thinai
|
|
33
|
+
|
|
34
|
+
Python SDK for **[Thinai](https://play.google.com/store/apps/details?id=in.atmega.thinai&hl=en_IN)**, the Android app that runs LLMs offline on your phone and serves them to your Wi-Fi.
|
|
35
|
+
|
|
36
|
+
Point your laptop at the phone and use its models from Python. There are no API keys and no cloud, and your data stays on your network.
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install thinai
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from thinai import Thinai
|
|
44
|
+
|
|
45
|
+
client = Thinai() # finds the phone on your Wi-Fi
|
|
46
|
+
print(client.chat("Explain RAG in one sentence.").content)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Set up the phone
|
|
50
|
+
|
|
51
|
+
1. Open Thinai, download a model, and go to the **Server** tab.
|
|
52
|
+
2. Press **Start**.
|
|
53
|
+
3. Turn on **Share on local network**.
|
|
54
|
+
4. Keep the computer on the same Wi-Fi. The app shows the address, for example `http://192.168.1.36:11434`.
|
|
55
|
+
|
|
56
|
+
## Connecting
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from thinai import Thinai, discover
|
|
60
|
+
|
|
61
|
+
client = Thinai() # scan the local /24 for Thinai
|
|
62
|
+
client = Thinai("192.168.1.36") # known IP
|
|
63
|
+
client = Thinai("192.168.1.36", port=8080) # custom port from the app
|
|
64
|
+
client = Thinai("http://192.168.1.36:11434", model="gemma-3-270m-it-q8_0")
|
|
65
|
+
|
|
66
|
+
for server in discover(): # every phone on the network
|
|
67
|
+
print(server.base_url, server.models)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
When you don't pass `host`, the client looks in this order:
|
|
71
|
+
|
|
72
|
+
1. `$THINAI_HOST`
|
|
73
|
+
2. a Thinai app on this machine
|
|
74
|
+
3. a scan of your local subnet
|
|
75
|
+
|
|
76
|
+
The app doesn't advertise itself on the network yet. The scan probes `GET /` on each address and matches the `Thinai is running` reply, which usually takes 1–2 seconds. Pass `discover(subnets=["10.0.0.0/24"])` if your network isn't a /24.
|
|
77
|
+
|
|
78
|
+
## Chat
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from thinai import Message
|
|
82
|
+
|
|
83
|
+
response = client.chat(
|
|
84
|
+
[Message.system("You are concise."), Message.user("What is a GGUF file?")],
|
|
85
|
+
temperature=0.3,
|
|
86
|
+
num_ctx=4096, # clamped by the phone to client.show().context_cap
|
|
87
|
+
num_predict=256,
|
|
88
|
+
)
|
|
89
|
+
print(response.content, response.done_reason)
|
|
90
|
+
print(f"{response.metrics.tokens_per_second:.1f} tok/s on the phone")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
To stream the reply as it's generated:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
for chunk in client.chat("Write a haiku about the monsoon.", stream=True):
|
|
97
|
+
print(chunk.content, end="", flush=True)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
If you leave out `model`, the client uses the model already loaded on the phone. Swapping models on a phone is slow.
|
|
101
|
+
|
|
102
|
+
Tool calling uses OpenAI-format tool definitions:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
tools = [{"type": "function", "function": {
|
|
106
|
+
"name": "get_weather",
|
|
107
|
+
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
|
|
108
|
+
}}]
|
|
109
|
+
response = client.chat("Weather in Chennai?", tools=tools)
|
|
110
|
+
for call in response.tool_calls:
|
|
111
|
+
print(call.name, call.arguments)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Generate, embed, models
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
client.generate("Once upon a time", num_predict=50).response
|
|
118
|
+
client.embed(["hello", "world"], model="<embedding-model-id>").embeddings # needs an embedding model
|
|
119
|
+
client.models() # installed models
|
|
120
|
+
client.running() # loaded model
|
|
121
|
+
client.show() # architecture, trained context_length, context_cap
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Async
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
import asyncio
|
|
128
|
+
from thinai import AsyncThinai
|
|
129
|
+
|
|
130
|
+
async def main():
|
|
131
|
+
async with AsyncThinai() as client:
|
|
132
|
+
async for chunk in await client.chat("Hi!", stream=True):
|
|
133
|
+
print(chunk.content, end="")
|
|
134
|
+
|
|
135
|
+
asyncio.run(main())
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## OpenAI-compatible API
|
|
139
|
+
|
|
140
|
+
The phone also serves `/v1/chat/completions`, `/v1/embeddings` and `/v1/models`. To use them through the official client:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
pip install 'thinai[openai]'
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
oai = Thinai().openai()
|
|
148
|
+
oai.chat.completions.create(model="lfm2.5-350m-q8_0", messages=[{"role": "user", "content": "hi"}])
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
This means LangChain, LlamaIndex and other OpenAI-compatible tools work too. Give them `client.base_url + "/v1"` as the base URL.
|
|
152
|
+
|
|
153
|
+
## Command line
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
thinai discover
|
|
157
|
+
thinai models --host 192.168.1.36
|
|
158
|
+
thinai chat "What is the capital of Tamil Nadu?"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Errors
|
|
162
|
+
|
|
163
|
+
| Exception | When |
|
|
164
|
+
| --- | --- |
|
|
165
|
+
| `DiscoveryError` | No phone found on the network |
|
|
166
|
+
| `APIConnectionError` / `APITimeoutError` | Phone unreachable (server stopped, sharing off, different Wi-Fi) |
|
|
167
|
+
| `NotFoundError` | Unknown model id |
|
|
168
|
+
| `BadRequestError` | Invalid request, such as embedding with a chat model |
|
|
169
|
+
| `ServerError` | Inference failed on the phone |
|
|
170
|
+
| `StreamError` | Server reported an error mid-stream |
|
|
171
|
+
|
|
172
|
+
All of them inherit from `thinai.ThinaiError`.
|
|
173
|
+
|
|
174
|
+
## Good to know
|
|
175
|
+
|
|
176
|
+
- **No authentication.** Anyone on the same Wi-Fi can use the phone's models while sharing is on. Only enable it on networks you trust.
|
|
177
|
+
- **One request at a time.** The phone processes requests in a queue, so concurrent calls wait. Cancelling a request on the client doesn't stop generation on the phone.
|
|
178
|
+
- **`/api/chat` and `/api/generate` can return engine errors as text.** For example, "request exceeds the available context size" may arrive as the reply content instead of an HTTP error.
|
|
179
|
+
- **`num_ctx` only works on `/api/*` routes.** The OpenAI-compatible `/v1/chat/completions` always uses the context size set in the app.
|
|
180
|
+
|
|
181
|
+
## Development
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
uv sync
|
|
185
|
+
uv run pytest # unit tests (offline)
|
|
186
|
+
THINAI_HOST=192.168.1.36 uv run pytest -m live # against a real phone
|
|
187
|
+
uv run ruff check . && uv run mypy
|
|
188
|
+
uv build && uv run twine check dist/*
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
MIT
|
thinai-0.1.0/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# thinai
|
|
2
|
+
|
|
3
|
+
Python SDK for **[Thinai](https://play.google.com/store/apps/details?id=in.atmega.thinai&hl=en_IN)**, the Android app that runs LLMs offline on your phone and serves them to your Wi-Fi.
|
|
4
|
+
|
|
5
|
+
Point your laptop at the phone and use its models from Python. There are no API keys and no cloud, and your data stays on your network.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install thinai
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from thinai import Thinai
|
|
13
|
+
|
|
14
|
+
client = Thinai() # finds the phone on your Wi-Fi
|
|
15
|
+
print(client.chat("Explain RAG in one sentence.").content)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Set up the phone
|
|
19
|
+
|
|
20
|
+
1. Open Thinai, download a model, and go to the **Server** tab.
|
|
21
|
+
2. Press **Start**.
|
|
22
|
+
3. Turn on **Share on local network**.
|
|
23
|
+
4. Keep the computer on the same Wi-Fi. The app shows the address, for example `http://192.168.1.36:11434`.
|
|
24
|
+
|
|
25
|
+
## Connecting
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from thinai import Thinai, discover
|
|
29
|
+
|
|
30
|
+
client = Thinai() # scan the local /24 for Thinai
|
|
31
|
+
client = Thinai("192.168.1.36") # known IP
|
|
32
|
+
client = Thinai("192.168.1.36", port=8080) # custom port from the app
|
|
33
|
+
client = Thinai("http://192.168.1.36:11434", model="gemma-3-270m-it-q8_0")
|
|
34
|
+
|
|
35
|
+
for server in discover(): # every phone on the network
|
|
36
|
+
print(server.base_url, server.models)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
When you don't pass `host`, the client looks in this order:
|
|
40
|
+
|
|
41
|
+
1. `$THINAI_HOST`
|
|
42
|
+
2. a Thinai app on this machine
|
|
43
|
+
3. a scan of your local subnet
|
|
44
|
+
|
|
45
|
+
The app doesn't advertise itself on the network yet. The scan probes `GET /` on each address and matches the `Thinai is running` reply, which usually takes 1–2 seconds. Pass `discover(subnets=["10.0.0.0/24"])` if your network isn't a /24.
|
|
46
|
+
|
|
47
|
+
## Chat
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from thinai import Message
|
|
51
|
+
|
|
52
|
+
response = client.chat(
|
|
53
|
+
[Message.system("You are concise."), Message.user("What is a GGUF file?")],
|
|
54
|
+
temperature=0.3,
|
|
55
|
+
num_ctx=4096, # clamped by the phone to client.show().context_cap
|
|
56
|
+
num_predict=256,
|
|
57
|
+
)
|
|
58
|
+
print(response.content, response.done_reason)
|
|
59
|
+
print(f"{response.metrics.tokens_per_second:.1f} tok/s on the phone")
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
To stream the reply as it's generated:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
for chunk in client.chat("Write a haiku about the monsoon.", stream=True):
|
|
66
|
+
print(chunk.content, end="", flush=True)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If you leave out `model`, the client uses the model already loaded on the phone. Swapping models on a phone is slow.
|
|
70
|
+
|
|
71
|
+
Tool calling uses OpenAI-format tool definitions:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
tools = [{"type": "function", "function": {
|
|
75
|
+
"name": "get_weather",
|
|
76
|
+
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
|
|
77
|
+
}}]
|
|
78
|
+
response = client.chat("Weather in Chennai?", tools=tools)
|
|
79
|
+
for call in response.tool_calls:
|
|
80
|
+
print(call.name, call.arguments)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Generate, embed, models
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
client.generate("Once upon a time", num_predict=50).response
|
|
87
|
+
client.embed(["hello", "world"], model="<embedding-model-id>").embeddings # needs an embedding model
|
|
88
|
+
client.models() # installed models
|
|
89
|
+
client.running() # loaded model
|
|
90
|
+
client.show() # architecture, trained context_length, context_cap
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Async
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
import asyncio
|
|
97
|
+
from thinai import AsyncThinai
|
|
98
|
+
|
|
99
|
+
async def main():
|
|
100
|
+
async with AsyncThinai() as client:
|
|
101
|
+
async for chunk in await client.chat("Hi!", stream=True):
|
|
102
|
+
print(chunk.content, end="")
|
|
103
|
+
|
|
104
|
+
asyncio.run(main())
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## OpenAI-compatible API
|
|
108
|
+
|
|
109
|
+
The phone also serves `/v1/chat/completions`, `/v1/embeddings` and `/v1/models`. To use them through the official client:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pip install 'thinai[openai]'
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
oai = Thinai().openai()
|
|
117
|
+
oai.chat.completions.create(model="lfm2.5-350m-q8_0", messages=[{"role": "user", "content": "hi"}])
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
This means LangChain, LlamaIndex and other OpenAI-compatible tools work too. Give them `client.base_url + "/v1"` as the base URL.
|
|
121
|
+
|
|
122
|
+
## Command line
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
thinai discover
|
|
126
|
+
thinai models --host 192.168.1.36
|
|
127
|
+
thinai chat "What is the capital of Tamil Nadu?"
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Errors
|
|
131
|
+
|
|
132
|
+
| Exception | When |
|
|
133
|
+
| --- | --- |
|
|
134
|
+
| `DiscoveryError` | No phone found on the network |
|
|
135
|
+
| `APIConnectionError` / `APITimeoutError` | Phone unreachable (server stopped, sharing off, different Wi-Fi) |
|
|
136
|
+
| `NotFoundError` | Unknown model id |
|
|
137
|
+
| `BadRequestError` | Invalid request, such as embedding with a chat model |
|
|
138
|
+
| `ServerError` | Inference failed on the phone |
|
|
139
|
+
| `StreamError` | Server reported an error mid-stream |
|
|
140
|
+
|
|
141
|
+
All of them inherit from `thinai.ThinaiError`.
|
|
142
|
+
|
|
143
|
+
## Good to know
|
|
144
|
+
|
|
145
|
+
- **No authentication.** Anyone on the same Wi-Fi can use the phone's models while sharing is on. Only enable it on networks you trust.
|
|
146
|
+
- **One request at a time.** The phone processes requests in a queue, so concurrent calls wait. Cancelling a request on the client doesn't stop generation on the phone.
|
|
147
|
+
- **`/api/chat` and `/api/generate` can return engine errors as text.** For example, "request exceeds the available context size" may arrive as the reply content instead of an HTTP error.
|
|
148
|
+
- **`num_ctx` only works on `/api/*` routes.** The OpenAI-compatible `/v1/chat/completions` always uses the context size set in the app.
|
|
149
|
+
|
|
150
|
+
## Development
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
uv sync
|
|
154
|
+
uv run pytest # unit tests (offline)
|
|
155
|
+
THINAI_HOST=192.168.1.36 uv run pytest -m live # against a real phone
|
|
156
|
+
uv run ruff check . && uv run mypy
|
|
157
|
+
uv build && uv run twine check dist/*
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Use the official OpenAI SDK against the phone. Requires: pip install 'thinai[openai]'."""
|
|
2
|
+
|
|
3
|
+
from thinai import Thinai
|
|
4
|
+
|
|
5
|
+
client = Thinai()
|
|
6
|
+
oai = client.openai()
|
|
7
|
+
|
|
8
|
+
model = client.running()[0].name
|
|
9
|
+
completion = oai.chat.completions.create(
|
|
10
|
+
model=model,
|
|
11
|
+
messages=[{"role": "user", "content": "Say hello from my phone."}],
|
|
12
|
+
max_tokens=40,
|
|
13
|
+
)
|
|
14
|
+
print(completion.choices[0].message.content)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Find the phone, list its models, and ask a question.
|
|
2
|
+
|
|
3
|
+
python examples/quickstart.py # auto-discover
|
|
4
|
+
THINAI_HOST=192.168.1.36 python examples/quickstart.py
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from thinai import Thinai
|
|
8
|
+
|
|
9
|
+
with Thinai() as client:
|
|
10
|
+
print(f"Connected to {client.base_url}")
|
|
11
|
+
loaded = {m.name for m in client.running()}
|
|
12
|
+
for model in client.models():
|
|
13
|
+
print(f" {'*' if model.name in loaded else ' '} {model.name}")
|
|
14
|
+
|
|
15
|
+
response = client.chat("In one sentence, why run an LLM on a phone?", num_predict=80)
|
|
16
|
+
print("\n" + response.content.strip())
|
|
17
|
+
if response.metrics and response.metrics.tokens_per_second:
|
|
18
|
+
print(f"\n[{response.metrics.tokens_per_second:.1f} tokens/s on the phone]")
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""A tiny terminal chat that streams replies from the phone, sync and async."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
|
|
5
|
+
from thinai import AsyncThinai, Thinai
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def sync_chat() -> None:
|
|
9
|
+
client = Thinai()
|
|
10
|
+
history = [{"role": "system", "content": "You are a helpful, concise assistant."}]
|
|
11
|
+
print(f"Chatting with {client.base_url} (empty line to quit)")
|
|
12
|
+
while True:
|
|
13
|
+
prompt = input("\nyou> ").strip()
|
|
14
|
+
if not prompt:
|
|
15
|
+
break
|
|
16
|
+
history.append({"role": "user", "content": prompt})
|
|
17
|
+
reply = ""
|
|
18
|
+
print("ai> ", end="")
|
|
19
|
+
for chunk in client.chat(history, stream=True):
|
|
20
|
+
print(chunk.content, end="", flush=True)
|
|
21
|
+
reply += chunk.content
|
|
22
|
+
print()
|
|
23
|
+
history.append({"role": "assistant", "content": reply})
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def async_once() -> None:
|
|
27
|
+
async with AsyncThinai() as client:
|
|
28
|
+
async for chunk in await client.chat("Give me three Tamil greetings.", stream=True):
|
|
29
|
+
print(chunk.content, end="", flush=True)
|
|
30
|
+
print()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
asyncio.run(async_once())
|
|
35
|
+
sync_chat()
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "thinai"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Python SDK for Thinai: discover and use LLMs running on an Android phone over your local network."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Thinai", email = "dev.atmega@gmail.com" }]
|
|
14
|
+
keywords = ["llm", "local-llm", "android", "ollama", "offline", "gguf", "lan", "sdk"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = ["httpx>=0.27,<1"]
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
openai = ["openai>=1.0"]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
thinai = "thinai.__main__:main"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/ATmega-Software-Technologies/thinai-python-sdk"
|
|
39
|
+
Repository = "https://github.com/ATmega-Software-Technologies/thinai-python-sdk"
|
|
40
|
+
Issues = "https://github.com/ATmega-Software-Technologies/thinai-python-sdk/issues"
|
|
41
|
+
Changelog = "https://github.com/ATmega-Software-Technologies/thinai-python-sdk/blob/main/CHANGELOG.md"
|
|
42
|
+
"Thinai app" = "https://play.google.com/store/apps/details?id=in.atmega.thinai"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"pytest>=8",
|
|
47
|
+
"pytest-asyncio>=0.23",
|
|
48
|
+
"ruff>=0.6",
|
|
49
|
+
"mypy>=1.10",
|
|
50
|
+
"openai>=1.0",
|
|
51
|
+
"twine>=5",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[tool.hatch.version]
|
|
55
|
+
path = "src/thinai/_version.py"
|
|
56
|
+
|
|
57
|
+
[tool.hatch.build.targets.wheel]
|
|
58
|
+
packages = ["src/thinai"]
|
|
59
|
+
|
|
60
|
+
[tool.hatch.build.targets.sdist]
|
|
61
|
+
include = ["src/thinai", "tests", "examples", "README.md", "CHANGELOG.md", "LICENSE"]
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
testpaths = ["tests"]
|
|
65
|
+
asyncio_mode = "auto"
|
|
66
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
67
|
+
addopts = "-m 'not live'"
|
|
68
|
+
markers = ["live: needs a running Thinai app on the local network (run with -m live)"]
|
|
69
|
+
|
|
70
|
+
[tool.ruff]
|
|
71
|
+
line-length = 100
|
|
72
|
+
target-version = "py39"
|
|
73
|
+
|
|
74
|
+
[tool.ruff.lint]
|
|
75
|
+
select = ["E", "F", "I", "B", "W"]
|
|
76
|
+
|
|
77
|
+
[tool.mypy]
|
|
78
|
+
python_version = "3.10"
|
|
79
|
+
strict = true
|
|
80
|
+
files = ["src"]
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Python SDK for Thinai: LLMs running on an Android phone, served over your Wi-Fi.
|
|
2
|
+
|
|
3
|
+
>>> from thinai import Thinai
|
|
4
|
+
>>> client = Thinai() # finds the phone on the local network
|
|
5
|
+
>>> print(client.chat("Hello!").content)
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from ._base import DEFAULT_PORT
|
|
9
|
+
from ._version import __version__
|
|
10
|
+
from .async_client import AsyncThinai
|
|
11
|
+
from .client import Thinai
|
|
12
|
+
from .discovery import adiscover, afind_server, discover, find_server
|
|
13
|
+
from .errors import (
|
|
14
|
+
APIConnectionError,
|
|
15
|
+
APIStatusError,
|
|
16
|
+
APITimeoutError,
|
|
17
|
+
AuthenticationError,
|
|
18
|
+
BadRequestError,
|
|
19
|
+
DiscoveryError,
|
|
20
|
+
NotFoundError,
|
|
21
|
+
ServerError,
|
|
22
|
+
StreamError,
|
|
23
|
+
ThinaiError,
|
|
24
|
+
)
|
|
25
|
+
from .types import (
|
|
26
|
+
ChatChunk,
|
|
27
|
+
ChatResponse,
|
|
28
|
+
EmbedResponse,
|
|
29
|
+
GenerateChunk,
|
|
30
|
+
GenerateResponse,
|
|
31
|
+
Message,
|
|
32
|
+
Metrics,
|
|
33
|
+
Model,
|
|
34
|
+
ModelInfo,
|
|
35
|
+
RunningModel,
|
|
36
|
+
Server,
|
|
37
|
+
ToolCall,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"DEFAULT_PORT",
|
|
42
|
+
"__version__",
|
|
43
|
+
"Thinai",
|
|
44
|
+
"AsyncThinai",
|
|
45
|
+
"discover",
|
|
46
|
+
"adiscover",
|
|
47
|
+
"find_server",
|
|
48
|
+
"afind_server",
|
|
49
|
+
"ThinaiError",
|
|
50
|
+
"DiscoveryError",
|
|
51
|
+
"APIConnectionError",
|
|
52
|
+
"APITimeoutError",
|
|
53
|
+
"APIStatusError",
|
|
54
|
+
"BadRequestError",
|
|
55
|
+
"AuthenticationError",
|
|
56
|
+
"NotFoundError",
|
|
57
|
+
"ServerError",
|
|
58
|
+
"StreamError",
|
|
59
|
+
"ChatChunk",
|
|
60
|
+
"ChatResponse",
|
|
61
|
+
"EmbedResponse",
|
|
62
|
+
"GenerateChunk",
|
|
63
|
+
"GenerateResponse",
|
|
64
|
+
"Message",
|
|
65
|
+
"Metrics",
|
|
66
|
+
"Model",
|
|
67
|
+
"ModelInfo",
|
|
68
|
+
"RunningModel",
|
|
69
|
+
"Server",
|
|
70
|
+
"ToolCall",
|
|
71
|
+
]
|