At this moment, Llama 3 is one of the most capable open-source models. In this guide, we give Llama 3 code interpreter capabilities and test it on data analysis and data visualization task.
We will show how to build a code interpreter with Llama 3 on Groq, and powered by open-sourceCode Interpreter SDK by E2B. The E2B Code Interpreter SDK quickly creates a secure cloud sandbox powered by Firecracker. Inside this sandbox is a running Jupyter server that the LLM can use.
Then we store the Groq and E2B API keys and set the model name for the Llama 3 instance you will use. In the system prompt we define sets the rules for the interaction with Llama.
# TODO: Get your Groq AI API key from https://console.groq.com/GROQ_API_KEY = ""# TODO: Get your E2B API key from https://docs.e2b.devE2B_API_KEY = ""# Or use 8b version# MODEL_NAME = "llama3-8b-8192"MODEL_NAME = "llama3-70b-8192"SYSTEM_PROMPT = """you are a python data scientist. you are given tasks to complete and you run python code to solve them.- the python code runs in jupyter notebook.- every time you call `execute_python` tool, the python code is executed in a separate cell. it's okay to multiple calls to `execute_python`.- display visualizations using matplotlib or any other visualization library directly in the notebook. don't worry about saving the visualizations to a file.- you have access to the internet and can make api requests.- you also have access to the filesystem and can read/write files.- you can install any pip package (if it exists) if you need to but the usual packages for data analysis are already preinstalled.- you can run any python code you want, everything is running in a secure sandbox environment"""tools = [ { "type": "function", "function": { "name": "execute_python", "description": "Execute python code in a Jupyter notebook cell and returns any result, stdout, stderr, display_data, and error.", "parameters": { "type": "object", "properties": { "code": { "type": "string", "description": "The python code to execute in a single cell.", } }, "required": ["code"], }, }, }]
3. Creating code interpreter
We define the main function that uses the E2B code interpreter to execute code in a Jupyter Notebook cell. We'll be calling this function a little bit further when we're parsing the Llama's response with tool calls.
def code_interpret(e2b_code_interpreter, code): print("Running code interpreter...") exec = e2b_code_interpreter.notebook.exec_cell( code, on_stderr=lambda stderr: print("[Code Interpreter]", stderr), on_stdout=lambda stdout: print("[Code Interpreter]", stdout), # You can also stream code execution results # on_result=... ) if exec.error: print("[Code Interpreter ERROR]", exec.error) else: return exec.results
4. Calling Llama 3
Now we're going to define and implement chat_with_llama method. In this method, we'll call the LLM with our tools dictionary, parse the output, and call our code_interpret method we defined above.
Finally, we can instantiate the code interpreter and pass the E2B API key. Then we call the chat_with_llama method with our user message and the code_interpreter instance.
from e2b_code_interpreter import CodeInterpreterwith CodeInterpreter(api_key=E2B_API_KEY) as code_interpreter: code_results = chat_with_llama( code_interpreter, "Visualize a distribution of height of men based on the latest data you know" ) if code_results: first_result = code_results[0] else: print("No code results") exit(0)# This will render the image# You can also access the data directly# first_result.png# first_result.jpg# first_result.pdf# ...first_result
==================================================User message: Visualize a distribution of height of men based on the latest data you know==================================================Running code interpreter...