Aarav
Aarav

Reputation: 1

ModuleNotFoundError: No module named 'pydantic_core._pydantic_core' When importing openai python sdk to renpy

when importing openai python sdk in a renpy project

init python:           

    from openai import OpenAI     

This error appear

I'm sorry, but an uncaught exception occurred.  While running game code:   File "game/script.rpy", line 22, in script     init python:   File "game/script.rpy", line 27, in <module>     from openai import OpenAI ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

The full traceback can be found here

I installed the Openai library using
pip install --target game/python-packages openai

I tried installing the pydantic core and pydantic-package via pip

pip install --target game/python-packages pydantic-core

pip install --target game/python-packages pydantic

I also tried deleting all the packages and installing them again but still no avail
It's only an issue with the openai package
the other installed packages work fine

I can see both pydantic and pydantic core package in the python-packages folder
The Pydantic-core version is 2.16.3
and the pydantic version is 2.6.4

Upvotes: 0

Views: 944

Answers (1)

Syntax_z
Syntax_z

Reputation: 1

Renpy isn't able to properly detect .pyd files which is why even though the file exists in the correct location, it can't be called.

You could try using an old version of openai's python library that doesn't use a .pyd file (maybe version 0.27.4) or use the request library instead, that way you can use openai without having to pip install it directly at all

So something like this:


import requests

history = [
    {
        "role": "system",
        "content": "You are a silly pirate"
    }
]

def chat(user_input):
    url = "https://api.openai.com/v1/chat/completions"
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer (your token)",
    }
    payload = {
        "model": "gpt-3.5-turbo-1106",
        "messages": user_input,
        "max_tokens": 100,
        "temperature": 0.7,
    }

    response = requests.post(url, headers=headers, json=payload)

    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"].strip()
    else:
        print("An error happened")


print("Say stuff")

while True:
    user_input = input("You: ")
    if user_input.lower() == "exit":
        print("AI: Goodbye!")
        break

    history.append({"role": "user", "content": user_input})
    chatbot_response = chat(history)
    history.append({"role": "assistant", "content": chatbot_response})

    print(f"AI: {chatbot_response}")

And a reminder that you can check the documentation for info on what the specific parameters do in the payload https://platform.openai.com/docs/api-reference/introduction

Upvotes: 0

Related Questions