How to view the Python code executed by OpenAI Assistants Code interpreter?

Anonymous
2024-08-09T17:03:39.8133333+00:00

How can I view the Python code that is getting executed by the code interpreter in OpenAI assistants? Seeing the Python codes it generated, I could verify if the generated code/answer is right or wrong.

I am using the following code for Assistants Q&A.

import os
import time
import json
import requests
from openai import AzureOpenAI
  
client = AzureOpenAI(
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
    api_key= os.getenv("AZURE_OPENAI_API_KEY"),
    api_version="2024-05-01-preview"
)

assistant = client.beta.assistants.create(
    model="nbr-ds-gpt4o", # replace with model deployment name.
    instructions="Show me the thought process to solve the problem. Also, share the generated Python code along with the answer.",
    tools=[{"type":"code_interpreter"}],
    tool_resources={"code_interpreter":{"file_ids":[file.id]}}
)

# Create a thread
thread = client.beta.threads.create()
# Add a user question to the thread
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="tell me about the data you have" # Replace this with your prompt
)
 
# Run the thread
run = client.beta.threads.runs.create_and_poll(
  thread_id=thread.id,
  assistant_id=assistant.id
)
# Looping until the run completes or fails
while run.status in ['queued', 'in_progress', 'cancelling']:
    time.sleep(1)
    run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
	if run.status == 'completed':
  		messages = client.beta.threads.messages.list(thread_id=thread.id)
  		print(messages)
	elif run.status == 'requires_action':
		pass  		
	else:
  		print(run.status)


I see on the Assistant Playground, code interpreter does output some code, but it is not always possible to see the whole code. Is there a way to view the whole output by code_interpreter and access that in the above code?

User's image

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
4,080 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Kulbhashkar Dwivedi 0 Reputation points
    2024-08-19T17:50:14.1166667+00:00

    Follow these three steps

    1. Ask Directly: When you want to see the code, you can simply ask the assistant to show it. For example, you could say, "Can you show me the Python code you used to generate that result?" or "Please display the Python code you're about to run."
    2. Review Outputs: Often, when the assistant provides a result, it will also display the code used to generate that result. If it doesn't, you can request to see it by asking, "Can you provide the code for that?"
    3. Manual Verification: If you want to verify the code for correctness, you can copy the displayed code and run it in your own Python environment to check the results.
    0 comments No comments

  2. Kulbhashkar Dwivedi 0 Reputation points
    2024-08-19T17:54:02.77+00:00

    To view the Python code executed by the OpenAI Assistant's code interpreter in the context you're using, you can follow these steps:

    Include Code in Instructions: When setting up the assistant, ensure your instructions clearly specify that you want the generated Python code to be shown alongside the answer. You’ve already done this with the instruction "Show me the thought process to solve the problem. Also, share the generated Python code along with the answer."

    Review the Response: After the assistant processes the thread and returns the result, it should include the Python code used. You can review this in the output you receive from the client.beta.threads.runs.create_and_poll call.

    Check the Messages: The messages = client.beta.threads.messages.list(thread_id=thread.id) line will retrieve all messages from the thread. These messages should contain the Python code if the assistant followed your instructions correctly.

    Polling the Run: Your loop is designed to keep checking the run status until it's completed. Once completed, the retrieved messages will show the thought process and the Python code, which you can then verify.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.