Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI-generated written text content #800

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
67 changes: 67 additions & 0 deletions docs/gpt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
gpt
=======
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=======
===


.. dfhack-tool::
:summary: AI-generated written content!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can understand the exclamation point, but summaries have to end in a period to match the parser : P

How about "Generate written content with AI."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will fix thanks.

:tags: fort gameplay

Enables a UI for submitting knowledge item descriptions to OpenAI for generating
poetry, star charts, and excerpts from longer works such as biographies, dictionaries,
treatises on technological evolution, comparative biographies, cultural histories,
autobiographies, cultural comparisons, essays, guides, manuals, and more.
gistya marked this conversation as resolved.
Show resolved Hide resolved

``enable gpt``
=======
Enables the plugin. The overlay will be shown when a knowledge item or unit view sheet is open.

``disable gpt``
=======
Disables the plugin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this actually isn't necessary. players can turn the overlay on and off in gui/control-panel.


Setup:
gistya marked this conversation as resolved.
Show resolved Hide resolved

1. Register for an OpenAI API account. It must be a paid or active trial account.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a url would be useful here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

2. Generate an API token for your account.
3. Save your OpenAI API token to a file at the root of your DF directory, `oaak.txt`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we try not to put any user data in the DF root directory. I suggest putting it in dfhack-config/.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, makes sense

4. Install python. We used version 3.11 installed from the Microsoft Store.
5. Install python dependencies Flask and OpenAI: `pip install Flask` and `pip install OpenAI`.
6. Start the local helper python app: cd into dfhack/scripts directory & run `python gptserver.py`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two options here. if we deploy this with DFHack, then this python script should probably go in the DFHack/dfhack repo and get installed somewhere more appropriate (like maybe the DF root dir)

if this script gets distributed as a mod (see https://docs.dfhack.org/en/stable/docs/guides/modding-guide.html#the-structure-of-a-mod), then the python code can get included in the mod files


Once the python helper is running, you may now enable and use the gpt plugin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest having the overlay display a message like "cannot contact server -- do you have the Python helper running?" instead of requiring the player to remember to start the server first

Copy link
Author

@gistya gistya Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overlay doesn't try to contact the server. It waits until you press the button and then, when there is something to submit, then it tries submitting. At that point, if it can't reach the server, the UI displays a message that says what you suggest (more or less).

The UI becomes fairly unresponsive though, since if the server can't be reached, the exiting luasocket library throws a C++ exception, and Lua's lack of modern error handling means we have to resort to pcall, which seems to carry a nasty performance penalty, perhaps exacerbated by how often updateLayout is being called. Still looking into this one, but it seems to have a workaround at least, and isn't expected to happen very often.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pcall shouldn't really be introducing much of a penalty, although errors themselves might... how often are you calling it?


The python script defaults to using the fast, cheap, legacy model `text-davinci-003`.
If you wish to use the slower, more expensive `gpt-3.5-turbo` or `gpt-4` models, you
can start the script with `python gptserver.py -gpt3` or `python gptserver.py -gpt4`.
Tweaking additional OpenAI API parameters will require modifying `gptserver.py` to suit
your particular desires, until such time as someone may have added additional
configuration options in a future update to DFHack :D
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be fairly trivial to have a configuration screen that can open from the overlay and send options data to the python server


Note: EVERY TEXT YOU GENERATE COSTS $$ if you are on a paid account. The fee is appx. $0.005 USD
at the time of this writing. YMMV!

Versions of python dependencies tested with:

Package Version
------------------ ---------
aiohttp 3.8.5
aiosignal 1.3.1
async-timeout 4.0.2
attrs 23.1.0
blinker 1.6.2
certifi 2023.7.22
charset-normalizer 3.2.0
click 8.1.6
colorama 0.4.6
Flask 2.3.2
frozenlist 1.4.0
idna 3.4
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.3
multidict 6.0.4
openai 0.27.8
requests 2.31.0
tqdm 4.65.0
urllib3 2.0.4
Werkzeug 2.3.6
yarl 1.9.2
Loading