Skip to content

Commit

Permalink
Add support for systemPromptId.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-btc committed Aug 16, 2023
1 parent 17c6a0e commit 233ab94
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 353 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

"prettier"
],
"ignorePatterns": ["examples/**/*"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "tsconfig.json"
},
"globals": {
"fetch": true
"fetch": true
},
"rules": {
"@typescript-eslint/explicit-module-boundary-types": ["warn"],
Expand Down
3 changes: 1 addition & 2 deletions examples/memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { generateWithInfo } from "../lib/generate";
import { createMemory, updateMemory, getAllMemories, generate } from "../lib/index";

(async () => {
Expand All @@ -11,6 +10,6 @@ import { createMemory, updateMemory, getAllMemories, generate } from "../lib/ind
const memories = await getAllMemories();
console.log("All memories:", memories);

const result2 = await generateWithInfo("<input-data>", { memoryId: memory.id, infos: true });
const result2 = await generate("<input-data>", { memoryId: memory.id, infos: true });
console.log("Generated:", result2);
})();
11 changes: 7 additions & 4 deletions examples/repl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";
import Polyfact from "../lib/index";

import { Chat } from "../lib/index";
async function main() {
const { Chat } = Polyfact.exec();
const chat = new Chat({ autoMemory: true, provider: "openai", model: "gpt-3.5-turbo" });
const chat = new Chat({
autoMemory: true,
provider: "openai",
model: "gpt-3.5-turbo-16k",
systemPromptId: "8fc39ca4-3941-40d9-824a-5ed283102f6e", // Holy Bible Prompt
});

const rl = readline.createInterface({ input, output });

Expand Down
54 changes: 36 additions & 18 deletions lib/chats/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import axios, { AxiosError } from "axios";
import * as t from "polyfact-io-ts";
import { Readable, PassThrough } from "readable-stream";
import { UUID } from "crypto";
import {
Exclusive,
generateStream,
generateStreamWithInfos,
generateWithTokenUsage,
GenerationOptions,
GenerationResult,
Expand All @@ -21,20 +22,22 @@ const Message = t.type({
});
export async function createChat(
systemPrompt?: string,
systemPromptId?: UUID,
options: InputClientOptions = {},
): Promise<string> {
try {
const { token, endpoint } = await defaultOptions(options);

const response = await axios.post(
`${endpoint}/chats`,
{ system_prompt: systemPrompt },
{
headers: {
"X-Access-Token": token,
},
const body = {
...(systemPromptId ? { system_prompt_id: systemPromptId } : {}),
...(systemPrompt && !systemPromptId ? { system_prompt: systemPrompt } : {}),
};

const response = await axios.post(`${endpoint}/chats`, body, {
headers: {
"X-Access-Token": token,
},
);
});

return response?.data?.id;
} catch (e: unknown) {
Expand All @@ -48,9 +51,8 @@ export async function createChat(
type ChatOptions = {
provider?: "openai" | "cohere" | "llama";
model?: string;
systemPrompt?: string;
autoMemory?: boolean;
};
} & Exclusive<{ systemPrompt?: string }, { systemPromptId?: UUID }>;

export class Chat {
chatId: Promise<string>;
Expand All @@ -63,9 +65,12 @@ export class Chat {

autoMemory?: Promise<Memory>;

systemPromptId?: UUID;

constructor(options: ChatOptions = {}, clientOptions: InputClientOptions = {}) {
this.systemPromptId = options.systemPromptId;
this.clientOptions = defaultOptions(clientOptions);
this.chatId = createChat(options.systemPrompt, this.clientOptions);
this.chatId = createChat(options.systemPrompt, options.systemPromptId, this.clientOptions);
this.provider = options.provider || "openai";
this.model = options.model;
if (options.autoMemory) {
Expand All @@ -85,7 +90,13 @@ export class Chat {

const result = await generateWithTokenUsage(
message,
{ provider: this.provider, model: this.model, ...options, chatId },
{
provider: this.provider,
model: this.model,
...options,
chatId,
systemPromptId: this.systemPromptId,
},
this.clientOptions,
);

Expand Down Expand Up @@ -117,9 +128,9 @@ export class Chat {
options.memory = await this.autoMemory;
}

const result = generateStreamWithInfos(
const result = generateStream(
message,
{ ...options, chatId },
{ ...options, chatId, infos: true, systemPromptId: this.systemPromptId },
await this.clientOptions,
);

Expand Down Expand Up @@ -158,7 +169,13 @@ export class Chat {

const result = generateStream(
message,
{ provider: this.provider, model: this.model, ...options, chatId },
{
provider: this.provider,
model: this.model,
...options,
chatId,
systemPromptId: this.systemPromptId,
},
await this.clientOptions,
);

Expand Down Expand Up @@ -207,13 +224,14 @@ export class Chat {
}

export type ChatClient = {
createChat: (systemPrompt?: string) => Promise<string>;
createChat: (systemPrompt?: string, systemPromptId?: UUID) => Promise<string>;
Chat: typeof Chat;
};

export default function client(clientOptions: InputClientOptions = {}): ChatClient {
return {
createChat: (systemPrompt?: string) => createChat(systemPrompt, clientOptions),
createChat: (systemPrompt?: string, systemPromptId?: UUID) =>
createChat(systemPrompt, systemPromptId, clientOptions),
Chat: class C extends Chat {
constructor(options: ChatOptions = {}) {
super(options, clientOptions);
Expand Down
2 changes: 1 addition & 1 deletion lib/clientOpts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createClient, SupabaseClient } from "@supabase/supabase-js";
import { POLYFACT_ENDPOINT, POLYFACT_TOKEN } from "./utils";

export type ClientOptions = {
Expand All @@ -19,6 +18,7 @@ export async function defaultOptions(popts: InputClientOptions): Promise<ClientO
return {
endpoint: POLYFACT_ENDPOINT || "https://api2.polyfact.com",
token: POLYFACT_TOKEN || "",

...opts,
};
}
Loading

0 comments on commit 233ab94

Please sign in to comment.