Skip to content

Commit

Permalink
Merge pull request #27 from polyfact/feat/prompt
Browse files Browse the repository at this point in the history
✨ Add prompt management
  • Loading branch information
kevin-btc authored Aug 11, 2023
2 parents c4beb39 + 83d08f3 commit f434571
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 36 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
11 changes: 11 additions & 0 deletions examples/calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { generate, getAllPrompts } from "../lib/index";

const systemPrompt = `Add all numbers together which are given in an array. your response have to be like : "4" or "2"`;

(async () => {
const res = await generate("[7, 8, 9]", {
systemPrompt,
infos: true,
});
console.log(res);
})();
4 changes: 2 additions & 2 deletions examples/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const functionDescriptionType = t.type({
(async () => {
await kv.set("abc", "");
console.log(await kv.get("abc"));
const { returnType } = await generateWithType(
const res = await generateWithType(
"function add(a, b, c) { return a + b + c }",
functionDescriptionType,
{},
);
console.log(returnType);
console.log(res);
})();
44 changes: 44 additions & 0 deletions examples/managePrompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import client, { PromptInsert, Filter } from "../lib/prompt";

async function main() {
const apiClient = client({
token: "YOUR_API_TOKEN",
endpoint: "https://localhost:8080",
});

// Create a new prompt
const newPrompt: PromptInsert = {
name: "Test Prompt",
description: "This is a test prompt description.",
prompt: "What is the meaning of life?",
tags: ["philosophy", "life"],
};
const createdPrompt = await apiClient.createPrompt(newPrompt);
console.log("Created Prompt:", createdPrompt);

// Get all prompts
const allPrompts = await apiClient.getAllPrompts();
console.log("All Prompts:", allPrompts);

// Get all prompts filtered by name
const filterByName: Filter = {
column: "name",
operation: "eq",
value: "Test Prompt",
};
const filteredPrompts = await apiClient.getAllPrompts([filterByName]);
console.log("Filtered Prompts:", filteredPrompts);

// Update a prompt
const updatedData = { description: "Updated description" };
const updatedPrompt = await apiClient.updatePrompt(createdPrompt.id, updatedData);
console.log("Updated Prompt:", updatedPrompt);

// Delete a prompt
await apiClient.deletePrompt(createdPrompt.id);
console.log("Prompt Deleted!");
}

main().catch((error) => {
console.error("An error occurred:", error);
});
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);
})();
16 changes: 16 additions & 0 deletions examples/midjourney.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generate } from "../lib/index";

(async () => {
const res = await generate(
"i want a Swiss white beger dog",
{
promptId: "f4a1f732-9c38-4bdb-b9e4-3baa7971286a",
infos: true,
},
{
endpoint: "http://localhost:8080",
token: "<token>",
},
);
console.log(res);
})();
52 changes: 32 additions & 20 deletions lib/generate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import axios, { AxiosError } from "axios";
import * as t from "polyfact-io-ts";
import { Readable } from "readable-stream";
Expand Down Expand Up @@ -28,7 +29,14 @@ export type GenerationOptions = {
stop?: string[];
};

export type GenerationOptionsWithInfos = GenerationOptions & { infos?: boolean };
type Exclusive<T, U = T> =
| (T & Partial<Record<Exclude<keyof U, keyof T>, never>>)
| (U & Partial<Record<Exclude<keyof T, keyof U>, never>>);

export type GenerationGlobalOptions = GenerationOptions &
Exclusive<{ promptId?: string }, { systemPrompt?: string }> & {
infos?: boolean;
};

export type TokenUsage = {
input: number;
Expand All @@ -52,26 +60,28 @@ export type GenerationResult = {

export async function generateWithTokenUsage(
task: string,
options: GenerationOptionsWithInfos = {},
options: GenerationGlobalOptions = {},
clientOptions: InputClientOptions = {},
): Promise<GenerationResult> {
const { token, endpoint } = await defaultOptions(clientOptions);
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"];
infos: boolean;
system_prompt?: string;
prompt_id?: string;
} = {
task,
provider: options?.provider || "openai",
memory_id: (await options?.memory?.memoryId) || options?.memoryId || "",
chat_id: options?.chatId || "",
stop: options?.stop || [],
infos: options?.infos || false,
system_prompt: options?.systemPrompt,
prompt_id: options?.promptId,
};

try {
Expand Down Expand Up @@ -102,30 +112,34 @@ export async function generateWithTokenUsage(
}
}

/**
* Generates a result based on provided options.
*
* If `options.infos` is set to `true`, this function will return a `GenerationResult`.
* If `options.infos` is set to `false` or left `undefined`, this function will return a `string`.
*
* @param task - The task string.
* @param options - The generation options.
* @param clientOptions - The client options.
* @returns Either a `string` or a `GenerationResult` based on `options.infos`.
*/
export async function generate(
task: string,
options: GenerationOptions = {},
options: GenerationGlobalOptions = {},
clientOptions: InputClientOptions = {},
): Promise<string> {
): Promise<string | GenerationResult> {
const res = await generateWithTokenUsage(task, options, clientOptions);

return res.result;
}

export async function generateWithInfo(
task: string,
options: GenerationOptionsWithInfos = {},
clientOptions: InputClientOptions = {},
): Promise<GenerationResult> {
options.infos = true;
const res = await generateWithTokenUsage(task, options, clientOptions);
if (options?.infos) {
return res;
}

return res;
return res.result;
}

function stream(
task: string,
options: GenerationOptionsWithInfos = {},
options: GenerationGlobalOptions = {},
clientOptions: InputClientOptions = {},
onMessage: (data: any, resultStream: Readable) => void,
): Readable {
Expand Down Expand Up @@ -206,8 +220,6 @@ export default function client(clientOptions: InputClientOptions = {}) {
generateWithTokenUsage(task, options, clientOptions),
generate: (task: string, options: GenerationOptions = {}) =>
generate(task, options, clientOptions),
generateWithInfo: (task: string, options: GenerationOptions = {}) =>
generateWithInfo(task, options, clientOptions),
generateStream: (task: string, options: GenerationOptions = {}) =>
generateStream(task, options, clientOptions),
generateStreamWithInfos: (task: string, options: GenerationOptions = {}) =>
Expand Down
6 changes: 3 additions & 3 deletions lib/helpers/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class ApiError extends Error {
errorMessage?: string;

constructor(errorData: ErrorData) {
super(errorData.message);
super(errorData?.message);

this.errorType = errorData.code || "unknown_error";
this.errorMessage = errorData.message || "An unknown error occured";
this.errorType = errorData?.code || "unknown_error";
this.errorMessage = errorData?.message || "An unknown error occured";
}
}
19 changes: 17 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as t from "polyfact-io-ts";
import { Mutex } from "async-mutex";
import { POLYFACT_TOKEN, POLYFACT_ENDPOINT } from "./utils";
import generateClient, {
generateWithInfo,
generateStreamWithInfos,
generate,
generateWithTokenUsage,
Expand All @@ -21,6 +20,17 @@ import { splitString, tokenCount } from "./split";
import { InputClientOptions } from "./clientOpts";
import kvClient, { get as KVGet, set as KVSet } from "./kv";

import {
getAllPrompts,
getPromptById,
getPromptByName,
createPrompt,
updatePrompt,
deletePrompt,
} from "./prompt";

export type { PromptInsert, PromptUpdate, Prompt, FilterOperation, Filter } from "./prompt";

// Export types and models
export type { TokenUsage, Ressource, GenerationResult } from "./generate";
export * from "./helpers/models";
Expand All @@ -37,7 +47,6 @@ export {
generateWithTokenUsage,
generateWithType,
generateWithTypeWithTokenUsage,
generateWithInfo,
generateStreamWithInfos,
splitString,
tokenCount,
Expand All @@ -50,6 +59,12 @@ export {
Chat,
Memory,
kv,
getAllPrompts,
getPromptById,
getPromptByName,
createPrompt,
updatePrompt,
deletePrompt,
};

function client(co: InputClientOptions) {
Expand Down
Loading

0 comments on commit f434571

Please sign in to comment.