Skip to content

Commit

Permalink
Merge pull request #1 from PepijnMC/v1.1.x
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
PepijnMC authored Jan 13, 2023
2 parents c628e5d + 2ca6a70 commit 1c78e22
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 7 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
GPT-3 is a text-based AI by [OpenAI](https://beta.openai.com/). This module provides a user-friendly implentation to communicate with its API from within your Foundry games to generate descriptions on the fly.

## Chat Commands
There are two new commands available to the GM to send prompts to GPT-3.

```
/gpt construct (subject) [subjectType]
```
- Constructs and sends a prompt similar to the Description Generator.
- The prompt is based on your settings, the provided `subject`, and optionally a `subjectType` (either `creature`, `item`, or `spell`).
- Examples:
- `/gpt construct Ancient White Dragon creature`
- `/gpt construct Beholder`
- `/gpt construct Fireball spell`

```
/gpt send (prompt)
```
- Sends a custom `prompt`.
- Examples:
- `/gpt send What does the Magic Missile spell do in dnd5e?`
- `/gpt send What would be a fun low level encounter for a dnd5e game set in a jungle?`

## Description Generator
All actors, items, and spells have a button added in their header for the GM to request a short description generated by the AI based on your rpg system and world/setting. Responses are put in the Foundry chat and can optionally only be whispered to you.

Expand Down
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "ai-description-generator",
"title": "AI Description Generator",
"description": "Utilizes the GPT-3 AI to generate descriptions from within Foundry.",
"version": "1.0.1",
"version": "1.1.0",
"authors": [
{
"name": "Pepijn Sietsema",
Expand All @@ -20,4 +20,4 @@
"url": "https://github.com/PepijnMC/FoundryVTT-AI-Description-Generator",
"manifest": "https://github.com/PepijnMC/FoundryVTT-AI-Description-Generator/releases/latest/download/module.json",
"download": "https://github.com/PepijnMC/FoundryVTT-AI-Description-Generator/releases/latest/download/module.zip"
}
}
Binary file added module.zip
Binary file not shown.
45 changes: 45 additions & 0 deletions scripts/chat_commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { constructPrompt, sendPrompt } from "./generator.js";


export function addChatCommands(log, data, chatData) {
const user = game.users.find(user => user.id === chatData.user);
if (!user.isGM) return
if (data.length < 4 || data.split(' ')[0].toLowerCase() != '/gpt') return

const subCommand = data.split(' ')[1]
if (!subCommand) return

if (subCommand == 'construct') {
const args = data.split(' ').slice(2, this.length);
if (args.length === 0) return;
const lastArg = args[args.length - 1].toLowerCase();
var subject = args.join(' ');
var subjectType = 'none';
switch (lastArg) {
case 'creature':
case 'item':
case 'spell':
subject = args.slice(0, args.length - 1).join(' ');
subjectType = lastArg;
break;
}
constructPrompt(
game.settings.get('ai-description-generator', 'system'),
game.settings.get('ai-description-generator', 'world'),
subjectType,
subject,
game.settings.get('ai-description-generator', 'key')
);
return false;
}
if (subCommand == 'send') {
const prompt = data.split(' ').slice(2, this.length).join(' ');
if (prompt === '') return
sendPrompt(
prompt,
game.settings.get('ai-description-generator', 'key')
)
return false;
}
return
}
9 changes: 5 additions & 4 deletions scripts/generator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export function constructPrompt(system, world, entityType, subject, key) {
export function constructPrompt(system, world, subjectType, subject, key) {
var prompt = `This is a tabletop roleplaying game using the ${system}`;
if (!system.toLowerCase().includes('system')) prompt += ' system';
if (world) prompt += ` and the ${world} setting`;
prompt += `. Give a cool short sensory description the game master can use for a ${subject}`;
switch (entityType.toLowerCase()) {
switch (subjectType.toLowerCase()) {
case 'creature':
case 'item':
case 'spell':
prompt += ` ${entityType}`;
prompt += ` ${subjectType}`;
}
prompt += '.';
console.log(`AI Description Generator | Sending the following prompt to GPT-3: ${prompt}`);
Expand Down Expand Up @@ -41,7 +41,8 @@ export function sendPrompt(prompt, key) {
if (s == "") s = "No response";
response += s;
}
const message = {user: game.user, speaker: {alias: 'GPT-3'}, content: response}
const speaker = game.settings.get('ai-description-generator', 'ai_name')
const message = {user: game.user, speaker: {alias: speaker}, content: response}
if (game.settings.get('ai-description-generator', 'whisper')) message['whisper'] = [game.userId]
ChatMessage.create(message);
}
Expand Down
5 changes: 4 additions & 1 deletion scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {registerSettings } from './settings.js';
import { registerAPI } from './api.js';
import { constructPrompt } from './generator.js';
import { addChatCommands } from './chat_commands.js';

Hooks.once('init', () => {
registerSettings();
Expand Down Expand Up @@ -28,4 +29,6 @@ Hooks.on('getItemSheetHeaderButtons', (sheet, headerButtons) => {
constructPrompt(game.settings.get('ai-description-generator', 'system'), game.settings.get('ai-description-generator', 'world'), sheet.object.type == 'spell' ? 'spell': 'item', sheet.object.name, game.settings.get('ai-description-generator', 'key'))
}
})
})
})

Hooks.on('chatMessage', addChatCommands);
9 changes: 9 additions & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ export function registerSettings() {
default: false
});

game.settings.register('ai-description-generator', 'ai_name', {
name: 'AI Name',
hint: 'The name of the AI to be used for its chat messages.',
scope: 'world',
config: true,
type: String,
default: 'GPT-3'
});

game.settings.register('ai-description-generator', 'max_tokens', {
name: 'AI Max Tokens',
hint: 'The maximum amount of tokens the AI can use per request.',
Expand Down

0 comments on commit 1c78e22

Please sign in to comment.