Notebooks
A
Arize AI
Evaluate Code Readability Classifications

Evaluate Code Readability Classifications

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

Open In Colab

phoenix logo
Docs | GitHub | Community

Code Readability Evals

Arize provides tooling to evaluate LLM applications, including tools to determine the readability or unreadability of code generated by LLM applications.

The purpose of this notebook is:

  • to evaluate the performance of an LLM-assisted approach to classifying generated code as readable or unreadable using datasets with ground-truth labels
  • to provide an experimental framework for users to iterate and improve on the default classification template.

Install Dependencies and Import Libraries

[1]
[2]
ERROR: Could not find a version that satisfies the requirement ip (from versions: none)
ERROR: No matching distribution found for ip

ℹ️ 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.

[3]
[4]

Download Benchmark Dataset

We'll evaluate the evaluation system consisting of an LLM model and settings in addition to an evaluation prompt template against a benchmark datasets of readable and unreadable code with ground-truth labels. Currently supported datasets for this task include:

  • openai_humaneval_with_readability
[5]

Display Binary Readability Classification Template

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

[6]

You are a stern but practical senior software engineer who cares a lot about simplicity and
readability of code. Can you review the following code that was written by another engineer?
Focus on readability of the code. Respond with "readable" if you think the code is readable,
or "unreadable" if the code is unreadable or needlessly complex for what it's trying
to accomplish.

ONLY respond with "readable" or "unreadable"

Task Assignment:
```
{input}
```

Implementation to Evaluate:
```
{output}
```

The template variables are:

  • input: the query from the user describing the coding task
  • output: an implementation of the coding task

Configure the LLM

Configure your OpenAI API key.

[7]

Instantiate the LLM and set parameters.

Benchmark Dataset Sample

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

[8]

LLM Evals: Code Readability Classifications GPT-4

Run readability classifications against a subset of the data.

[9]
[10]
"Hello! I'm working perfectly. How can I assist you today?"
[11]
llm_classify |          | 0/10 (0.0%) | ⏳ 00:00<? | ?it/s

Evaluate the predictions against human-labeled ground-truth readability labels.

[12]
              precision    recall  f1-score   support

    readable       0.83      0.83      0.83         6
  unreadable       0.75      0.75      0.75         4

    accuracy                           0.80        10
   macro avg       0.79      0.79      0.79        10
weighted avg       0.80      0.80      0.80        10

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

Inspecting evaluations

Because the evals are binary classifications, we can easily sample a few rows where the evals deviated from ground truth and see what the actual code was in that case.

[13]

Classifications with explanations

When evaluating a dataset for readability, it can be useful to know why the LLM classified text as readable or not. 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.

[14]
Using prompt:


You are a stern but practical senior software engineer who cares a lot about simplicity and
readability of code. Can you review the following code that was written by another engineer?
Focus on readability of the code. The implementation is "readable" if you think the code is
readable, or "unreadable" if the code is unreadable or needlessly complex for what it's trying
to accomplish.

Task Assignment:
```
{input}
```

Implementation to Evaluate:
```
{output}
```

Please read the code carefully, then write out in a step by step manner an EXPLANATION to show how
to evaluate the readability of the code. Avoid simply stating the correct answer at the outset.
Your response LABEL must be a single word, either "readable" or "unreadable", and should not
contain any text or characters aside from that. "readable" means that the code is readable.
"unreadable" means the code is unreadable or needlessly complex for what it's trying to accomplish.

Example response:
************
EXPLANATION: An explanation of your reasoning for why the label is "readable" or "unreadable"
LABEL: "readable" or "unreadable"
************

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 |          | 0/5 (0.0%) | ⏳ 00:00<? | ?it/s
- Snapped 'unreadable' to rail: unreadable
- Snapped 'readable' to rail: readable
- Snapped 'readable' to rail: readable
- Snapped 'readable' to rail: readable
- Snapped 'unreadable' to rail: unreadable
[15]

LLM Evals: Code Readability Classifications GPT-3.5

Run readability classifications against a subset of the data.

[17]
llm_classify |          | 0/10 (0.0%) | ⏳ 00:00<? | ?it/s
[18]
              precision    recall  f1-score   support

    readable       0.86      1.00      0.92         6
  unreadable       1.00      0.75      0.86         4

    accuracy                           0.90        10
   macro avg       0.93      0.88      0.89        10
weighted avg       0.91      0.90      0.90        10

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

Preview: GPT-4 Turbo

[20]
llm_classify |          | 0/10 (0.0%) | ⏳ 00:00<? | ?it/s
[21]
              precision    recall  f1-score   support

    readable       0.80      0.67      0.73         6
  unreadable       0.60      0.75      0.67         4

    accuracy                           0.70        10
   macro avg       0.70      0.71      0.70        10
weighted avg       0.72      0.70      0.70        10

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