From 0f881c4640059b6237006107b9b0b97804e988da Mon Sep 17 00:00:00 2001 From: Javier Bullrich Date: Sat, 4 May 2024 16:53:51 +0200 Subject: [PATCH] Updated documentation (#5) - Removed unused interface - Updated documentation for most classes - Removed `provenance` from `npm publish` command - added badge to readme --- .github/workflows/npm-publish.yml | 2 +- README.md | 4 ++++ src/chat.ts | 16 ++++++++++++++++ src/image.ts | 8 +++++--- src/story.ts | 4 ++++ src/types.ts | 12 +++--------- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index b6a2cea..ef4f838 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -25,6 +25,6 @@ jobs: - name: Set version run: npm version --no-git-tag-version ${{github.event.release.tag_name}} - run: npm run build - - run: npm publish --provenance --access public + - run: npm publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/README.md b/README.md index e769ca8..0ce2960 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ Typescript library used to generate the stories for [StoryBot](https://storybot. ## Installation +[![NPM Release](https://github.com/CodingBull-dev/story-gpt/actions/workflows/npm-publish.yml/badge.svg?branch=main)](https://github.com/CodingBull-dev/story-gpt/actions/workflows/npm-publish.yml) + +![NPM Version](https://img.shields.io/npm/v/story-gpt) + `npm install --save story-gpt` ## Usage diff --git a/src/chat.ts b/src/chat.ts index e5dedad..ead1dc6 100644 --- a/src/chat.ts +++ b/src/chat.ts @@ -7,10 +7,26 @@ type Conversation = { chat(message: OpenAI.Chat.Completions.ChatCompletionMessageParam): Promise; } +/** + * Assistant class that allows to have conversations while keeping the history + */ export class ChatAssistant { public constructor(private readonly openai: OpenAI, public readonly temperature: number, public readonly chatModel: string = "gpt-4-turbo") { } + /** + * Allows to start a conversation and returns an answer and an function with the history + * @param messages thread messages, usually the system one and a first message + * @returns an object with the response and the function `chat` + * which allows to continue the conversation using the previous history + * + * @example + * ```ts + * const conv = await chat.chat({role: "user", content: "Where is Argentina?"}); + * console.log("Answer is", conv.answer.content); + * const followUp = conv.chat({role: "user", content: "And how big is it?"}); + * console.log("Argentina size is:", followUp.answer.content); + */ public async chat(...messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[]): Promise { const response = await this.openai.chat.completions.create({ model: this.chatModel, diff --git a/src/image.ts b/src/image.ts index 1a8f97e..0fa8c7a 100644 --- a/src/image.ts +++ b/src/image.ts @@ -4,14 +4,16 @@ import type { ILogger } from "./types"; export type ImageSize = '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792' export type Model = 'dall-e-2' | 'dall-e-3'; +/** + * Class used to generate images + */ export class ImageGenerator { public constructor(private readonly openai: OpenAI, private readonly logger: ILogger) { - } - public async generateImage(prompt: string, size: ImageSize = "512x512", model: Model = "dall-e-3") { + public async generateImage(prompt: string, size: ImageSize = "512x512", model: Model = "dall-e-3"): Promise { const image = await this.generateImages(prompt, 1, size, model); - return image[0]; + return image[0]!; } public async generateImages(prompt: string, numberOfImages: 1 | 2 | 3 | 4 | 5, size: ImageSize = "512x512", model: Model = "dall-e-3"): Promise { diff --git a/src/story.ts b/src/story.ts index aa293c5..eaa73f2 100644 --- a/src/story.ts +++ b/src/story.ts @@ -11,6 +11,9 @@ export const systemInfo = `You are Story Bot, a language model that helps users When writing a post, story or script, try to extend the text as much as possible without making it boring. Do NOT include the title in the post unless you are asked for it.`; +/** + * Story object which contains a prompt and story and can generate a title and an image + */ export class Story { private readonly creationPrompt: OpenAI.Chat.Completions.ChatCompletionMessageParam[]; private readonly chat: ChatAssistant; @@ -26,6 +29,7 @@ export class Story { this.chat = new ChatAssistant(this.openai, storyParams.temperature); } + /** Utility method which allows a Story object to be generated from a prompt with a story */ static async generateStory(prompt: string, openai: OpenAI, logger: ILogger = console): Promise { const chat = new ChatAssistant(openai, Math.round(Math.random() * 100) / 100); logger.log("Generating story for prompt", prompt); diff --git a/src/types.ts b/src/types.ts index 4497785..b393b90 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,14 +1,8 @@ +/** + * Tool used for logging. The default `console` works as an implementation + */ export interface ILogger { log(...message: unknown[]): void; error(...args: unknown[]): void; warn(...args: unknown[]): void; } - -export interface Post { - prompt: string; - title: string; - content: string; - temperature: number; - imagePrompt: string; - image: string; -}