
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@axflow/models
Advanced tools
Zero-dependency, modular SDK for building robust natural language applications
Zero-dependency, modular SDK for building robust natural language applications.
npm i @axflow/models
fetch
and the stream APIsfetch
implementation for request middleware (e.g., custom headers, logging)View the Guides or the reference:
import { OpenAIChat } from '@axflow/models/openai/chat';
import { CohereGenerate } from '@axflow/models/cohere/generate';
import { StreamToIterable } from '@axflow/models/shared';
const gpt4Stream = OpenAIChat.stream(
{
model: 'gpt-4',
messages: [{ role: 'user', content: 'What is the Eiffel tower?' }],
},
{
apiKey: '<openai api key>',
},
);
const cohereStream = CohereGenerate.stream(
{
model: 'command-nightly',
prompt: 'What is the Eiffel tower?',
},
{
apiKey: '<cohere api key>',
},
);
// StreamToIterable is optional in recent node versions as
// ReadableStreams already implement the async iterator protocol
for await (const chunk of StreamToIterable(gpt4Stream)) {
console.log(chunk.choices[0].delta.content);
}
for await (const chunk of StreamToIterable(cohereStream)) {
console.log(chunk.text);
}
For models that support streaming, there is a convenience method for streaming only the string tokens.
import { OpenAIChat } from '@axflow/models/openai/chat';
const tokenStream = OpenAIChat.streamTokens(
{
model: 'gpt-4',
messages: [{ role: 'user', content: 'What is the Eiffel tower?' }],
},
{
apiKey: '<openai api key>',
},
);
// Example stdout output:
//
// The Eiffel Tower is a renowned wrought-iron landmark located in Paris, France, known globally as a symbol of romance and elegance.
//
for await (const token of tokenStream) {
process.stdout.write(token);
}
process.stdout.write('\n');
useChat
hook for dead simple UI integrationWe've made building chat and completion UIs trivial. It doesn't get any easier than this 🚀
///////////////////
// On the server //
///////////////////
import { OpenAIChat } from '@axflow/models/openai/chat';
import { StreamingJsonResponse, type MessageType } from '@axflow/models/shared';
export const runtime = 'edge';
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = await OpenAIChat.streamTokens(
{
model: 'gpt-4',
messages: messages.map((msg: MessageType) => ({ role: msg.role, content: msg.content })),
},
{
apiKey: process.env.OPENAI_API_KEY!,
},
);
return new StreamingJsonResponse(stream);
}
///////////////////
// On the client //
///////////////////
import { useChat } from '@axflow/models/react';
function ChatComponent() {
const { input, messages, onChange, onSubmit } = useChat();
return (
<>
<Messages messages={messages} />
<Form input={input} onChange={onChange} onSubmit={onSubmit} />
</>
);
}
Sometimes you just want to create a proxy to the underlying LLM API. In this 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.
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();
// We'll stream the bytes from OpenAI directly to the client
const stream = await OpenAIChat.streamBytes(chatRequest, {
apiKey: process.env.OPENAI_API_KEY!,
});
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.
DO NOT expose api keys to your frontend.
import { OpenAIChat } from '@axflow/models/openai/chat';
import { StreamToIterable } from '@axflow/models/shared';
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);
}
Uses express + React hook on frontend.
///////////////////
// On the server //
///////////////////
const express = require('express');
const { OpenAIChat } = require('@axflow/models/openai/chat');
const { streamJsonResponse } = require('@axflow/models/node');
const app = express();
app.use(express.json());
app.post('/api/chat', async (req, res) => {
const { messages } = req.body;
const stream = await OpenAIChat.streamTokens(
{
model: 'gpt-3.5-turbo',
messages: messages.map((msg) => ({
role: msg.role,
content: msg.content,
})),
},
{
apiKey: process.env.OPENAI_API_KEY,
},
);
return streamJsonResponse(stream, res);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
///////////////////
// On the client //
///////////////////
import { useChat } from '@axflow/models/react';
function ChatComponent() {
const { input, messages, onChange, onSubmit } = useChat();
return (
<>
<Messages messages={messages} />
<Form input={input} onChange={onChange} onSubmit={onSubmit} />
</>
);
}
FAQs
Zero-dependency, modular SDK for building robust natural language applications
The npm package @axflow/models receives a total of 155 weekly downloads. As such, @axflow/models popularity was classified as not popular.
We found that @axflow/models demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.