@axflow/models
Advanced tools
Comparing version 0.0.1-alpha.3 to 0.0.1-alpha.4
@@ -13,3 +13,3 @@ declare namespace AnthropicTypes { | ||
export type RequestOptions = { | ||
apiKey: string; | ||
apiKey?: string; | ||
apiUrl?: string; | ||
@@ -16,0 +16,0 @@ version?: string; |
@@ -32,8 +32,11 @@ "use strict"; | ||
function headers(apiKey, version) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
"content-type": "application/json", | ||
"anthropic-version": version || "2023-06-01", | ||
"x-api-key": apiKey | ||
"anthropic-version": version || "2023-06-01" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2["x-api-key"] = apiKey; | ||
} | ||
return headers2; | ||
} | ||
@@ -40,0 +43,0 @@ async function run(request, options) { |
@@ -0,1 +1,7 @@ | ||
type SharedRequestOptions = { | ||
apiKey?: string; | ||
apiUrl?: string; | ||
fetch?: typeof fetch; | ||
}; | ||
declare namespace CohereEmbeddingTypes { | ||
@@ -7,7 +13,3 @@ type Request = { | ||
}; | ||
type RequestOptions = { | ||
apiKey: string; | ||
apiUrl?: string; | ||
fetch?: typeof fetch; | ||
}; | ||
type RequestOptions = SharedRequestOptions; | ||
type Response = { | ||
@@ -14,0 +16,0 @@ id: string; |
@@ -28,10 +28,17 @@ "use strict"; | ||
var import_utils = require("@axflow/models/utils"); | ||
var COHERE_API_URL = "https://api.cohere.ai/v1/embed"; | ||
// src/cohere/shared.ts | ||
function headers(apiKey) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
"content-type": "application/json", | ||
authorization: `Bearer ${apiKey}` | ||
"content-type": "application/json" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2.authorization = `Bearer ${apiKey}`; | ||
} | ||
return headers2; | ||
} | ||
// src/cohere/embedding.ts | ||
var COHERE_API_URL = "https://api.cohere.ai/v1/embed"; | ||
async function run(request, options) { | ||
@@ -38,0 +45,0 @@ const url = options.apiUrl || COHERE_API_URL; |
@@ -0,1 +1,7 @@ | ||
type SharedRequestOptions = { | ||
apiKey?: string; | ||
apiUrl?: string; | ||
fetch?: typeof fetch; | ||
}; | ||
declare namespace CohereGenerationTypes { | ||
@@ -19,7 +25,3 @@ type Request = { | ||
}; | ||
type RequestOptions = { | ||
apiKey: string; | ||
apiUrl?: string; | ||
fetch?: typeof fetch; | ||
}; | ||
type RequestOptions = SharedRequestOptions; | ||
type Generation = { | ||
@@ -49,3 +51,3 @@ id: string; | ||
type Chunk = { | ||
text: string; | ||
text?: string; | ||
is_finished: boolean; | ||
@@ -52,0 +54,0 @@ finished_reason?: 'COMPLETE' | 'MAX_TOKENS' | 'ERROR' | 'ERROR_TOXIC'; |
@@ -30,10 +30,17 @@ "use strict"; | ||
var import_utils = require("@axflow/models/utils"); | ||
var COHERE_API_URL = "https://api.cohere.ai/v1/generate"; | ||
// src/cohere/shared.ts | ||
function headers(apiKey) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
"content-type": "application/json", | ||
authorization: `Bearer ${apiKey}` | ||
"content-type": "application/json" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2.authorization = `Bearer ${apiKey}`; | ||
} | ||
return headers2; | ||
} | ||
// src/cohere/generation.ts | ||
var COHERE_API_URL = "https://api.cohere.ai/v1/generate"; | ||
async function run(request, options) { | ||
@@ -76,7 +83,3 @@ const url = options.apiUrl || COHERE_API_URL; | ||
try { | ||
const event = JSON.parse(line); | ||
if (typeof event.text !== "string") { | ||
event.text = ""; | ||
} | ||
return event; | ||
return JSON.parse(line); | ||
} catch (error) { | ||
@@ -83,0 +86,0 @@ throw new Error( |
type SharedRequestOptions = { | ||
apiKey: string; | ||
apiKey?: string; | ||
apiUrl?: string; | ||
@@ -4,0 +4,0 @@ fetch?: typeof fetch; |
@@ -33,7 +33,10 @@ "use strict"; | ||
function headers(apiKey) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
authorization: `Bearer ${apiKey}`, | ||
"content-type": "application/json" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2.authorization = `Bearer ${apiKey}`; | ||
} | ||
return headers2; | ||
} | ||
@@ -40,0 +43,0 @@ function streamTransformer() { |
type SharedRequestOptions = { | ||
apiKey: string; | ||
apiKey?: string; | ||
apiUrl?: string; | ||
@@ -4,0 +4,0 @@ fetch?: typeof fetch; |
@@ -33,7 +33,10 @@ "use strict"; | ||
function headers(apiKey) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
authorization: `Bearer ${apiKey}`, | ||
"content-type": "application/json" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2.authorization = `Bearer ${apiKey}`; | ||
} | ||
return headers2; | ||
} | ||
@@ -40,0 +43,0 @@ function streamTransformer() { |
type SharedRequestOptions = { | ||
apiKey: string; | ||
apiKey?: string; | ||
apiUrl?: string; | ||
@@ -4,0 +4,0 @@ fetch?: typeof fetch; |
@@ -31,7 +31,10 @@ "use strict"; | ||
function headers(apiKey) { | ||
return { | ||
const headers2 = { | ||
accept: "application/json", | ||
authorization: `Bearer ${apiKey}`, | ||
"content-type": "application/json" | ||
}; | ||
if (typeof apiKey === "string") { | ||
headers2.authorization = `Bearer ${apiKey}`; | ||
} | ||
return headers2; | ||
} | ||
@@ -38,0 +41,0 @@ |
{ | ||
"name": "@axflow/models", | ||
"version": "0.0.1-alpha.3", | ||
"version": "0.0.1-alpha.4", | ||
"description": "Zero-dependency module to run, stream, and render results across the most popular LLMs and embedding models", | ||
@@ -123,3 +123,3 @@ "author": "Axilla (https://axilla.io)", | ||
}, | ||
"gitHead": "ecd50ae048b20029a0d97f9562a0d67b144d4d86" | ||
"gitHead": "ad74308aa99d91c2b2736415cfe4ed497b13dac2" | ||
} |
@@ -11,16 +11,17 @@ # @axflow/models | ||
* Zero-dependency, lightweight package to consume all the most popular models and APIs | ||
* First-class streaming support with both low-level byte streams or higher-level parsed chunks | ||
* Supports Node 17.5.0+, browsers, ESM, CJS, and more environments | ||
* Supports custom `fetch` implementation, allowing for older node environments or request middleware (e.g., logging) | ||
* Zero-dependency, lightweight package to consume all the most popular LLMs, embedding models, and more | ||
* Built exclusively on modern web standards such as `fetch` and the stream APIs | ||
* First-class streaming support with both low-level byte streams or higher-level JavaScript objects | ||
* Supports Node 18+, Next.js serverless or edge runtime, browsers, ESM, CJS, and more | ||
* Supports a custom `fetch` implementation for request middleware (e.g., custom headers, logging) | ||
## Supported models | ||
- [X] OpenAI and OpenAI-compatible Chat, Completion, and Embedding models | ||
- [X] Cohere and Cohere-compatible generation and embedding models | ||
- [X] Anthropic and Anthropic-compatible completion models | ||
- [ ] Google PaLM models | ||
- [ ] Azure OpenAI | ||
- [ ] Replicate | ||
- [ ] HuggingFace | ||
- ✅ OpenAI and OpenAI-compatible Chat, Completion, and Embedding models | ||
- ✅ Cohere and Cohere-compatible Generation and Embedding models | ||
- ✅ Anthropic and Anthropic-compatible Completion models | ||
- Google PaLM models (coming soon) | ||
- Azure OpenAI (coming soon) | ||
- Replicate (coming soon) | ||
- HuggingFace (coming soon) | ||
@@ -65,24 +66,49 @@ ## Basic Usage | ||
In NextJS, it's as simple as: | ||
## Next.js edge proxy example | ||
The server intercepts the request on the edge, adds the proper API key, and forwards the byte stream back to the client. | ||
*Note this pattern works exactly the same with our other models that support streaming, like Cohere and Anthropic.* | ||
```ts | ||
import {OpenAIChat} from '@axflow/models/openai/chat'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { OpenAIChat } from '@axflow/models/openai/chat'; | ||
export const runtime = 'edge' | ||
// POST /api/openai/chat | ||
export async function POST(request: NextRequest) { | ||
const chatRequest = await request.json(); | ||
// Byte stream is more efficient here because we do not parse the stream and | ||
// re-encode it, but rather just pass the bytes directly through to the client. | ||
const stream = await OpenAIChat.streamBytes( | ||
{ | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'What is the Eiffel tower?' }], | ||
}, | ||
{ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}, | ||
); | ||
// re-encode it, but rather pass the bytes directly through to the client. | ||
const stream = await OpenAIChat.streamBytes(chatRequest, { | ||
apiKey: process.env.OPENAI_API_KEY!, | ||
}); | ||
return new Response(stream); | ||
return new NextResponse(stream); | ||
} | ||
``` | ||
On the client, we can use `OpenAIChat.stream` with a custom `apiUrl` in place of the `apiKey` that points to our Next.js edge route. | ||
```ts | ||
import { OpenAIChat } from '@axflow/models/openai/chat'; | ||
import { StreamToIterable } from '@axflow/models/utils'; | ||
const stream = await OpenAIChat.stream( | ||
{ | ||
model: 'gpt-4', | ||
messages: [{ role: 'user', content: 'What is the Eiffel tower?' }], | ||
}, | ||
{ | ||
apiUrl: "/api/openai/chat", | ||
} | ||
); | ||
for await (const chunk of StreamToIterable(stream)) { | ||
console.log(chunk.choices[0].delta.content); | ||
} | ||
``` | ||
## API | ||
@@ -145,1 +171,7 @@ | ||
``` | ||
### @axflow/models/utils | ||
```ts | ||
import {StreamToIterable, HttpError, isHttpError} from '@axflow/models/anthropic/completion'; | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65543
1467
175