@djangocfg/llm 2.1.164

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.
Files changed (45) hide show
  1. package/README.md +181 -0
  2. package/dist/index.cjs +1164 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +164 -0
  5. package/dist/index.d.ts +164 -0
  6. package/dist/index.mjs +1128 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/dist/providers/index.cjs +317 -0
  9. package/dist/providers/index.cjs.map +1 -0
  10. package/dist/providers/index.d.cts +30 -0
  11. package/dist/providers/index.d.ts +30 -0
  12. package/dist/providers/index.mjs +304 -0
  13. package/dist/providers/index.mjs.map +1 -0
  14. package/dist/sdkrouter-D8GMBmTi.d.ts +171 -0
  15. package/dist/sdkrouter-hlQlVd0v.d.cts +171 -0
  16. package/dist/text-utils-DoYqMIr6.d.ts +289 -0
  17. package/dist/text-utils-VXWN-8Oq.d.cts +289 -0
  18. package/dist/translator/index.cjs +794 -0
  19. package/dist/translator/index.cjs.map +1 -0
  20. package/dist/translator/index.d.cts +24 -0
  21. package/dist/translator/index.d.ts +24 -0
  22. package/dist/translator/index.mjs +769 -0
  23. package/dist/translator/index.mjs.map +1 -0
  24. package/dist/types-D6lazgm1.d.cts +59 -0
  25. package/dist/types-D6lazgm1.d.ts +59 -0
  26. package/package.json +82 -0
  27. package/src/client.ts +119 -0
  28. package/src/index.ts +70 -0
  29. package/src/providers/anthropic.ts +98 -0
  30. package/src/providers/base.ts +90 -0
  31. package/src/providers/index.ts +15 -0
  32. package/src/providers/openai.ts +73 -0
  33. package/src/providers/sdkrouter.ts +279 -0
  34. package/src/translator/cache.ts +237 -0
  35. package/src/translator/index.ts +55 -0
  36. package/src/translator/json-translator.ts +408 -0
  37. package/src/translator/prompts.ts +90 -0
  38. package/src/translator/text-utils.ts +148 -0
  39. package/src/translator/types.ts +112 -0
  40. package/src/translator/validator.ts +181 -0
  41. package/src/types.ts +85 -0
  42. package/src/utils/env.ts +67 -0
  43. package/src/utils/index.ts +2 -0
  44. package/src/utils/json.ts +44 -0
  45. package/src/utils/schema.ts +153 -0
