Open heyfletch opened 8 months ago
Hey, @heyfletch I'm facing the same issue. Were you able to resolve this by downgrading openai to some other version?
No, I never resolved it, and I didn't try downgrading openai. If anyone does and it works, I'd be curious to know.
Downgrading openai worked for me. But if you need to use LangChain's newer versions, you'll need to upgrade openai once again.
Is there any update to this, I am trying to use this with langchain and there is conflict coming in
@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
and upgrading to latest would help accessing GPT4 o
My transformer stopped working with latest version of openai. The error message spells it out pretty clearly: