finic-ai / doctran

MIT License
479 stars 42 forks source link

Transformers.py no longer compatible with openai >= 1.0.0 #16

Open heyfletch opened 8 months ago

heyfletch commented 8 months ago

My transformer stopped working with latest version of openai. The error message spells it out pretty clearly:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

from langchain.schema import Document
from langchain_community.document_transformers import DoctranQATransformer

documents = [Document(page_content=input_text)]
qa_transformer = DoctranQATransformer()
transformed_document = qa_transformer.transform_documents(documents)
python transform.py
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/doctran/transformers/transformers.py", line 70, in executeOpenAICall
    completion = self.config.openai.ChatCompletion.create(**function_call.dict())
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/lib/_old_api.py", line 39, in __call__
    raise APIRemovedInV1(symbol=self._symbol)
openai.lib._old_api.APIRemovedInV1: 

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/doctran/doctran.py", line 136, in execute
    transformed_document = transformer.transform(transformed_document)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/doctran/transformers/transformers.py", line 55, in transform
    return self.executeOpenAICall(document)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/doctran/transformers/transformers.py", line 87, in executeOpenAICall
    raise Exception(f"OpenAI function call failed: {e}")
Exception: OpenAI function call failed: 

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/joe/Code/chunker/transform.py", line 40, in <module>
    transformed_document = qa_transformer.transform_documents(documents)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_community/document_transformers/doctran_text_qa.py", line 56, in transform_documents
    doctran_doc = doctran.parse(content=d.page_content).interrogate().execute()
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/doctran/doctran.py", line 140, in execute
    raise Exception(f"Error executing transformation {transformation}: {e}")
Exception: Error executing transformation (<Transformation.interrogate: 'DocumentInterrogator'>, {}): OpenAI function call failed: 

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. 

Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`

A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
aldrinjenson commented 8 months ago

Hey, @heyfletch I'm facing the same issue. Were you able to resolve this by downgrading openai to some other version?

heyfletch commented 8 months ago

No, I never resolved it, and I didn't try downgrading openai. If anyone does and it works, I'd be curious to know.

joselo85 commented 7 months ago

Downgrading openai worked for me. But if you need to use LangChain's newer versions, you'll need to upgrade openai once again.

piyush-accacia commented 7 months ago

Is there any update to this, I am trying to use this with langchain and there is conflict coming in

MrCredible commented 6 months ago

@heyfletch

I was able to resolve this issue, but I only made sure it works for me and did not do thorough testing.

You need to remove references to deployment_id and adjust the chat completions call to chat.completions.create in the transformers.py file

Notice the code around

            completion = self.config.openai.ChatCompletion.create(**function_call.dict())
            arguments = completion.choices[0].message["function_call"]["arguments"]

Previous code:

    def executeOpenAICall(self, document: Document) -> Document:
        try:
            function_call = OpenAIFunctionCall(
                deployment_id=self.config.openai_deployment_id,
                model=self.config.openai_model, 
                messages=[{"role": "user", "content": document.transformed_content}], 
                functions=[{
                    "name": self.function_name,
                    "description": self.function_description,
                    "parameters": self.function_parameters,
                }],
                function_call={"name": self.function_name}
            )
            completion = self.config.openai.ChatCompletion.create(**function_call.dict())
            arguments = completion.choices[0].message["function_call"]["arguments"]
            try:
                arguments = json.loads(arguments)
            except Exception as e:
                raise Exception("OpenAI returned malformatted JSON" +
                                "This is likely due to the completion running out of tokens. " +
                                f"Setting a higher token limit may fix this error. JSON returned: {arguments}")
            first_value = next(iter(arguments.values()))
            if len(arguments) > 1 or not isinstance(first_value, str):
                # If multiple arguments or a dict/list is returned, treat arguments as extracted values
                document.extracted_properties = document.extracted_properties or arguments
            else:
                # If there is only one argument and it's a string, treat arguments as transformed content
                document.transformed_content = first_value
            return document
        except Exception as e:
            raise Exception(f"OpenAI function call failed: {e}")

My updates:

    def executeOpenAICall(self, document: Document) -> Document:
        try:
            function_call = OpenAIFunctionCall(
                # deployment_id=self.config.openai_deployment_id,
                model=self.config.openai_model, 
                messages=[{"role": "user", "content": document.transformed_content}], 
                functions=[{
                    "name": self.function_name,
                    "description": self.function_description,
                    "parameters": self.function_parameters,
                }],
                function_call={"name": self.function_name}
            )
            completion = self.config.openai.chat.completions.create(**function_call.dict())
            arguments = completion.choices[0].message.function_call.arguments
            try:
                arguments = json.loads(arguments)
            except Exception as e:
                raise Exception("OpenAI returned malformatted JSON" +
                                "This is likely due to the completion running out of tokens. " +
                                f"Setting a higher token limit may fix this error. JSON returned: {arguments}")
            first_value = next(iter(arguments.values()))
            if len(arguments) > 1 or not isinstance(first_value, str):
                # If multiple arguments or a dict/list is returned, treat arguments as extracted values
                document.extracted_properties = document.extracted_properties or arguments
            else:
                # If there is only one argument and it's a string, treat arguments as transformed content
                document.transformed_content = first_value
            return document
        except Exception as e:
            raise Exception(f"OpenAI function call failed: {e}")

I do not plan on creating a branch and creating a pull request b/c I do not plan on thoroughly testing this since I am just creating personal prototype code

pechaut78 commented 4 months ago

and upgrading to latest would help accessing GPT4 o