Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure support multitask.py #136

Merged
merged 1 commit into from
Nov 4, 2023
Merged

Azure support multitask.py #136

merged 1 commit into from
Nov 4, 2023

Conversation

zboyles
Copy link
Contributor

@zboyles zboyles commented Nov 4, 2023

Azure support for Multiple Extractions

Without validation, Azure use breaks with some chunks not containing any choices and the first function_call chunk not containing arguments.

As of API version "2023-07-01-preview", most chunks related to a function call seem to spit out all at once. So be aware the experience won't match OpenAI until it's addressed in a future API update.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling in the new task extraction process to prevent potential issues and improve overall app stability.

Azure support for Multiple Extractions
Copy link
Contributor

coderabbitai bot commented Nov 4, 2023

Walkthrough

The recent changes focus on enhancing error handling in the tasks_from_chunks class, specifically within the extract_json method. The modifications ensure that the keys delta and function_call exist before their values are accessed, and if arguments is present in function_call, its value is yielded. This prevents potential KeyError exceptions, improving the robustness of the code.

Changes

File Summary
.../dsl/multitask.py Enhanced error handling in the extract_json method of the tasks_from_chunks class. Added checks for the existence of delta and function_call keys before accessing their values. If arguments key is present in function_call, its value is yielded.

Poem

🍂 As autumn leaves fall, we code with glee, 🍁

Fixing bugs, making changes, as busy as a bee. 🐝

No more KeyErrors, no more fright, 🌙

Our code now robust, shining bright. 💡

So let's celebrate this day, with a cup of hot tea, ☕

And continue to code, as happy as can be. 😊


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5139780 and 5023d2d.
Files selected for processing (1)
  • instructor/dsl/multitask.py (1 hunks)

Comment on lines 31 to 39
@staticmethod
def extract_json(completion):
for chunk in completion:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
yield delta["function_call"]["arguments"]
if chunk["choices"]:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the choices key will always be present in the chunk dictionary. This could lead to a KeyError if choices is not present. It would be safer to check for the existence of choices before accessing it. Also, it's a good practice to check if choices is not empty before accessing its first element. Here is the improved code:

- if chunk["choices"]:
+ if "choices" in chunk and chunk["choices"]:

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
@staticmethod
def extract_json(completion):
for chunk in completion:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
yield delta["function_call"]["arguments"]
if chunk["choices"]:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
@staticmethod
def extract_json(completion):
for chunk in completion:
- if chunk["choices"]:
+ if "choices" in chunk and chunk["choices"]:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]

Comment on lines +34 to +38
if chunk["choices"]:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the delta key will always be present in the choices[0] dictionary. This could lead to a KeyError if delta is not present. It would be safer to check for the existence of delta before accessing it. Here is the improved code:

- delta = chunk["choices"][0]["delta"]
+ delta = chunk["choices"][0].get("delta")

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if chunk["choices"]:
delta = chunk["choices"][0]["delta"]
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
if chunk["choices"]:
delta = chunk["choices"][0].get("delta")
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]

Comment on lines +36 to +38
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the function_call key will always be present in the delta dictionary. This could lead to a KeyError if function_call is not present. It would be safer to check for the existence of function_call before accessing it. Here is the improved code:

- if "function_call" in delta:
+ if delta and "function_call" in delta:

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
if delta and "function_call" in delta:
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]

Comment on lines +37 to +38
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes that the arguments key will always be present in the function_call dictionary. This could lead to a KeyError if arguments is not present. It would be safer to check for the existence of arguments before accessing it. Here is the improved code:

- if "arguments" in delta["function_call"]:
+ if "arguments" in delta.get("function_call", {}):

Commitable suggestion

[!IMPORTANT]
Carefully review the code before committing. Make sure it correctly replaces the highlighted code, has no missing lines and indentaion issues.

Suggested change
if "arguments" in delta["function_call"]:
yield delta["function_call"]["arguments"]
if "arguments" in delta.get("function_call", {}):
yield delta["function_call"]["arguments"]

@jxnl jxnl merged commit fc15d8c into instructor-ai:main Nov 4, 2023
@jxnl
Copy link
Collaborator

jxnl commented Nov 4, 2023

it's now in 0.2.10

ivanleomk pushed a commit that referenced this pull request Nov 5, 2023
Azure support for Multiple Extractions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants