noosphere 0.8.0 → 0.9.1

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.
package/README.md CHANGED
@@ -7,6 +7,8 @@ One import. Every model. Every modality.
7
7
  ## Features
8
8
 
9
9
  - **7 modalities** — LLM, image, video, TTS, STT, music, and embeddings
10
+ - **OpenAI media** — GPT-Image-1/1.5, DALL-E 2/3, Sora 2/Pro (video), TTS-1/HD, Whisper — all auto-fetched from `OPENAI_API_KEY`
11
+ - **Google media** — Imagen 4.0 (image), Veo 2/3/3.1 (video), Gemini TTS — all auto-fetched from `GEMINI_API_KEY`
10
12
  - **Always up-to-date models** — Dynamic auto-fetch from ALL provider APIs at runtime (OpenAI, Anthropic, Google, Groq, Mistral, xAI, Cerebras, OpenRouter)
11
13
  - **Dynamic descriptions** — Model descriptions fetched from source (Ollama library, HuggingFace READMEs, CivitAI API) — no hardcoded strings
12
14
  - **Modality-filtered sync** — `syncModels('llm')` only fetches LLM providers, avoiding unnecessary requests
@@ -14,6 +16,8 @@ One import. Every model. Every modality.
14
16
  - **30+ HuggingFace tasks** — LLM, image, TTS, translation, summarization, classification, and more
15
17
  - **Local-first architecture** — Auto-detects Ollama, ComfyUI, Whisper, AudioCraft, Piper, and Kokoro on your machine
16
18
  - **Org-aware logos** — HuggingFace models show the real org logo (Meta, Google, NVIDIA) instead of generic HF logo
19
+ - **Pre-request token counting** — Count tokens before sending, for ALL providers (OpenAI/Groq/Ollama via tiktoken, Google/Anthropic via API)
20
+ - **Full pi-ai access** — Agent loop with tool calling, preprocessor (compaction hook), `calculateCost`, direct stream/complete APIs — all re-exported
17
21
  - **Agentic capabilities** — Tool use, function calling, reasoning/thinking, vision, and agent loops via Pi-AI
18
22
  - **Failover & retry** — Automatic retries with exponential backoff and cross-provider failover
19
23
  - **Usage tracking** — Real-time cost, latency, and token tracking across all providers
@@ -38,13 +42,29 @@ const response = await ai.chat({
38
42
  });
39
43
  console.log(response.content);
40
44
 
41
- // Generate an image
45
+ // Generate an image with GPT-Image-1 (OpenAI) — just needs OPENAI_API_KEY
42
46
  const image = await ai.image({
43
47
  prompt: 'A sunset over mountains',
48
+ provider: 'openai-media',
49
+ });
50
+ // image.buffer contains the PNG data
51
+
52
+ // Generate an image with Imagen 4.0 (Google) — just needs GEMINI_API_KEY
53
+ const googleImage = await ai.image({
54
+ prompt: 'A sunset over mountains',
55
+ provider: 'google-media',
56
+ });
57
+ // googleImage.buffer contains the PNG data
58
+
59
+ // Generate an image with DALL-E 3
60
+ const dalle = await ai.image({
61
+ prompt: 'A sunset over mountains',
62
+ provider: 'openai-media',
63
+ model: 'dall-e-3',
44
64
  width: 1024,
45
65
  height: 1024,
46
66
  });
47
- console.log(image.url);
67
+ console.log(dalle.url);
48
68
 
49
69
  // Generate a video
50
70
  const video = await ai.video({
@@ -53,7 +73,7 @@ const video = await ai.video({
53
73
  });
54
74
  console.log(video.url);
55
75
 
56
- // Text-to-speech
76
+ // Text-to-speech with OpenAI TTS — just needs OPENAI_API_KEY
57
77
  const audio = await ai.speak({
58
78
  text: 'Welcome to Noosphere',
59
79
  voice: 'alloy',
@@ -368,6 +388,8 @@ await ai.uninstallModel('deepseek-r1:14b');
368
388
  | Provider | Modality | Models | Source | Auto-Detect |
369
389
  |---|---|---|---|---|
370
390
  | **pi-ai** | LLM | 482 | OpenAI, Anthropic, Google, Groq, Mistral, xAI, OpenRouter, Cerebras | API keys |
391
+ | **openai-media** | image, video, tts, stt | 12 | GPT-Image-1/1.5, DALL-E 2/3, Sora 2/Pro, TTS-1/HD, Whisper | `OPENAI_API_KEY` |
392
+ | **google-media** | image, video, tts | 10 | Imagen 4.0, Veo 2/3/3.1, Gemini TTS (Flash/Pro) | `GEMINI_API_KEY` |
371
393
  | **ollama** | LLM, embedding | 70 | 38 installed + 32 from Ollama web catalog | `localhost:11434` |
372
394
  | **hf-local** | image, video, tts, stt, music | 220 | HuggingFace catalog (FLUX, SDXL, Wan2.2, Whisper, MusicGen) | Always (no API key) |
373
395
  | **huggingface** | LLM, image, tts | dynamic | HuggingFace Inference API | `HUGGINGFACE_TOKEN` |
@@ -400,11 +422,11 @@ await ai.syncModels();
400
422
 
401
423
  | Modality | Providers Synced |
402
424
  |---|---|
403
- | `llm` | pi-ai, ollama, openai-compat, huggingface (cloud, needs API key) |
404
- | `image` | hf-local, comfyui, fal, huggingface (cloud) |
405
- | `video` | hf-local, comfyui, fal |
406
- | `tts` | hf-local (speech models), fal, piper, kokoro, huggingface (cloud) |
407
- | `stt` | hf-local, whisper-local |
425
+ | `llm` | pi-ai, ollama, openai-compat, huggingface (cloud) |
426
+ | `image` | **openai-media** (GPT-Image-1, DALL-E), **google-media** (Imagen 4.0), hf-local, comfyui, fal, huggingface (cloud) |
427
+ | `video` | **openai-media** (Sora 2/Pro), **google-media** (Veo 2/3/3.1), hf-local, comfyui, fal |
428
+ | `tts` | **openai-media** (TTS-1, TTS-1-HD), **google-media** (Gemini TTS), hf-local, fal, piper, kokoro, huggingface (cloud) |
429
+ | `stt` | **openai-media** (Whisper), hf-local, whisper-local |
408
430
  | `music` | hf-local (MusicGen, AudioLDM, etc.), audiocraft |
409
431
  | `embedding` | ollama |
410
432
 
@@ -609,6 +631,102 @@ Noosphere auto-detects all local runtimes on startup:
609
631
 
610
632
  ---
611
633
 
634
+ ## Pre-Request Token Counting
635
+
636
+ Count tokens **before** sending a request to any provider. Know the cost upfront.
637
+
638
+ ```typescript
639
+ // Via Noosphere instance (auto-routes by model)
640
+ const result = await ai.countTokens({
641
+ messages: [
642
+ { role: 'system', content: 'You are a helpful assistant.' },
643
+ { role: 'user', content: 'Explain quantum computing.' },
644
+ ],
645
+ model: 'gpt-4o',
646
+ });
647
+ console.log(result.tokens); // 26
648
+ console.log(result.method); // "tiktoken" (instant, local)
649
+ console.log(result.provider); // "openai"
650
+
651
+ // Google — exact count via API
652
+ const google = await ai.countTokens({
653
+ messages: [{ role: 'user', content: 'Hello!' }],
654
+ model: 'gemini-2.5-flash',
655
+ });
656
+ console.log(google.tokens); // 3
657
+ console.log(google.method); // "api" (exact)
658
+ ```
659
+
660
+ **Token counting by provider:**
661
+
662
+ | Provider | Method | Speed | Accuracy |
663
+ |---|---|---|---|
664
+ | **OpenAI** (GPT-4o, o1, o3, o4, GPT-5) | tiktoken (local) | Instant | Exact |
665
+ | **Google** (Gemini) | `/countTokens` API | ~200ms | Exact |
666
+ | **Anthropic** (Claude) | `/messages/count_tokens` API | ~200ms | Exact |
667
+ | **Groq** (Llama, Mixtral, Gemma) | tiktoken (local) | Instant | Exact |
668
+ | **Cerebras** (Llama) | tiktoken (local) | Instant | Exact |
669
+ | **Mistral** (Mistral, Mixtral, Codestral) | tiktoken (local) | Instant | Close approx |
670
+ | **xAI** (Grok) | tiktoken (local) | Instant | Close approx |
671
+ | **OpenRouter** (all models) | tiktoken (local) | Instant | Close approx |
672
+ | **Ollama** (all local models) | tiktoken (local) | Instant | Close approx |
673
+
674
+ You can also use standalone functions without a Noosphere instance:
675
+
676
+ ```typescript
677
+ import {
678
+ countTokensOpenAI, countTokensGoogle, countTokensAnthropic,
679
+ countTokensGroq, countTokensMistral, countTokensXai,
680
+ countTokensCerebras, countTokensOpenRouter, countTokensOllama,
681
+ } from 'noosphere';
682
+
683
+ // Local (instant, no API key needed)
684
+ const tokens = countTokensOpenAI(messages, 'gpt-4o'); // 26
685
+ const groq = countTokensGroq(messages, 'llama-3.3-70b'); // 26
686
+ const ollama = countTokensOllama(messages, 'qwen3:8b'); // 26
687
+
688
+ // API-based (exact, needs key)
689
+ const google = await countTokensGoogle(messages, GEMINI_KEY, 'gemini-2.5-flash'); // 16
690
+ const claude = await countTokensAnthropic(messages, ANTHROPIC_KEY, 'claude-sonnet-4-20250514'); // exact
691
+ ```
692
+
693
+ ---
694
+
695
+ ## Agent Loop & pi-ai Access
696
+
697
+ Noosphere re-exports the full [pi-ai](https://github.com/nicholasgriffintn/pi-ai) library for direct access to agent loops, tool calling, cost calculation, and streaming APIs.
698
+
699
+ ```typescript
700
+ import {
701
+ agentLoop, calculateCost,
702
+ piStream, piComplete, piStreamSimple, piCompleteSimple,
703
+ setApiKey, getApiKey, getPiModel, getPiModels, getPiProviders,
704
+ } from 'noosphere';
705
+
706
+ // Agent loop with tool calling and preprocessor (compaction hook)
707
+ import type { AgentLoopConfig, AgentContext, AgentTool } from 'noosphere';
708
+
709
+ const config: AgentLoopConfig = {
710
+ model: getPiModel('openai', 'gpt-4o'),
711
+ // Preprocessor runs before each LLM call — use for context compaction
712
+ preprocessor: async (messages) => {
713
+ // Truncate old messages, summarize, etc.
714
+ if (messages.length > 50) {
715
+ return messages.slice(-20); // keep last 20
716
+ }
717
+ return messages;
718
+ },
719
+ };
720
+
721
+ // Calculate cost before sending
722
+ const model = getPiModel('openai', 'gpt-4o');
723
+ const usage = { input: 1000, output: 500, cacheRead: 0, cacheWrite: 0 };
724
+ const cost = calculateCost(model, usage);
725
+ console.log(cost.total); // $0.00625
726
+ ```
727
+
728
+ ---
729
+
612
730
  ## Configuration
613
731
 
614
732
  API keys are resolved from the constructor config or environment variables (config takes priority):