package/README.md ADDED
@@ -0,0 +1,181 @@
1
+ # @djangocfg/llm
2
+
3
+ LLM client with SDKRouter, OpenAI, and Anthropic support. Includes JSON translator with validation.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @djangocfg/llm
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { createLLMClient, Model, createTranslator } from '@djangocfg/llm'
15
+
16
+ // Auto-detect from env (SDKRouter preferred)
17
+ const llm = createLLMClient()
18
+
19
+ // With model alias
20
+ const llm = createLLMClient({ model: Model.balanced({ code: true }) })
21
+
22
+ // Simple chat
23
+ const response = await llm.chat('Hello!')
24
+
25
+ // JSON response
26
+ const data = await llm.json<{ name: string }>('Return a user object')
27
+
28
+ // Translate JSON
29
+ const translator = createTranslator(llm)
30
+ const result = await translator.translate({ title: 'Hello' }, 'ru')
31
+ console.log(result.data) // { title: 'Привет' }
32
+ ```
33
+
34
+ ## Environment Variables
35
+
36
+ ```bash
37
+ # SDKRouter (recommended)
38
+ SDKROUTER_API_KEY=sk-...
39
+
40
+ # Or OpenAI
41
+ OPENAI_API_KEY=sk-...
42
+
43
+ # Or Anthropic
44
+ ANTHROPIC_API_KEY=sk-ant-...
45
+ ```
46
+
47
+ Priority: `SDKROUTER_API_KEY` > `OPENAI_API_KEY` > `ANTHROPIC_API_KEY`
48
+
49
+ ## Model Aliases (SDKRouter)
50
+
51
+ SDKRouter uses smart model aliases to select the best model for your task:
52
+
53
+ ```ts
54
+ import { Model, ModelPresets } from '@djangocfg/llm'
55
+
56
+ // Tier methods
57
+ Model.cheap() // '@cheap' - Cheapest
58
+ Model.balanced() // '@balanced' - Best value (default)
59
+ Model.smart() // '@smart' - Highest quality
60
+ Model.fast() // '@fast' - Lowest latency
61
+
62
+ // With capabilities
63
+ Model.cheap({ vision: true }) // '@cheap+vision'
64
+ Model.balanced({ tools: true }) // '@balanced+tools'
65
+ Model.smart({ json: true }) // '@smart+json'
66
+
67
+ // With categories
68
+ Model.balanced({ code: true }) // '@balanced+code'
69
+ Model.smart({ reasoning: true }) // '@smart+reasoning'
70
+
71
+ // Pre-built presets
72
+ ModelPresets.translation // '@cheap+json' - For translation tasks
73
+ ModelPresets.code // '@balanced+code' - For code generation
74
+ ModelPresets.reasoning // '@smart+reasoning' - For complex reasoning
75
+ ```
76
+
77
+ ### Available Tiers
78
+
79
+ - `cheap` - Cheapest available model
80
+ - `budget` - Budget-friendly with decent quality
81
+ - `standard` - Standard tier
82
+ - `balanced` - Best quality/price ratio (recommended)
83
+ - `smart` - Highest quality model
84
+ - `fast` - Lowest latency model
85
+ - `premium` - Top-tier premium model
86
+
87
+ ### Available Capabilities
88
+
89
+ - `vision` - Image understanding
90
+ - `tools` - Function/tool calling
91
+ - `agents` - Agent tool calling (verified)
92
+ - `json` - JSON mode
93
+ - `streaming` - Streaming support
94
+ - `long` - Long context (128k+)
95
+ - `image` - Image generation
96
+
97
+ ### Available Categories
98
+
99
+ - `code` - Code generation
100
+ - `reasoning` - Reasoning & math
101
+ - `creative` - Creative writing
102
+ - `chat` - Conversational
103
+ - `analysis` - Analysis & extraction
104
+
105
+ ## API
106
+
107
+ ### createLLMClient(config?)
108
+
109
+ ```ts
110
+ const llm = createLLMClient({
111
+ provider: 'sdkrouter', // 'sdkrouter' | 'openai' | 'anthropic'
112
+ apiKey: 'sk-...', // Optional, uses env
113
+ model: '@balanced', // Default model
114
+ temperature: 0.1,
115
+ maxTokens: 4096,
116
+ })
117
+ ```
118
+
119
+ ### llm.chat(prompt, options?)
120
+
121
+ ```ts
122
+ const response = await llm.chat('Translate to Russian: Hello', {
123
+ temperature: 0,
124
+ system: 'You are a professional translator',
125
+ })
126
+
127
+ console.log(response.content) // 'Привет'
128
+ console.log(response.usage) // { promptTokens, completionTokens, totalTokens }
129
+ ```
130
+
131
+ ### llm.json<T>(prompt, options?)
132
+
133
+ ```ts
134
+ interface User {
135
+ name: string
136
+ email: string
137
+ }
138
+
139
+ const user = await llm.json<User>('Return a sample user object')
140
+ ```
141
+
142
+ ### createTranslator(llm)
143
+
144
+ ```ts
145
+ const translator = createTranslator(llm)
146
+
147
+ const result = await translator.translate(
148
+ { title: 'Hello', items: ['One', 'Two'] },
149
+ 'ru',
150
+ {
151
+ sourceLanguage: 'en',
152
+ maxRetries: 2
153
+ }
154
+ )
155
+
156
+ if (result.valid) {
157
+ console.log(result.data) // { title: 'Привет', items: ['Один', 'Два'] }
158
+ } else {
159
+ console.error('Errors:', result.errors)
160
+ }
161
+ ```
162
+
163
+ ## Providers
164
+
165
+ ### SDKRouter (Default)
166
+
167
+ - Base URL: `https://llm.sdkrouter.com/v1`
168
+ - Uses smart model aliases (@cheap, @balanced, @smart)
169
+ - Automatically selects best model for capabilities
170
+
171
+ ### OpenAI
172
+
173
+ Models: `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `gpt-3.5-turbo`
174
+
175
+ ### Anthropic
176
+
177
+ Models: `claude-3-5-sonnet-latest`, `claude-3-5-haiku-latest`, `claude-3-opus-latest`
178
+
179
+ ## License
180
+
181
+ MIT