Comparing version 0.1.24 to 0.2.0
import * as t from "polyfact-io-ts"; | ||
import { GenerationOptions } from "../generate"; | ||
import { ClientOptions } from "../clientOpts"; | ||
import { Memory } from "../memory"; | ||
declare const Message: t.TypeC<{ | ||
@@ -12,14 +9,9 @@ id: t.StringC; | ||
}>; | ||
export declare function createChat(systemPrompt?: string, options?: Partial<ClientOptions>): Promise<string>; | ||
export declare class Chat { | ||
chatId: Promise<string>; | ||
provider: "openai" | "cohere"; | ||
clientOptions: ClientOptions; | ||
autoMemory?: Memory; | ||
constructor(options?: { | ||
provider?: "openai" | "cohere"; | ||
systemPrompt?: string; | ||
autoMemory?: boolean; | ||
}, clientOptions?: Partial<ClientOptions>); | ||
sendMessageWithTokenUsage(message: string, options?: GenerationOptions): Promise<{ | ||
}); | ||
sendMessageWithTokenUsage(message: string): Promise<{ | ||
result: string; | ||
@@ -31,12 +23,5 @@ tokenUsage: { | ||
}>; | ||
sendMessage(message: string, options?: GenerationOptions): Promise<string>; | ||
sendMessage(message: string): Promise<string>; | ||
getMessages(): Promise<t.TypeOf<typeof Message>[]>; | ||
} | ||
export default function client(clientOptions?: Partial<ClientOptions>): { | ||
createChat: (systemPrompt?: string) => Promise<string>; | ||
Chat: (options?: { | ||
provider?: "openai" | "cohere"; | ||
systemPrompt?: string; | ||
}) => Chat; | ||
}; | ||
export {}; |
@@ -29,8 +29,17 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Chat = exports.createChat = void 0; | ||
exports.Chat = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
const t = __importStar(require("polyfact-io-ts")); | ||
const ensurePolyfactToken_1 = require("../helpers/ensurePolyfactToken"); | ||
const generate_1 = require("../generate"); | ||
const clientOpts_1 = require("../clientOpts"); | ||
const memory_1 = require("../memory"); | ||
const { POLYFACT_ENDPOINT = "https://api2.polyfact.com", POLYFACT_TOKEN = "" } = process.env; | ||
async function createChat() { | ||
(0, ensurePolyfactToken_1.ensurePolyfactToken)(); | ||
const response = await axios_1.default.post(`${POLYFACT_ENDPOINT}/chats`, {}, { | ||
headers: { | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
}); | ||
return response?.data?.id; | ||
} | ||
const Message = t.type({ | ||
@@ -43,41 +52,20 @@ id: t.string, | ||
}); | ||
async function createChat(systemPrompt, options = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(options); | ||
const response = await axios_1.default.post(`${endpoint}/chats`, { system_prompt: systemPrompt }, { | ||
headers: { | ||
"X-Access-Token": token, | ||
}, | ||
}); | ||
return response?.data?.id; | ||
} | ||
exports.createChat = createChat; | ||
class Chat { | ||
constructor(options = {}, clientOptions = {}) { | ||
this.clientOptions = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
this.chatId = createChat(options.systemPrompt, this.clientOptions); | ||
constructor(options = {}) { | ||
this.chatId = createChat(); | ||
this.provider = options.provider || "openai"; | ||
if (options.autoMemory) { | ||
this.autoMemory = new memory_1.Memory(this.clientOptions); | ||
} | ||
} | ||
async sendMessageWithTokenUsage(message, options = {}) { | ||
async sendMessageWithTokenUsage(message) { | ||
const chatId = await this.chatId; | ||
if (this.autoMemory && !options.memory && !options.memoryId) { | ||
options.memory = this.autoMemory; | ||
} | ||
const result = await (0, generate_1.generateWithTokenUsage)(message, { ...options, chatId }, this.clientOptions); | ||
if (this.autoMemory) { | ||
this.autoMemory.add(`Human: ${message}`); | ||
this.autoMemory.add(`AI: ${result.result}`); | ||
} | ||
const result = await (0, generate_1.generateWithTokenUsage)(message, { chatId }); | ||
return result; | ||
} | ||
async sendMessage(message, options = {}) { | ||
const result = await this.sendMessageWithTokenUsage(message, options); | ||
async sendMessage(message) { | ||
const result = await this.sendMessageWithTokenUsage(message); | ||
return result.result; | ||
} | ||
async getMessages() { | ||
const response = await axios_1.default.get(`${this.clientOptions.endpoint}/chat/${await this.chatId}/history`, { | ||
const response = await axios_1.default.get(`${POLYFACT_ENDPOINT}/chat/${await this.chatId}/history`, { | ||
headers: { | ||
"X-Access-Token": this.clientOptions.token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -89,8 +77,1 @@ }); | ||
exports.Chat = Chat; | ||
function client(clientOptions = {}) { | ||
return { | ||
createChat: (systemPrompt) => createChat(systemPrompt, clientOptions), | ||
Chat: (options) => new Chat(options, clientOptions), | ||
}; | ||
} | ||
exports.default = client; |
@@ -1,2 +0,1 @@ | ||
import { ClientOptions } from "./clientOpts"; | ||
import { Memory } from "./memory"; | ||
@@ -8,5 +7,4 @@ export type GenerationOptions = { | ||
memoryId?: string; | ||
stop?: string[]; | ||
}; | ||
export declare function generateWithTokenUsage(task: string, options?: GenerationOptions, clientOptions?: Partial<ClientOptions>): Promise<{ | ||
declare function generateWithTokenUsage(task: string, options?: GenerationOptions): Promise<{ | ||
result: string; | ||
@@ -18,12 +16,3 @@ tokenUsage: { | ||
}>; | ||
export declare function generate(task: string, options?: GenerationOptions, clientOptions?: Partial<ClientOptions>): Promise<string>; | ||
export default function client(clientOptions?: Partial<ClientOptions>): { | ||
generateWithTokenUsage: (task: string, options?: GenerationOptions) => Promise<{ | ||
result: string; | ||
tokenUsage: { | ||
input: number; | ||
output: number; | ||
}; | ||
}>; | ||
generate: (task: string, options?: GenerationOptions) => Promise<string>; | ||
}; | ||
declare function generate(task: string, options?: GenerationOptions): Promise<string>; | ||
export { generate, generateWithTokenUsage }; |
@@ -29,6 +29,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generate = exports.generateWithTokenUsage = void 0; | ||
exports.generateWithTokenUsage = exports.generate = void 0; | ||
const axios_1 = __importDefault(require("axios")); | ||
const t = __importStar(require("polyfact-io-ts")); | ||
const clientOpts_1 = require("./clientOpts"); | ||
const ensurePolyfactToken_1 = require("./helpers/ensurePolyfactToken"); | ||
const { POLYFACT_ENDPOINT = "https://api2.polyfact.com", POLYFACT_TOKEN = "" } = process.env; | ||
class GenerationError extends Error { | ||
@@ -57,4 +58,4 @@ constructor(errorType) { | ||
}); | ||
async function generateWithTokenUsage(task, options = {}, clientOptions = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
async function generateWithTokenUsage(task, options = {}) { | ||
(0, ensurePolyfactToken_1.ensurePolyfactToken)(); | ||
const requestBody = { | ||
@@ -65,9 +66,8 @@ task, | ||
chat_id: options?.chatId || "", | ||
stop: options?.stop || [], | ||
}; | ||
try { | ||
const res = await axios_1.default.post(`${endpoint}/generate`, requestBody, { | ||
const res = await axios_1.default.post(`${POLYFACT_ENDPOINT}/generate`, requestBody, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Access-Token": token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -89,13 +89,6 @@ }); | ||
exports.generateWithTokenUsage = generateWithTokenUsage; | ||
async function generate(task, options = {}, clientOptions = {}) { | ||
const res = await generateWithTokenUsage(task, options, clientOptions); | ||
async function generate(task, options = {}) { | ||
const res = await generateWithTokenUsage(task, options); | ||
return res.result; | ||
} | ||
exports.generate = generate; | ||
function client(clientOptions = {}) { | ||
return { | ||
generateWithTokenUsage: (task, options = {}) => generateWithTokenUsage(task, options, clientOptions), | ||
generate: (task, options = {}) => generate(task, options, clientOptions), | ||
}; | ||
} | ||
exports.default = client; |
@@ -1,3 +0,1 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import * as t from "polyfact-io-ts"; | ||
@@ -7,49 +5,5 @@ import { generate, generateWithTokenUsage, GenerationOptions } from "./generate"; | ||
import { transcribe } from "./transcribe"; | ||
import { splitString, tokenCount } from "./split"; | ||
import { Memory, createMemory, updateMemory, getAllMemories } from "./memory"; | ||
import { Chat } from "./chats"; | ||
import { Memory, createMemory, updateMemory, getAllMemories } from "./memory"; | ||
import { splitString, tokenCount } from "./split"; | ||
import { ClientOptions } from "./clientOpts"; | ||
import { get as KVGet, set as KVSet } from "./kv"; | ||
declare const kv: { | ||
get: typeof KVGet; | ||
set: typeof KVSet; | ||
}; | ||
export { generate, generateWithTokenUsage, generateWithType, generateWithTypeWithTokenUsage, splitString, tokenCount, t, GenerationOptions, transcribe, createMemory, updateMemory, getAllMemories, Chat, Memory, kv, }; | ||
export default function client(clientOptions: Partial<ClientOptions>): { | ||
kv: { | ||
get: (key: string) => Promise<string>; | ||
set: (key: string, value: string) => Promise<void>; | ||
}; | ||
createChat: (systemPrompt?: string | undefined) => Promise<string>; | ||
Chat: (options?: { | ||
provider?: "openai" | "cohere" | undefined; | ||
systemPrompt?: string | undefined; | ||
} | undefined) => Chat; | ||
createMemory: () => Promise<{ | ||
id: string; | ||
}>; | ||
updateMemory: (id: string, input: string, maxToken?: number | undefined) => Promise<{ | ||
success: boolean; | ||
}>; | ||
getAllMemories: () => Promise<{ | ||
ids: string[]; | ||
}>; | ||
Memory: () => Memory; | ||
transcribe: (file: import("stream").Readable | Buffer) => Promise<string>; | ||
generateWithTypeWithTokenUsage: <T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions) => Promise<{ | ||
result: { [K in keyof T]: t.TypeOf<T[K]>; }; | ||
tokenUsage: { | ||
input: number; | ||
output: number; | ||
}; | ||
}>; | ||
generateWithType: <T_1 extends t.Props>(task: string, type: t.TypeC<T_1>, options?: GenerationOptions) => Promise<{ [K_1 in keyof T_1]: t.TypeOf<T_1[K_1]>; }>; | ||
generateWithTokenUsage: (task: string, options?: GenerationOptions) => Promise<{ | ||
result: string; | ||
tokenUsage: { | ||
input: number; | ||
output: number; | ||
}; | ||
}>; | ||
generate: (task: string, options?: GenerationOptions) => Promise<string>; | ||
}; | ||
export { generate, generateWithTokenUsage, generateWithType, generateWithTypeWithTokenUsage, splitString, tokenCount, t, GenerationOptions, transcribe, createMemory, updateMemory, getAllMemories, Chat, Memory, }; |
@@ -26,16 +26,17 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.kv = exports.Memory = exports.Chat = exports.getAllMemories = exports.updateMemory = exports.createMemory = exports.transcribe = exports.t = exports.tokenCount = exports.splitString = exports.generateWithTypeWithTokenUsage = exports.generateWithType = exports.generateWithTokenUsage = exports.generate = void 0; | ||
exports.Memory = exports.Chat = exports.getAllMemories = exports.updateMemory = exports.createMemory = exports.transcribe = exports.t = exports.tokenCount = exports.splitString = exports.generateWithTypeWithTokenUsage = exports.generateWithType = exports.generateWithTokenUsage = exports.generate = void 0; | ||
const t = __importStar(require("polyfact-io-ts")); | ||
exports.t = t; | ||
const generate_1 = __importStar(require("./generate")); | ||
const generate_1 = require("./generate"); | ||
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_1.generate; } }); | ||
Object.defineProperty(exports, "generateWithTokenUsage", { enumerable: true, get: function () { return generate_1.generateWithTokenUsage; } }); | ||
const generateWithType_1 = __importStar(require("./probabilistic_helpers/generateWithType")); | ||
const generateWithType_1 = require("./probabilistic_helpers/generateWithType"); | ||
Object.defineProperty(exports, "generateWithType", { enumerable: true, get: function () { return generateWithType_1.generateWithType; } }); | ||
Object.defineProperty(exports, "generateWithTypeWithTokenUsage", { enumerable: true, get: function () { return generateWithType_1.generateWithTypeWithTokenUsage; } }); | ||
const transcribe_1 = __importStar(require("./transcribe")); | ||
const transcribe_1 = require("./transcribe"); | ||
Object.defineProperty(exports, "transcribe", { enumerable: true, get: function () { return transcribe_1.transcribe; } }); | ||
const chats_1 = __importStar(require("./chats")); | ||
Object.defineProperty(exports, "Chat", { enumerable: true, get: function () { return chats_1.Chat; } }); | ||
const memory_1 = __importStar(require("./memory")); | ||
const split_1 = require("./split"); | ||
Object.defineProperty(exports, "splitString", { enumerable: true, get: function () { return split_1.splitString; } }); | ||
Object.defineProperty(exports, "tokenCount", { enumerable: true, get: function () { return split_1.tokenCount; } }); | ||
const memory_1 = require("./memory"); | ||
Object.defineProperty(exports, "Memory", { enumerable: true, get: function () { return memory_1.Memory; } }); | ||
@@ -45,21 +46,3 @@ Object.defineProperty(exports, "createMemory", { enumerable: true, get: function () { return memory_1.createMemory; } }); | ||
Object.defineProperty(exports, "getAllMemories", { enumerable: true, get: function () { return memory_1.getAllMemories; } }); | ||
const split_1 = require("./split"); | ||
Object.defineProperty(exports, "splitString", { enumerable: true, get: function () { return split_1.splitString; } }); | ||
Object.defineProperty(exports, "tokenCount", { enumerable: true, get: function () { return split_1.tokenCount; } }); | ||
const kv_1 = __importStar(require("./kv")); | ||
const kv = { | ||
get: kv_1.get, | ||
set: kv_1.set, | ||
}; | ||
exports.kv = kv; | ||
function client(clientOptions) { | ||
return { | ||
...(0, generate_1.default)(clientOptions), | ||
...(0, generateWithType_1.default)(clientOptions), | ||
...(0, transcribe_1.default)(clientOptions), | ||
...(0, memory_1.default)(clientOptions), | ||
...(0, chats_1.default)(clientOptions), | ||
kv: (0, kv_1.default)(clientOptions), | ||
}; | ||
} | ||
exports.default = client; | ||
const chats_1 = require("./chats"); | ||
Object.defineProperty(exports, "Chat", { enumerable: true, get: function () { return chats_1.Chat; } }); |
@@ -1,9 +0,8 @@ | ||
import { ClientOptions } from "./clientOpts"; | ||
declare function createMemory(clientOptions?: Partial<ClientOptions>): Promise<{ | ||
declare function createMemory(): Promise<{ | ||
id: string; | ||
}>; | ||
declare function updateMemory(id: string, input: string, maxToken?: number, clientOptions?: Partial<ClientOptions>): Promise<{ | ||
declare function updateMemory(id: string, input: string, maxToken?: number): Promise<{ | ||
success: boolean; | ||
}>; | ||
declare function getAllMemories(clientOptions?: Partial<ClientOptions>): Promise<{ | ||
declare function getAllMemories(): Promise<{ | ||
ids: string[]; | ||
@@ -16,18 +15,5 @@ }>; | ||
memoryId: Promise<string>; | ||
clientOptions: ClientOptions; | ||
constructor(clientOptions?: Partial<ClientOptions>); | ||
constructor(); | ||
add(input: string, { maxToken }?: MemoryAddOptions): Promise<void>; | ||
} | ||
export { createMemory, updateMemory, getAllMemories, Memory }; | ||
export default function client(clientOptions?: Partial<ClientOptions>): { | ||
createMemory: () => Promise<{ | ||
id: string; | ||
}>; | ||
updateMemory: (id: string, input: string, maxToken?: number) => Promise<{ | ||
success: boolean; | ||
}>; | ||
getAllMemories: () => Promise<{ | ||
ids: string[]; | ||
}>; | ||
Memory: () => Memory; | ||
}; |
@@ -8,3 +8,4 @@ "use strict"; | ||
const axios_1 = __importDefault(require("axios")); | ||
const clientOpts_1 = require("./clientOpts"); | ||
const ensurePolyfactToken_1 = require("./helpers/ensurePolyfactToken"); | ||
const { POLYFACT_ENDPOINT = "https://api2.polyfact.com", POLYFACT_TOKEN = "" } = process.env; | ||
class MemoryError extends Error { | ||
@@ -21,9 +22,9 @@ constructor(errorType) { | ||
} | ||
async function createMemory(clientOptions = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
async function createMemory() { | ||
(0, ensurePolyfactToken_1.ensurePolyfactToken)(); | ||
try { | ||
const res = await axios_1.default.post(`${endpoint}/memory`, {}, { | ||
const res = await axios_1.default.post(`${POLYFACT_ENDPOINT}/memory`, {}, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Access-Token": token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -42,6 +43,6 @@ }); | ||
exports.createMemory = createMemory; | ||
async function updateMemory(id, input, maxToken = 0, clientOptions = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
async function updateMemory(id, input, maxToken = 0) { | ||
(0, ensurePolyfactToken_1.ensurePolyfactToken)(); | ||
try { | ||
const res = await axios_1.default.put(`${endpoint}/memory`, { | ||
const res = await axios_1.default.put(`${POLYFACT_ENDPOINT}/memory`, { | ||
id, | ||
@@ -53,3 +54,3 @@ input, | ||
"Content-Type": "application/json", | ||
"X-Access-Token": token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -67,9 +68,9 @@ }); | ||
exports.updateMemory = updateMemory; | ||
async function getAllMemories(clientOptions = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
async function getAllMemories() { | ||
(0, ensurePolyfactToken_1.ensurePolyfactToken)(); | ||
try { | ||
const res = await axios_1.default.get(`${endpoint}/memories`, { | ||
const res = await axios_1.default.get(`${POLYFACT_ENDPOINT}/memories`, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Access-Token": token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -88,20 +89,10 @@ }); | ||
class Memory { | ||
constructor(clientOptions = {}) { | ||
this.clientOptions = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
this.memoryId = createMemory(this.clientOptions).then((res) => res.id); | ||
constructor() { | ||
this.memoryId = createMemory().then((res) => res.id); | ||
} | ||
async add(input, { maxToken = 0 } = {}) { | ||
const id = await this.memoryId; | ||
await updateMemory(id, input, maxToken, this.clientOptions); | ||
await updateMemory(id, input, maxToken); | ||
} | ||
} | ||
exports.Memory = Memory; | ||
function client(clientOptions = {}) { | ||
return { | ||
createMemory: () => createMemory(clientOptions), | ||
updateMemory: (id, input, maxToken) => updateMemory(id, input, maxToken, clientOptions), | ||
getAllMemories: () => getAllMemories(clientOptions), | ||
Memory: () => new Memory(clientOptions), | ||
}; | ||
} | ||
exports.default = client; |
import * as t from "polyfact-io-ts"; | ||
import { GenerationOptions } from "../index"; | ||
import { ClientOptions } from "../clientOpts"; | ||
export declare function generateWithTypeWithTokenUsage<T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions, clientOptions?: Partial<ClientOptions>): Promise<{ | ||
export declare function generateWithTypeWithTokenUsage<T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions): Promise<{ | ||
result: t.TypeOf<t.TypeC<T>>; | ||
@@ -11,12 +10,2 @@ tokenUsage: { | ||
}>; | ||
export declare function generateWithType<T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions, clientOptions?: Partial<ClientOptions>): Promise<t.TypeOf<t.TypeC<T>>>; | ||
export default function client(clientOptions?: Partial<ClientOptions>): { | ||
generateWithTypeWithTokenUsage: <T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions) => Promise<{ | ||
result: { [K in keyof T]: t.TypeOf<T[K]>; }; | ||
tokenUsage: { | ||
input: number; | ||
output: number; | ||
}; | ||
}>; | ||
generateWithType: <T_1 extends t.Props>(task: string, type: t.TypeC<T_1>, options?: GenerationOptions) => Promise<{ [K_1 in keyof T_1]: t.TypeOf<T_1[K_1]>; }>; | ||
}; | ||
export declare function generateWithType<T extends t.Props>(task: string, type: t.TypeC<T>, options?: GenerationOptions): Promise<t.TypeOf<t.TypeC<T>>>; |
@@ -66,7 +66,7 @@ "use strict"; | ||
} | ||
async function generateWithTypeWithTokenUsage(task, type, options = {}, clientOptions = {}) { | ||
async function generateWithTypeWithTokenUsage(task, type, options = {}) { | ||
const typeFormat = tsio2String(type); | ||
const tokenUsage = { input: 0, output: 0 }; | ||
for (let tryCount = 0; tryCount < 5; tryCount++) { | ||
const { result: resultJson, tokenUsage: tu } = await (0, index_1.generateWithTokenUsage)(generateTypedPrompt(typeFormat, task), options, clientOptions); | ||
const { result: resultJson, tokenUsage: tu } = await (0, index_1.generateWithTokenUsage)(generateTypedPrompt(typeFormat, task), options); | ||
tokenUsage.output += tu.output; | ||
@@ -92,13 +92,6 @@ tokenUsage.input += tu.input; | ||
exports.generateWithTypeWithTokenUsage = generateWithTypeWithTokenUsage; | ||
async function generateWithType(task, type, options = {}, clientOptions = {}) { | ||
const res = await generateWithTypeWithTokenUsage(task, type, options, clientOptions); | ||
async function generateWithType(task, type, options = {}) { | ||
const res = await generateWithTypeWithTokenUsage(task, type, options); | ||
return res.result; | ||
} | ||
exports.generateWithType = generateWithType; | ||
function client(clientOptions = {}) { | ||
return { | ||
generateWithTypeWithTokenUsage: (task, type, options = {}) => generateWithTypeWithTokenUsage(task, type, options, clientOptions), | ||
generateWithType: (task, type, options = {}) => generateWithType(task, type, options, clientOptions), | ||
}; | ||
} | ||
exports.default = client; |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { Readable } from "stream"; | ||
import { ClientOptions } from "./clientOpts"; | ||
export declare function transcribe(file: Buffer | Readable, clientOptions?: Partial<ClientOptions>): Promise<string>; | ||
export default function client(clientOptions?: Partial<ClientOptions>): { | ||
transcribe: (file: Buffer | Readable) => Promise<string>; | ||
}; | ||
export declare function transcribe(file: Buffer | Readable): Promise<string>; |
@@ -33,8 +33,11 @@ "use strict"; | ||
const t = __importStar(require("polyfact-io-ts")); | ||
const clientOpts_1 = require("./clientOpts"); | ||
const { POLYFACT_ENDPOINT = "https://api2.polyfact.com" } = process.env; | ||
const { POLYFACT_TOKEN = "" } = process.env; | ||
const ResultType = t.type({ | ||
text: t.string, | ||
}); | ||
async function transcribe(file, clientOptions = {}) { | ||
const { token, endpoint } = (0, clientOpts_1.defaultOptions)(clientOptions); | ||
async function transcribe(file) { | ||
if (!POLYFACT_TOKEN) { | ||
throw new Error("Please put your polyfact token in the POLYFACT_TOKEN environment variable. You can get one at https://app.polyfact.com"); | ||
} | ||
const formData = new form_data_1.default(); | ||
@@ -45,5 +48,5 @@ formData.append("file", file, { | ||
}); | ||
const res = await axios_1.default.post(`${endpoint}/transcribe`, formData, { | ||
const res = await axios_1.default.post(`${POLYFACT_ENDPOINT}/transcribe`, formData, { | ||
headers: { | ||
"X-Access-Token": token, | ||
"X-Access-Token": POLYFACT_TOKEN, | ||
}, | ||
@@ -58,7 +61,1 @@ }); | ||
exports.transcribe = transcribe; | ||
function client(clientOptions = {}) { | ||
return { | ||
transcribe: (file) => transcribe(file, clientOptions), | ||
}; | ||
} | ||
exports.default = client; |
@@ -13,3 +13,6 @@ import * as readline from "node:readline/promises"; | ||
console.log(await chat.sendMessage(userInput)); | ||
const stream = chat.sendMessageStream(userInput); | ||
stream.pipe(output); | ||
await new Promise((res) => stream.on("end", res)); | ||
output.write("\n"); | ||
} | ||
@@ -16,0 +19,0 @@ } |
import axios from "axios"; | ||
import * as t from "polyfact-io-ts"; | ||
import { generateWithTokenUsage, GenerationOptions } from "../generate"; | ||
import { Readable, PassThrough } from "stream"; | ||
import { generateStream, generateWithTokenUsage, GenerationOptions } from "../generate"; | ||
import { ClientOptions, defaultOptions } from "../clientOpts"; | ||
@@ -88,2 +89,35 @@ import { Memory } from "../memory"; | ||
sendMessageStream(message: string, options: GenerationOptions = {}): Readable { | ||
const resultStream = new PassThrough(); | ||
(async () => { | ||
const chatId = await this.chatId; | ||
if (this.autoMemory && !options.memory && !options.memoryId) { | ||
options.memory = this.autoMemory; | ||
} | ||
const result = generateStream(message, { ...options, chatId }, this.clientOptions); | ||
result.pipe(resultStream); | ||
const bufs: Buffer[] = []; | ||
const totalResult = await new Promise((res, _rej) => { | ||
result.on("data", (d) => { | ||
bufs.push(d); | ||
}); | ||
result.on("end", () => { | ||
res(Buffer.concat(bufs).toString("utf8")); | ||
}); | ||
}); | ||
if (this.autoMemory) { | ||
this.autoMemory.add(`Human: ${message}`); | ||
this.autoMemory.add(`AI: ${totalResult}`); | ||
} | ||
})(); | ||
return resultStream; | ||
} | ||
async getMessages(): Promise<t.TypeOf<typeof Message>[]> { | ||
@@ -90,0 +124,0 @@ const response = await axios.get( |
@@ -14,3 +14,3 @@ export type ClientOptions = { | ||
return { | ||
endpoint: "https://api2.polyfact.com", | ||
endpoint: process?.env.POLYFACT_ENDPOINT || "https://api2.polyfact.com", | ||
token: process?.env?.POLYFACT_TOKEN || "", | ||
@@ -17,0 +17,0 @@ ...opts, |
import axios from "axios"; | ||
import * as t from "polyfact-io-ts"; | ||
import { Readable } from "stream"; | ||
import WebSocket from "isomorphic-ws"; | ||
import { ClientOptions, defaultOptions } from "./clientOpts"; | ||
@@ -96,2 +98,47 @@ import { Memory } from "./memory"; | ||
export function generateStream( | ||
task: string, | ||
options: GenerationOptions = {}, | ||
clientOptions: Partial<ClientOptions> = {}, | ||
): Readable { | ||
const resultStream = new Readable({ | ||
read() {}, | ||
}); | ||
(async () => { | ||
const requestBody: { | ||
task: string; | ||
// eslint-disable-next-line camelcase | ||
memory_id?: string; | ||
// eslint-disable-next-line camelcase | ||
chat_id?: string; | ||
provider: GenerationOptions["provider"]; | ||
stop: GenerationOptions["stop"]; | ||
} = { | ||
task, | ||
provider: options?.provider || "openai", | ||
memory_id: (await options?.memory?.memoryId) || options?.memoryId || "", | ||
chat_id: options?.chatId || "", | ||
stop: options?.stop || [], | ||
}; | ||
const { token, endpoint } = defaultOptions(clientOptions); | ||
const ws = new WebSocket(`${endpoint.replace("http", "ws")}/stream`, { | ||
headers: { | ||
"X-Access-Token": token, | ||
}, | ||
}); | ||
ws.onopen = () => ws.send(JSON.stringify(requestBody)); | ||
ws.onmessage = (data: any) => { | ||
if (data.data === "") { | ||
resultStream.push(null); | ||
} else { | ||
resultStream.push(data.data); | ||
} | ||
}; | ||
})(); | ||
return resultStream; | ||
} | ||
export default function client(clientOptions: Partial<ClientOptions> = {}) { | ||
@@ -103,3 +150,5 @@ return { | ||
generate(task, options, clientOptions), | ||
generateStream: (task: string, options: GenerationOptions = {}) => | ||
generateStream(task, options, clientOptions), | ||
}; | ||
} |
{ | ||
"name": "polyfact", | ||
"version": "0.1.24", | ||
"version": "0.2.0", | ||
"main": "dist/index.js", | ||
@@ -14,5 +14,7 @@ "types": "dist/index.d.ts", | ||
"isomorphic-fetch": "^3.0.0", | ||
"isomorphic-ws": "^5.0.0", | ||
"js-tiktoken": "^1.0.7", | ||
"polyfact-io-ts": "^2.2.20", | ||
"ts-node": "^10.9.1" | ||
"ts-node": "^10.9.1", | ||
"ws": "^8.13.0" | ||
}, | ||
@@ -19,0 +21,0 @@ "devDependencies": { |
@@ -11,3 +11,3 @@ <h1 align="center">🏭 PolyFact</h1> | ||
PolyFact's goal is to make it possible to code every AI tool/Chatbot you could want in only a couple of lines of code without the need for complex abstractions and to have to deploy anything. | ||
PolyFact's goal is to make it possible to code every AI tool/Chatbot you could want in only a couple of lines of code without the need for complex abstractions and having to deploy anything. | ||
@@ -20,3 +20,3 @@ <p align="center"><img src="demo.gif" /></p> | ||
- **[Transcribe](https://github.com/polyfact/polyfact-node/wiki/transcribe)**: Transcribe audio files to text | ||
- **[Memory](https://github.com/polyfact/polyfact-node/wiki/memory)**: Easily create a long term memory and simplify use of large amount of information | ||
- **[Memory](https://github.com/polyfact/polyfact-node/wiki/memory)**: Easily create a long-term memory and simplify the use of large amounts of information | ||
- **[Type checked generation](https://github.com/polyfact/polyfact-node/wiki/generateWithType)**: Answer simple requests with a type you defined *(🎲 probabilistic function)* | ||
@@ -36,3 +36,3 @@ | ||
Get your your polyfact token by signing up with github here: https://app.polyfact.com<br/> | ||
Get your polyfact token by signing up with GitHub here: https://app.polyfact.com<br/> | ||
Add your PolyFact Token in the `POLYFACT_TOKEN` environment variable: | ||
@@ -46,3 +46,3 @@ | ||
There's more examples and tutorials in the [Documentation](https://github.com/polyfact/polyfact-node/wiki) but here's a simple chatbot to get you started: | ||
There are more examples and tutorials in the [Documentation](https://github.com/polyfact/polyfact-node/wiki) but here's a simple chatbot to get you started: | ||
@@ -71,6 +71,6 @@ ```js | ||
We are striving for feedbacks and want to understand everyone's needs, you can hang out with us on [Discord](https://discord.gg/8mkBfDXNTM) ! | ||
We strive for feedback and want to understand everyone's needs, and you can hang out with us on [Discord](https://discord.gg/8mkBfDXNTM)! | ||
## 🧑💻 Contributing | ||
PolyFact is opensource ! You can contribute to this package or to the [API](https://github.com/polyfact/polyfact-api-go) by opening an issue or a PR ! | ||
PolyFact is open-source! You can contribute to this package or the [API](https://github.com/polyfact/polyfact-api-go) by opening an issue or a PR! |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
932856
9
53
2056
6
+ Addedisomorphic-ws@^5.0.0
+ Addedws@^8.13.0
+ Addedisomorphic-ws@5.0.0(transitive)
+ Addedws@8.18.1(transitive)