Skip to content

Commit

Permalink
Updated documentation (#5)
Browse files Browse the repository at this point in the history
- Removed unused interface
- Updated documentation for most classes
- Removed `provenance` from `npm publish` command
- added badge to readme
  • Loading branch information
Bullrich authored May 4, 2024
1 parent edd794c commit 0f881c4
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ type Conversation = {
chat(message: OpenAI.Chat.Completions.ChatCompletionMessageParam): Promise<Conversation>;
}

/**
* 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<Conversation> {
const response = await this.openai.chat.completions.create({
model: this.chatModel,
Expand Down
8 changes: 5 additions & 3 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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<string[]> {
Expand Down
4 changes: 4 additions & 0 deletions src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Story> {
const chat = new ChatAssistant(openai, Math.round(Math.random() * 100) / 100);
logger.log("Generating story for prompt", prompt);
Expand Down
12 changes: 3 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 0f881c4

Please sign in to comment.