Notebooks
A
Arize AI
Evaluate Code Functionality Classifications

Evaluate Code Functionality Classifications

agentsllmsLlamaIndexarize-phoenixopenaitutorialsevalsllmopsai-monitoringaiengineeringprompt-engineeringdatasetsllm-evalai-observabilityllm-evaluationsmolagentslegacyanthropiclangchain

Open In Colab

phoenix logo
Docs | GitHub | Community

Code Functionality Evals

This tests whether code is written correctly, without bugs, accomplishes the functionality you want, does not have syntax errors.

The purpose of this notebook is:

  • to evaluate the performance of code fuctionality Eval
  • to provide an experimental framework for users to iterate and improve on the default classification template.

Install Dependencies and Import Libraries

[1]

ℹ️ To enable async request submission in notebook environments like Jupyter or Google Colab, optionally use nest_asyncio. nest_asyncio globally patches asyncio to enable event loops to be re-entrant. This is not required for non-notebook environments.

Without nest_asyncio, eval submission can be much slower, depending on your organization's rate limits. Speed increases of about 5x are typical.

[2]
[3]

Download Benchmark Dataset

TODO

[4]
# This is formatted as code

Display Code Functionality Classification Template

View the default template used to code functionality. You can tweak this template and evaluate its performance relative to the default.

[6]

Code Evaluation Prompt:
TEST EDIT
-----------------------
Evaluate the provided code to determine its correctness in solving the given instruction.

Data:
-----
[Instruction]: {coding_instruction}
  Clearly define the task or problem that the code aims to address.

[Reference Code]: {code}
  Examine the submitted code for evaluation in the context of the provided instruction.

Evaluation:
-----------
Provide a concise response with a single word: either "bug_free" or "is_bug".
- "bug_free" signifies that the code correctly and efficiently solves the instruction with no bugs.
- "is_bug" indicates that the code either fails to meet the instruction requirements or contains bugs.

Example:
-----------

[Instruction]: Implement the Fibonacci sequence in Python.

[Reference Code]: 'def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

for i in range(10):
    print(fibonacci(i))'

[Output]: bug_free

Note: Assumptions can be made that any code needed for the instruction is correct, and optimization is not a requirement for a correct solution. Your response should consist solely of the words "bug_free" or "is_bug" without additional text or characters.

The template variables are:

  • coding_instruction: What is the code supposed to do as an instruction
  • code: The code to evaluate

Configure the LLM

Configure your OpenAI API key.

[7]

Benchmark Dataset Sample

Sample size determines run time Recommend iterating small: 100 samples Then increasing to large test set

LLM Evals: Code Functionality Classifications GPT-4

Run Code Functionality against a subset of the data. Instantiate the LLM and set parameters.

[8]
The `model_name` field is deprecated. Use `model` instead.                 This will be removed in a future release.
[9]
"Hello! I'm working perfectly. How can I assist you today?"

Run Code Func Classifications

Run code functionality classifications against a subset of the data.

[10]
llm_classify |██████████| 50/50 (100.0%) | ⏳ 00:08<00:00 |  6.20it/s

Evaluate Classifications

Evaluate the predictions against human-labeled ground-truth code functionality labels.

[11]
              precision    recall  f1-score   support

    bug_free       0.96      0.81      0.88        31
      is_bug       0.75      0.95      0.84        19

    accuracy                           0.86        50
   macro avg       0.86      0.88      0.86        50
weighted avg       0.88      0.86      0.86        50

<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>
Output

Classifications with explanations

When evaluating a dataset for code functionality, it can be useful to know why the LLM classified a document as relevant or irrelevant. The following code block runs llm_classify with explanations turned on so that we can inspect why the LLM made the classification it did. There is speed tradeoff since more tokens is being generated but it can be highly informative when troubleshooting.

[12]
Using prompt:


Code Evaluation Prompt:
-----------------------
Evaluate the provided code to determine its correctness in solving the given instruction.

Data:
-----
[Instruction]: {coding_instruction}
  Clearly define the task or problem that the code aims to address.

[Reference Code]: {code}
  Examine the submitted code for evaluation in the context of the provided instruction.

Evaluation:
-----------
Provide a concise response with a explanation and a single word LABEL: either "bug_free" or "is_bug".
- "bug_free" signifies that the code correctly and efficiently solves the instruction with no bugs.
- "is_bug" indicates that the code either fails to meet the instruction requirements or contains bugs.

Example:
-----------

[Instruction]: Implement the Fibonacci sequence in Python.

[Reference Code]: 'def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

for i in range(10):
    print(fibonacci(i))'

[Output]: bug_free

Note: Assumptions can be made that any code needed for the instruction is correct, and optimization is not a requirement for a correct solution. Your response should consist solely of the words "bug_free" or "is_bug" without additional text or characters.

Please read the instruction and code carefully, then write out in a step by step manner an EXPLANATION to show how
to evaluate the functionality of the code. Avoid simply stating the correct answer at the outset.
You are then going to respond with a LABEL (a single word evaluation).
If the reference code functionally solves the instruction problem without any bugs than call it "bug_free". 
If reference code has bugs or does not functionally solve the instruction problem than call it "is_bug".

Example response:
************
EXPLANATION: An explanation of your reasoning for why the code is bug_free or is_bug
LABEL: "bug_free" or "is_bug"
************

EXPLANATION:

OpenAI invocation parameters: {'model': 'gpt-4', 'temperature': 0.0, 'max_tokens': 256, 'frequency_penalty': 0, 'presence_penalty': 0, 'top_p': 1, 'n': 1, 'timeout': None}
llm_classify |██        | 1/5 (20.0%) | ⏳ 00:02<00:11 |  2.97s/it
- Snapped 'is_bug' to rail: is_bug
llm_classify |████      | 2/5 (40.0%) | ⏳ 00:04<00:06 |  2.07s/it 
- Snapped 'is_bug' to rail: is_bug
llm_classify |████████  | 4/5 (80.0%) | ⏳ 00:05<00:00 |  1.06it/s 
- Snapped 'is_bug' to rail: is_bug
- Snapped 'bug_free' to rail: bug_free
llm_classify |██████████| 5/5 (100.0%) | ⏳ 00:09<00:00 |  2.07s/it
- Snapped 'bug_free' to rail: bug_free
llm_classify |██████████| 5/5 (100.0%) | ⏳ 00:10<00:00 |  2.07s/it
[13]

LLM Evals: code functionality Classifications GPT-3.5 Turbo

Run Code functionality against a subset of the data using GPT-3.5. GPT-3.5 can significantly speed up the classification process. However there are tradeoffs as we will see below.

[14]
The `model_name` field is deprecated. Use `model` instead.                 This will be removed in a future release.
[15]
llm_classify |██████████| 50/50 (100.0%) | ⏳ 00:10<00:00 |  4.59it/s
[16]
              precision    recall  f1-score   support

    bug_free       0.95      0.68      0.79        31
      is_bug       0.64      0.95      0.77        19

    accuracy                           0.78        50
   macro avg       0.80      0.81      0.78        50
weighted avg       0.84      0.78      0.78        50

<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>
Output

Preview: Running with GPT-4 Turbo

[17]
The `model_name` field is deprecated. Use `model` instead.                 This will be removed in a future release.
llm_classify |██████████| 50/50 (100.0%) | ⏳ 00:12<00:00 |  4.16it/s
[18]
              precision    recall  f1-score   support

    bug_free       1.00      0.77      0.87        31
      is_bug       0.73      1.00      0.84        19

    accuracy                           0.86        50
   macro avg       0.87      0.89      0.86        50
weighted avg       0.90      0.86      0.86        50

<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>
Output