-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added script to verify that prompts are valid - Created a basic readme - Added typedoc library and npm script - Added GitHub action to automatically deploy docs
- Loading branch information
Showing
8 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Deploy docs | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
pages: write | ||
id-token: write | ||
|
||
jobs: | ||
deploy-docs: | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: github-pages | ||
url: ${{ steps.deployment.outputs.page_url }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- name: Install node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
- name: Get npm cache directory | ||
id: npm-cache-dir | ||
shell: bash | ||
run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} | ||
- name: Cache node modules | ||
uses: actions/cache@v4 | ||
id: npm-cache | ||
with: | ||
path: ${{ steps.npm-cache-dir.outputs.dir }} | ||
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | ||
restore-keys: | | ||
${{ runner.os }}-node- | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Generate docs | ||
run: npm run docs | ||
- name: Setup Pages | ||
uses: actions/configure-pages@v5 | ||
- name: Upload artifact | ||
uses: actions/upload-pages-artifact@v3 | ||
with: | ||
path: ./docs | ||
- name: Deploy to GitHub Pages | ||
id: deployment | ||
uses: actions/deploy-pages@v4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Story-GPT | ||
|
||
Typescript library used to generate the stories for [StoryBot](https://storybot.dev) | ||
|
||
## Installation | ||
|
||
`npm install --save story-gpt` | ||
|
||
## Usage | ||
|
||
```typescript | ||
import { createStory } from "story-gpt"; | ||
|
||
const story = await createStory("A story about a happy horse", new OpenAI({apiKey: ">my api key<"})); | ||
|
||
console.log("The story is named %s and it's tells the following story:", story.title, story.content); | ||
console.log("See the cover picture for the story here:", story.image); | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { OpenAI } from "openai"; | ||
|
||
type StoryResult = { validStory: true } | { validStory: false, reasonForRejection: string } | ||
|
||
/** | ||
* Utility method that verifies if a prompt qualifies as a story. | ||
* Useful to be used as the filter before processing a story and receiving an answer like | ||
* "I'm sorry, but X is not a prompt for a story" | ||
* @param prompt The prompt to analyze | ||
* @param openai The openai authenticated client | ||
* @param chatModel optional, the model to use. Defaults to gpt-4-turbo | ||
* @returns a `{validStory:boolean,reasonForRejection?:string}` object. | ||
* If validStory is false, the reasonForRejection will contain the information | ||
*/ | ||
export async function verifyPrompt(prompt: string, openai: OpenAI, chatModel: string = "gpt-4-turbo"): Promise<StoryResult> { | ||
const response = await openai.chat.completions.create({ | ||
model: chatModel, | ||
messages: [{ | ||
role: "system", content: "You verify if a prompt is a set of instructions to a story or blogpost or if it is an unrelated command" | ||
}, | ||
{ role: "user", content: `Is the following prompt a prompt for a story?\n\n${prompt}` }, | ||
], | ||
tools: [{ | ||
type: "function", | ||
function: { | ||
name: "is_story_prompt", | ||
description: "Informs if a prompt is a story prompt", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
isStory: { | ||
type: "boolean", | ||
description: "True if the prompt is an instruction for a story or a blog. False if the prompt is unrelated" | ||
}, | ||
kindOfPrompt: { | ||
type: "string", | ||
description: "Explain what is missing to be a story prompt. Around 140 characters" | ||
} | ||
}, | ||
required: ["isStory", "kindOfPrompt"], | ||
} | ||
} | ||
}], | ||
tool_choice: "auto" | ||
}); | ||
|
||
const responseMessage = response.choices[0]?.message; | ||
if (!responseMessage?.tool_calls || !responseMessage.tool_calls[0]?.function.arguments) { | ||
throw new Error("Missing tool calls"); | ||
} | ||
|
||
const { isStory, kindOfPrompt } = JSON.parse(responseMessage.tool_calls[0].function.arguments) as { isStory: boolean, kindOfPrompt: string }; | ||
|
||
return isStory ? { validStory: true } : { validStory: false, reasonForRejection: kindOfPrompt }; | ||
} |