Socket
Socket
Sign inDemoInstall

@axflow/models

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@axflow/models - npm Package Compare versions

Comparing version 0.0.1-beta.0 to 0.0.1-beta.1

8

dist/anthropic/completion.js

@@ -27,3 +27,3 @@ "use strict";

module.exports = __toCommonJS(completion_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");
var ANTHROPIC_API_URL = "https://api.anthropic.com/v1/complete";

@@ -43,3 +43,3 @@ function headers(apiKey, version) {

const url = options.apiUrl || ANTHROPIC_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey, options.version),

@@ -53,3 +53,3 @@ body: JSON.stringify({ ...request, stream: false }),

const url = options.apiUrl || ANTHROPIC_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey, options.version),

@@ -60,3 +60,3 @@ body: JSON.stringify({ ...request, stream: true }),

if (!response.body) {
throw new import_utils.HttpError("Expected response body to be a ReadableStream", response);
throw new import_shared.HttpError("Expected response body to be a ReadableStream", response);
}

@@ -63,0 +63,0 @@ return response.body;

@@ -26,3 +26,3 @@ "use strict";

module.exports = __toCommonJS(embedding_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");

@@ -45,3 +45,3 @@ // src/cohere/shared.ts

const url = options.apiUrl || COHERE_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -48,0 +48,0 @@ body: JSON.stringify(request),

@@ -27,3 +27,3 @@ "use strict";

module.exports = __toCommonJS(generation_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");

@@ -46,3 +46,3 @@ // src/cohere/shared.ts

const url = options.apiUrl || COHERE_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -56,3 +56,3 @@ body: JSON.stringify({ ...request, stream: false }),

const url = options.apiUrl || COHERE_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -63,3 +63,3 @@ body: JSON.stringify({ ...request, stream: true }),

if (!response.body) {
throw new import_utils.HttpError("Expected response body to be a ReadableStream", response);
throw new import_shared.HttpError("Expected response body to be a ReadableStream", response);
}

@@ -66,0 +66,0 @@ return response.body;

@@ -27,3 +27,3 @@ "use strict";

module.exports = __toCommonJS(chat_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");

@@ -83,3 +83,3 @@ // src/openai/shared.ts

const url = options.apiUrl || OPENAI_CHAT_COMPLETIONS_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -93,3 +93,3 @@ body: JSON.stringify({ ...request, stream: false }),

const url = options.apiUrl || OPENAI_CHAT_COMPLETIONS_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -100,3 +100,3 @@ body: JSON.stringify({ ...request, stream: true }),

if (!response.body) {
throw new import_utils.HttpError("Expected response body to be a ReadableStream", response);
throw new import_shared.HttpError("Expected response body to be a ReadableStream", response);
}

@@ -103,0 +103,0 @@ return response.body;

@@ -27,3 +27,3 @@ "use strict";

module.exports = __toCommonJS(completion_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");

@@ -83,3 +83,3 @@ // src/openai/shared.ts

const url = options.apiUrl || OPENAI_COMPLETIONS_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -93,3 +93,3 @@ body: JSON.stringify({ ...request, stream: false }),

const url = options.apiUrl || OPENAI_COMPLETIONS_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -100,3 +100,3 @@ body: JSON.stringify({ ...request, stream: true }),

if (!response.body) {
throw new import_utils.HttpError("Expected response body to be a ReadableStream", response);
throw new import_shared.HttpError("Expected response body to be a ReadableStream", response);
}

@@ -103,0 +103,0 @@ return response.body;

@@ -26,3 +26,3 @@ "use strict";

module.exports = __toCommonJS(embedding_exports);
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");

@@ -45,3 +45,3 @@ // src/openai/shared.ts

const url = options.apiUrl || OPENAI_COMPLETIONS_API_URL;
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers: headers(options.apiKey),

@@ -48,0 +48,0 @@ body: JSON.stringify(request),

import { MessageType, JSONValueType } from '@axflow/models/shared';
interface AccessorType<T = any> {
(value: T): string;
}
type BodyType = Record<string, JSONValueType> | ((message: MessageType, history: MessageType[]) => JSONValueType);
/**
* The options supplied to the useChat hook.
*/
type UseChatOptionsType = {
/**
* The API endpoint to call when submitting a new message.
*
* Defaults to `/api/chat`.
*/
url?: string;
/**
* Customize the request body sent to the API using this value. It accepts
* either a function or object.
*
* If given a function, the return value of the function will become the body
* of the request. The function will be passed the new message as its first
* argument and the history (an array of previous messages) as its second argument.
*
* If given an object, the object will be merged into the request body with the
* new message and the message history, e.g., `{...body, message, history }`.
*
* By default, the request body is `{ message, history }`.
*/
body?: BodyType;
/**
* Additional headers to send along with the request to the API.
*/
headers?: Record<string, string>;
accessor?: AccessorType;
/**
* An accessor used to pluck out the message text. The response body or response
* stream can send back arbitrary values. If the value sent back is not the message
* text, then this component needs a way to access the message text. This function
* is given the value from the API as its input and should return the message text
* as its output.
*
* By default, it assumes the value from the API is the message text itself.
*/
accessor?: (value: any) => string;
/**
* Initial message input. Defaults to empty string.
*/
initialInput?: string;
/**
* Initial message history. Defaults to an empty list.
*/
initialMessages?: MessageType[];
};
declare function useChat(options?: UseChatOptionsType): {
/**
* The result of invoking the useChat hook.
*/
type UseChatResultType = {
/**
* Current user's message input.
*/
input: string;
/**
* The history of messages so far in this chat.
*/
messages: MessageType[];
/**
* A handler to change the user's message input.
*
* @param e Either a form field change event or the string representing the changed user input.
*/
onChange: (e: React.ChangeEvent<HTMLInputElement> | React.ChangeEvent<HTMLTextAreaElement> | string) => void;
/**
* A handler to trigger submission to the API.
*
* @param e Optional `React.FormEvent<HTMLFormElement>` if this value is used with a Form.
*/
onSubmit: (e?: React.FormEvent<HTMLFormElement>) => void;
messages: MessageType[];
};
/**
* A React hook to power LLM chat applications.
*
* This hook supports streaming and non-streaming responses. If streaming, the API
* response must have a content-type header set to `application/x-ndjson; charset=utf-8`.
* Additionally, it must send its JSON chunks using the following format:
*
* { type: 'data' | 'chunk', value: <any valid JSON value> }
*
* When `type` is `chunk`, `value` represents a chunk of the source stream. When `type`
* is `data`, `value` represents any additional data sent along with the source stream.
*
* @param options UseChatOptionsType
* @returns UseChatResultType
*/
declare function useChat(options?: UseChatOptionsType): UseChatResultType;
export { UseChatOptionsType, useChat };
export { UseChatOptionsType, UseChatResultType, useChat };

@@ -29,4 +29,6 @@ "use strict";

var import_react = require("react");
var import_utils = require("@axflow/models/utils");
var import_shared = require("@axflow/models/shared");
function uuid() {
return crypto.randomUUID();
}
async function stableAppend(message, messagesRef, setMessages, url, headers, body, accessor) {

@@ -36,3 +38,3 @@ const history = messagesRef.current;

setMessages(messagesRef.current.concat(message));
const response = await (0, import_utils.POST)(url, {
const response = await (0, import_shared.POST)(url, {
headers,

@@ -49,7 +51,7 @@ body: JSON.stringify(requestBody)

let id = null;
for await (const chunk of (0, import_utils.StreamToIterable)(import_utils.NdJsonStream.decode(responseBody))) {
for await (const chunk of (0, import_shared.StreamToIterable)(import_shared.NdJsonStream.decode(responseBody))) {
let messages = messagesRef.current;
if (chunk.type !== "chunk") {
if (!id) {
id = (0, import_shared.uuid)();
id = uuid();
messages = messages.concat({

@@ -70,3 +72,3 @@ id,

if (!id) {
id = (0, import_shared.uuid)();
id = uuid();
messages = messages.concat({

@@ -90,3 +92,3 @@ id,

const messages = messagesRef.current.concat({
id: (0, import_shared.uuid)(),
id: uuid(),
role: "assistant",

@@ -104,4 +106,5 @@ content,

function useChat(options) {
const [input, setInput] = (0, import_react.useState)("");
const [messages, _setMessages] = (0, import_react.useState)([]);
options = options ?? {};
const [input, setInput] = (0, import_react.useState)(options.initialInput ?? "");
const [messages, _setMessages] = (0, import_react.useState)(options.initialMessages ?? []);
const messagesRef = (0, import_react.useRef)([]);

@@ -115,3 +118,2 @@ const setMessages = (0, import_react.useCallback)(

);
options = options || {};
const url = options.url || DEFAULT_URL;

@@ -136,3 +138,3 @@ const accessor = options.accessor || DEFAULT_ACCESSOR;

append({
id: (0, import_shared.uuid)(),
id: uuid(),
role: "user",

@@ -139,0 +141,0 @@ content: input,

@@ -1,7 +0,12 @@

/**
* Creates a new, random UUID.
*
* @returns UUID
*/
declare function uuid(): `${string}-${string}-${string}-${string}-${string}`;
declare class HttpError extends Error {
readonly code: number;
readonly response: Response;
constructor(message: string, response: Response);
}
declare function isHttpError(e: unknown): e is HttpError;
type HttpOptionsType = RequestInit & {
fetch?: typeof fetch;
};
declare function POST(url: string, options?: HttpOptionsType): Promise<Response>;
type JSONValueType = null | string | number | boolean | {

@@ -18,2 +23,125 @@ [x: string]: JSONValueType;

export { JSONValueType, MessageType, uuid };
/**
* Convert a ReadableStream<T> to an AsyncIterable<T>.
*
* ReadableStreams implement this natively in recent node versions. Unfortunately, older
* node versions, most browsers, and the TypeScript type system do not support it yet.
*
* Example:
*
* for await (const chunk of StreamToIterable(stream)) {
* // Do stuff with chunk
* }
*
* @param stream A ReadableStream.
* @returns An AsyncIterable over the stream contents.
*/
declare function StreamToIterable<T>(stream: ReadableStream<T>): AsyncIterable<T>;
type NdJsonValueType = {
type: 'chunk' | 'data';
value: JSONValueType;
};
/**
* An object that can encode and decode newline-delimited JSON in the following format:
*
* { type: 'data' | 'chunk', value: <any valid JSON value> }
*
* When `type` is `chunk`, `value` represents a chunk of the source stream. When `type`
* is `data`, `value` represents any additional data sent along with the source stream.
*
* @see http://ndjson.org
*/
declare class NdJsonStream {
/**
* These are the proper headers for newline-delimited JSON streams.
*
* @see http://ndjson.org
*/
static headers: Readonly<{
'content-type': "application/x-ndjson; charset=utf-8";
}>;
/**
* Transforms a stream of JSON-serializable objects to stream of newline-delimited JSON.
*
* Each object is wrapped with an object that specifies the `type` and references
* the `value`. The `type` is one of `chunk` or `data`. A type of `chunk` means that
* the `value` corresponds to chunks from the input stream. A type of `data` means
* that the `value` corresponds to the additional data provided as the second argument
* to this function.
*
*
* Example WITHOUT additional data:
*
* const chunk = { key: 'value' };
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }});
* const ndJsonStream = NdJsonStream.encode(stream);
* const entries = [];
* for await (const chunk of stream) {
* entry.push(new TextDecoder().decode(chunk));
* }
* console.log(entries); // [ "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ]
*
*
* Example WITH additional data:
*
* const chunk = { key: 'value' };
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }});
* const ndJsonStream = NdJsonStream.encode(stream, { data: [{ extra: 'data' }] });
* const entries = [];
* for await (const chunk of stream) {
* entry.push(new TextDecoder().decode(chunk));
* }
* console.log(entries); // [ "{\"type\":\"data\",\"value\":{\"extra\":\"data\"}}\n", "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ]
*
* @see http://ndjson.org
*
* @param stream A readable stream of chunks to encode as newline-delimited JSON.
* @param options
* @param options.map A function to map input chunks to output chunks. The return value must be either a JSON-serializable object or a Promise that resolves to a JSON-serializable object.
* @param options.data Additional data to prepend to the output stream.
* @returns A readable stream of newline-delimited JSON.
*/
static encode<T = any>(stream: ReadableStream<T>, options?: {
map?: (value: T) => JSONValueType | Promise<JSONValueType>;
data?: JSONValueType[];
}): ReadableStream<Uint8Array>;
/**
* Transforms a stream of newline-delimited JSON to a stream of objects.
*
* @see http://ndjson.org
*
* @param stream A readable stream of newline-delimited JSON objects.
* @returns A readable stream of objects.
*/
static decode(stream: ReadableStream<Uint8Array>): ReadableStream<NdJsonValueType>;
}
/**
* Returns a `Response` object that streams newline-delimited JSON objects.
*
* Example
*
* export async function POST(request: Request) {
* const req = await request.json();
* const stream = await OpenAIChat.stream(req, { apiKey: OPENAI_API_KEY });
* return StreamingJsonResponse(stream, {
* map: (chunk) => chunk.choices[0].delta.content ?? ''
* });
* }
*
* @see http://ndjson.org
*
* @param stream A readable stream of chunks to encode as newline-delimited JSON.
* @param options
* @param options.status HTTP response status.
* @param options.statusText HTTP response status text.
* @param options.headers HTTP response headers.
* @param options.map A function to map input chunks to output chunks. The return value must be either a JSON-serializable object or a Promise that resolves to a JSON-serializable object.
* @param options.data Additional data to prepend to the output stream.
* @returns A `Response` object that streams newline-delimited JSON objects.
*/
declare function StreamingJsonResponse<T>(stream: ReadableStream<T>, options?: ResponseInit & {
map?: (value: T) => JSONValueType | Promise<JSONValueType>;
data?: JSONValueType[];
}): Response;
export { HttpError, JSONValueType, MessageType, NdJsonStream, NdJsonValueType, POST, StreamToIterable, StreamingJsonResponse, isHttpError };

@@ -23,11 +23,178 @@ "use strict";

__export(shared_exports, {
uuid: () => uuid
HttpError: () => HttpError,
NdJsonStream: () => NdJsonStream,
POST: () => POST,
StreamToIterable: () => StreamToIterable,
StreamingJsonResponse: () => StreamingJsonResponse,
isHttpError: () => isHttpError
});
module.exports = __toCommonJS(shared_exports);
function uuid() {
return crypto.randomUUID();
// src/shared/http.ts
var HttpError = class extends Error {
code;
response;
constructor(message, response) {
super(message);
this.code = response.status;
this.response = response;
}
};
function isHttpError(e) {
return e instanceof HttpError;
}
async function POST(url, options) {
const _fetch = options?.fetch ?? fetch;
if (typeof _fetch === "undefined") {
throw new Error(
"Environment does not support fetch (https://developer.mozilla.org/en-US/docs/Web/API/fetch)"
);
}
const init = { ...options, method: "POST" };
delete init.fetch;
const response = await _fetch(url, init);
if (!response.ok) {
throw new HttpError(`Request failed with status code ${response.status}`, response);
}
return response;
}
// src/shared/stream.ts
function StreamToIterable(stream) {
return stream[Symbol.asyncIterator] ? stream[Symbol.asyncIterator]() : createIterable(stream);
}
async function* createIterable(stream) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
return;
}
yield value;
}
} finally {
reader.releaseLock();
}
}
function identity(value) {
return value;
}
var NdJsonStream = class {
/**
* These are the proper headers for newline-delimited JSON streams.
*
* @see http://ndjson.org
*/
static headers = Object.freeze({ "content-type": "application/x-ndjson; charset=utf-8" });
/**
* Transforms a stream of JSON-serializable objects to stream of newline-delimited JSON.
*
* Each object is wrapped with an object that specifies the `type` and references
* the `value`. The `type` is one of `chunk` or `data`. A type of `chunk` means that
* the `value` corresponds to chunks from the input stream. A type of `data` means
* that the `value` corresponds to the additional data provided as the second argument
* to this function.
*
*
* Example WITHOUT additional data:
*
* const chunk = { key: 'value' };
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }});
* const ndJsonStream = NdJsonStream.encode(stream);
* const entries = [];
* for await (const chunk of stream) {
* entry.push(new TextDecoder().decode(chunk));
* }
* console.log(entries); // [ "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ]
*
*
* Example WITH additional data:
*
* const chunk = { key: 'value' };
* const stream = new ReadableStream({start(con) { con.enqueue(chunk); con.close() }});
* const ndJsonStream = NdJsonStream.encode(stream, { data: [{ extra: 'data' }] });
* const entries = [];
* for await (const chunk of stream) {
* entry.push(new TextDecoder().decode(chunk));
* }
* console.log(entries); // [ "{\"type\":\"data\",\"value\":{\"extra\":\"data\"}}\n", "{\"type\":\"chunk\",\"value\":{\"key\":\"value\"}}\n" ]
*
* @see http://ndjson.org
*
* @param stream A readable stream of chunks to encode as newline-delimited JSON.
* @param options
* @param options.map A function to map input chunks to output chunks. The return value must be either a JSON-serializable object or a Promise that resolves to a JSON-serializable object.
* @param options.data Additional data to prepend to the output stream.
* @returns A readable stream of newline-delimited JSON.
*/
static encode(stream, options) {
options = options || {};
const map = options.map || identity;
const data = options.data || [];
const encoder = new TextEncoder();
function serialize(obj) {
const serialized = JSON.stringify(obj);
return encoder.encode(`${serialized}
`);
}
const ndJsonEncode = new TransformStream({
start(controller) {
for (const value of data) {
controller.enqueue(serialize({ type: "data", value }));
}
},
async transform(chunk, controller) {
const value = await Promise.resolve(map(chunk));
controller.enqueue(serialize({ type: "chunk", value }));
}
});
return stream.pipeThrough(ndJsonEncode);
}
/**
* Transforms a stream of newline-delimited JSON to a stream of objects.
*
* @see http://ndjson.org
*
* @param stream A readable stream of newline-delimited JSON objects.
* @returns A readable stream of objects.
*/
static decode(stream) {
let buffer = [];
const decoder = new TextDecoder();
const parser = new TransformStream({
transform(bytes, controller) {
const chunk = decoder.decode(bytes);
for (let i = 0, len = chunk.length; i < len; ++i) {
const isChunkSeparator = chunk[i] === "\n";
if (!isChunkSeparator) {
buffer.push(chunk[i]);
continue;
}
const line = buffer.join("").trimEnd();
controller.enqueue(JSON.parse(line));
buffer = [];
}
}
});
return stream.pipeThrough(parser);
}
};
function StreamingJsonResponse(stream, options) {
options = options ?? {};
const ndjson = NdJsonStream.encode(stream, { map: options.map, data: options.data });
return new Response(ndjson, {
status: options.status,
statusText: options.statusText,
headers: { ...options.headers, ...NdJsonStream.headers }
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
uuid
HttpError,
NdJsonStream,
POST,
StreamToIterable,
StreamingJsonResponse,
isHttpError
});
{
"name": "@axflow/models",
"version": "0.0.1-beta.0",
"version": "0.0.1-beta.1",
"description": "Zero-dependency module to run, stream, and render results across the most popular LLMs and embedding models",

@@ -88,5 +88,2 @@ "author": "Axilla (https://axilla.io)",

"./dist/shared/index.d.ts"
],
"utils": [
"./dist/utils/index.d.ts"
]

@@ -144,11 +141,5 @@ }

"require": "./dist/shared/index.js"
},
"./utils": {
"types": "./dist/utils/index.d.ts",
"import": "./dist/utils/index.mjs",
"module": "./dist/utils/index.mjs",
"require": "./dist/utils/index.js"
}
},
"gitHead": "c5303f65b61cffb5ef0f85acb98a6291d515d6e7"
"gitHead": "58f70b0f492bbcc1a577527687fd956e5b6fa4ab"
}

@@ -32,3 +32,3 @@ # @axflow/models

import {CohereGenerate} from '@axflow/models/cohere/generate';
import {StreamToIterable} from '@axflow/models/utils';
import {StreamToIterable} from '@axflow/models/shared';

@@ -97,3 +97,3 @@ const gpt4Stream = OpenAIChat.stream(

import { OpenAIChat } from '@axflow/models/openai/chat';
import { StreamToIterable } from '@axflow/models/utils';
import { StreamToIterable } from '@axflow/models/shared';

@@ -179,6 +179,7 @@ const stream = await OpenAIChat.stream(

### @axflow/models/utils
### @axflow/models/shared
```ts
import {StreamToIterable, NdJsonStream, HttpError, isHttpError} from '@axflow/models/anthropic/completion';
import {StreamToIterable, NdJsonStream, StreamingJsonResponse, HttpError, isHttpError} from '@axflow/models/shared';
import type {NdJsonValueType, JSONValueType, MessageType} from '@axflow/models/shared';
```

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc