Search code examples
pythonpython-3.xrosopenai-api

ModuleNotFoundError: No module named 'openai.embeddings_utils'


I only get that error if I run the script as a service in ROS:

import openai
from openai.embeddings_utils import get_embedding, cosine_similarity

import sys
import rospy
from servicio_palabras.srv import WordCount

openai.api_key = "--"

def ask_user():
    context = {"role": "system",
                "content": "Command System parser."}
    messages = [context]

    petition = input("What do you want me to do?: ")

    messages.append({"role": "user", "content": petition})

    response = openai.ChatCompletion.create(model="--", messages=messages)
    response_content = response.choices[0].message.content

    messages.append({"role": "assistant", "content": response_content})

    return response_content

def word_count_client(sentence):
    rospy.wait_for_service('word_count')
    try:
        word_count = rospy.ServiceProxy('word_count', WordCount)
        response = word_count(sentence)
        return response.words
    except rospy.ServiceException as e:
        print("Service call failed: %s"%e)

if __name__ == "__main__":
    input = ask_user()
    words = word_count_client(input)
    print("The words in the sentence are: ", words)

I have done pip install openai several times but i dont know where the problem is. I also have deactivated conda and done catkin_make and source devel/setup.bash in the terminal i am using.


Solution

  • openai.embeddings_utils does not exist in latest openai 1.2.0

    You can downgrade to:

    pip install openai==0.27.7