Search code examples
pythonopenai-api

openai library in Python not able to parse URLs to search using keywords


I have written a short chatbot prototype, using the OpenAI library, that takes a text input from the keyboard, runs that search through a list of websites, processes any returned text via ChatGPT and gives it back to the user.

I am using the following code to create a response

html = openai.File.create(url=source).text

Where source is created by iterating through a list of URLs.

I am building using a standard Raspberry Pi, Thonny is my IDE, Python 3.9.2.

I get the following errors. It seems to me that the openai.File.create method does not accept 'url', but I cannot find a list pf the acceptable keyword arguments in the Library documentation.

Do you how to use a keyword which will take a URL from my list per the above?

Many thanks

Error searching source https://en.wikipedia.org/wiki/Python_(programming_language): create() got an unexpected keyword argument 'url' Error searching source https://docs.python.org/3/tutorial/index.html: create() got an unexpected keyword argument 'url' Error searching source https://realpython.com/tutorials/: create() got an unexpected keyword argument 'url' Error searching source https://www.geeksforgeeks.org/python-programming-language/: create() got an unexpected keyword argument 'url' Error searching source https://www.python.org/: create() got an unexpected keyword argument 'url' Error searching source https://positivepython.co.uk: create() got an unexpected keyword argument 'url'


Solution

  • All the examples I've seen of File.create take in a file, not an url. The keyword is file. E.g.

    openai.File.create(file=open("myfile.jsonl"), purpose="search")
    

    according some docs the File.create method creates files.

    Here is the source code for the File class create method:

    @classmethod
    def create(
        cls,
        file,
        purpose,
        model=None,
        api_key=None,
        api_base=None,
        api_type=None,
        api_version=None,
        organization=None,
        user_provided_filename=None,
    ):
        requestor, url, files = cls.__prepare_file_create(
            file,
            purpose,
            model,
            api_key,
            api_base,
            api_type,
            api_version,
            organization,
            user_provided_filename,
        )
        response, _, api_key = requestor.request("post", url, files=files)
        return util.convert_to_openai_object(
            response, api_key, api_version, organization
        )
    

    as you can see, no url keyword.