|
| 1 | +from together import Together |
| 2 | + |
| 3 | +client = Together() |
| 4 | + |
| 5 | +# Create a code interpreter instance |
| 6 | +code_interpreter = client.code_interpreter |
| 7 | + |
| 8 | +# Example 1: Simple print statement |
| 9 | +print("Example 1: Simple print") |
| 10 | +response = code_interpreter.run(code='print("Hello from Together!")', language="python") |
| 11 | +print(f"Status: {response.data.status}") |
| 12 | +for output in response.data.outputs: |
| 13 | + print(f"{output.type}: {output.data}") |
| 14 | +if response.data.errors: |
| 15 | + print(f"Errors: {response.data.errors}") |
| 16 | +print("\n") |
| 17 | + |
| 18 | +# Example 2: Using session for maintaining state |
| 19 | +print("Example 2: Using session for state") |
| 20 | +response1 = code_interpreter.run(code="x = 42", language="python") |
| 21 | +session_id = response1.data.session_id |
| 22 | + |
| 23 | +response2 = code_interpreter.run( |
| 24 | + code='print(f"The value of x is {x}")', language="python", session_id=session_id |
| 25 | +) |
| 26 | +for output in response2.data.outputs: |
| 27 | + print(f"{output.type}: {output.data}") |
| 28 | +if response2.data.errors: |
| 29 | + print(f"Errors: {response2.data.errors}") |
| 30 | +print("\n") |
| 31 | + |
| 32 | +# Example 3: More complex computation |
| 33 | +print("Example 3: Complex computation") |
| 34 | +code = """ |
| 35 | +!pip install numpy |
| 36 | +import numpy as np |
| 37 | +
|
| 38 | +# Create a random matrix |
| 39 | +matrix = np.random.rand(3, 3) |
| 40 | +print("Random matrix:") |
| 41 | +print(matrix) |
| 42 | +
|
| 43 | +# Calculate eigenvalues |
| 44 | +eigenvalues = np.linalg.eigvals(matrix) |
| 45 | +print("\\nEigenvalues:") |
| 46 | +print(eigenvalues) |
| 47 | +""" |
| 48 | + |
| 49 | +response = code_interpreter.run(code=code, language="python") |
| 50 | +for output in response.data.outputs: |
| 51 | + print(f"{output.type}: {output.data}") |
| 52 | +if response.data.errors: |
| 53 | + print(f"Errors: {response.data.errors}") |
| 54 | + |
| 55 | +# Example 4: Uploading and using a file |
| 56 | +print("Example 4: Uploading and using a file") |
| 57 | + |
| 58 | +# Define the file content and structure as a dictionary |
| 59 | +file_to_upload = { |
| 60 | + "name": "data.txt", |
| 61 | + "encoding": "string", |
| 62 | + "content": "This is the content of the uploaded file.", |
| 63 | +} |
| 64 | + |
| 65 | +# Code to read the uploaded file |
| 66 | +code_to_read_file = """ |
| 67 | +try: |
| 68 | + with open('data.txt', 'r') as f: |
| 69 | + content = f.read() |
| 70 | + print(f"Content read from data.txt: {content}") |
| 71 | +except FileNotFoundError: |
| 72 | + print("Error: data.txt not found.") |
| 73 | +""" |
| 74 | + |
| 75 | +response = code_interpreter.run( |
| 76 | + code=code_to_read_file, |
| 77 | + language="python", |
| 78 | + files=[file_to_upload], # Pass the file dictionary in a list |
| 79 | +) |
| 80 | + |
| 81 | +# Print results |
| 82 | +print(f"Status: {response.data.status}") |
| 83 | +for output in response.data.outputs: |
| 84 | + print(f"{output.type}: {output.data}") |
| 85 | +if response.data.errors: |
| 86 | + print(f"Errors: {response.data.errors}") |
| 87 | +print("\n") |
| 88 | + |
| 89 | +# Example 5: Uploading a script and running it |
| 90 | +print("Example 5: Uploading a python script and running it") |
| 91 | + |
| 92 | +script_content = "import sys\nprint(f'Hello from {sys.argv[0]}!')" |
| 93 | + |
| 94 | +# Define the script file as a dictionary |
| 95 | +script_file = { |
| 96 | + "name": "myscript.py", |
| 97 | + "encoding": "string", |
| 98 | + "content": script_content, |
| 99 | +} |
| 100 | + |
| 101 | +code_to_run_script = "!python myscript.py" |
| 102 | + |
| 103 | +response = code_interpreter.run( |
| 104 | + code=code_to_run_script, |
| 105 | + language="python", |
| 106 | + files=[script_file], # Pass the script dictionary in a list |
| 107 | +) |
| 108 | + |
| 109 | +# Print results |
| 110 | +print(f"Status: {response.data.status}") |
| 111 | +for output in response.data.outputs: |
| 112 | + print(f"{output.type}: {output.data}") |
| 113 | +if response.data.errors: |
| 114 | + print(f"Errors: {response.data.errors}") |
| 115 | +print("\n") |
| 116 | + |
| 117 | +# Example 6: Uploading a base64 encoded image (simulated) |
| 118 | + |
| 119 | +print("Example 6: Uploading a base64 encoded binary file (e.g., image)") |
| 120 | + |
| 121 | +# Example: A tiny 1x1 black PNG image, base64 encoded |
| 122 | +# In a real scenario, you would read your binary file and base64 encode its content |
| 123 | +tiny_png_base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=" |
| 124 | + |
| 125 | +image_file = { |
| 126 | + "name": "tiny.png", |
| 127 | + "encoding": "base64", # Use base64 encoding for binary files |
| 128 | + "content": tiny_png_base64, |
| 129 | +} |
| 130 | + |
| 131 | +# Code to check if the file exists and its size (Python doesn't inherently know image dimensions from bytes alone) |
| 132 | +code_to_check_file = ( |
| 133 | + """ |
| 134 | +import os |
| 135 | +import base64 |
| 136 | +
|
| 137 | +file_path = 'tiny.png' |
| 138 | +if os.path.exists(file_path): |
| 139 | + # Read the raw bytes back |
| 140 | + with open(file_path, 'rb') as f: |
| 141 | + raw_bytes = f.read() |
| 142 | + original_bytes = base64.b64decode('""" |
| 143 | + + tiny_png_base64 |
| 144 | + + """') |
| 145 | + print(f"File '{file_path}' exists.") |
| 146 | + print(f"Size on disk: {os.path.getsize(file_path)} bytes.") |
| 147 | + print(f"Size of original decoded base64 data: {len(original_bytes)} bytes.") |
| 148 | +
|
| 149 | +else: |
| 150 | + print(f"File '{file_path}' does not exist.") |
| 151 | +""" |
| 152 | +) |
| 153 | + |
| 154 | +response = code_interpreter.run( |
| 155 | + code=code_to_check_file, |
| 156 | + language="python", |
| 157 | + files=[image_file], |
| 158 | +) |
| 159 | + |
| 160 | +# Print results |
| 161 | +print(f"Status: {response.data.status}") |
| 162 | +for output in response.data.outputs: |
| 163 | + print(f"{output.type}: {output.data}") |
| 164 | +if response.data.errors: |
| 165 | + print(f"Errors: {response.data.errors}") |
| 166 | +print("\n") |
0 commit comments