1

Trying to plug openai's API to Ren'Py (to make characters answer to the player)

I installed the openai python module in Ren'Py using this pip command :

pip install --target /Users/...../GameName/game/python-packages netrc

And then inside the game I use this to import the module (as expalined in the Ren'Py documentation here : https://www.renpy.org/doc/html/python.html

init python:
    import openai

But I get this error :

File "python-packages/aiohttp/helpers.py", line 9, in ModuleNotFoundError: No module named 'netrc'

I guess it means that Ren'Py runs a custom python environment without netrc, but I don't know how to install netrc as a module in Ren'Py

Any help would be greatly appreciated, I'll gladly open source this GPT-3 powered Ren'Py project once it starts working.

Taiko
  • 1,351
  • 1
  • 19
  • 35
  • To install openai, shouldn't your pip command be `pip install --target /Users/...../GameName/game/python-packages openai`? I'm confused about the `No module named `netrc` error, because I thought that netrc is part of the Python standard library. But I haven't used Ren'Py. My only suggestion here is to upgrade to a newer version of Python if you can? Maybe an older version doesn't include "netrc"? – Plonetheus Mar 02 '23 at 16:44

3 Answers3

2

Renpy 8.0.3 doesn't ship with the necessary packages of the Python standard library (in this case 'netrc' and '_multibytecodec'). I asked the main developer about it. He now included more of the standard library as part of the Renpy nightly build, which of course will make its way into the next release.

The current nightly build supports the required modules.

So importing the openai module as described earlier is now possible. Just make sure to delete the "certifi" module from python-packages, as it is already part of the Renpy environment.

  • Wow, thanks ! Actually I simply re-made a chatgpt module that doesn't use netrc which wasn't really necessary and here it is : https://github.com/Taiko3615/RenPyChatGPTExample – Taiko Mar 24 '23 at 09:37
0

You just have to install the dependencies in the project's files. To do so, go to the project's directory and run

pip install --target game/python-packages netrc
mknull
  • 49
  • 4
0

There was really no way around, but actually it was super simple to just re-program the openai package using "requests" which is a package supported by renpy

https://github.com/Taiko3615/RenPyChatGPTExample

# Import required libraries
import requests
import json

# Define the completion function that takes messages and an API key as input
def completion(messages, api_key):
    # Set the API endpoint URL for ChatGPT completions
    url = "https://api.openai.com/v1/chat/completions"

    # Set the headers for the API request, including the Content-Type and Authorization with the API key
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }

    # Set the data for the API request, including the model and the input messages
    data = {
        "model": "gpt-3.5-turbo",
        "messages": messages
    }

    # Send the API request using the POST method, passing the headers and the data as JSON
    response = requests.post(url, headers=headers, data=json.dumps(data))

    # Check if the response status code is 200 (successful)
    if response.status_code == 200:
        # Extract the message from the response JSON and append it to the messages list
        completion = response.json()["choices"][0]["message"]
        messages.append(completion)
        return messages  # Return the updated messages list
    else:
        # If the status code is not 200, raise an exception with the error details
        raise Exception(f"Error: {response.status_code}, {response.text}")
Taiko
  • 1,351
  • 1
  • 19
  • 35