Skip to content

Commit 417648b

Browse files
Add example for running LLM code
1 parent 3a8f970 commit 417648b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,34 @@ import judge0
223223
client = judge0.get_client()
224224
print(client.get_languages())
225225
```
226+
227+
### Running LLM-Generated Code
228+
229+
```python
230+
import os
231+
from ollama import Client
232+
import judge0
233+
234+
# Get your Ollama Cloud API key from https://ollama.com.
235+
client = Client(
236+
host="https://ollama.com",
237+
headers={"Authorization": "Bearer " + os.environ.get("OLLAMA_API_KEY")},
238+
)
239+
240+
system = "You are a helpful assistant that can execute Python code. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
241+
prompt = "Calculate how many r's are in the word 'strawberry'."
242+
243+
response = client.chat(
244+
model="gpt-oss:120b-cloud",
245+
messages=[
246+
{"role": "system", "content": system},
247+
{"role": "user", "content": prompt},
248+
],
249+
)
250+
251+
code = response["message"]["content"]
252+
print(f"Generated code:\n{code}")
253+
254+
result = judge0.run(source_code=code, language=judge0.PYTHON)
255+
print(f"Execution result:\n{result.stdout}")
256+
```

0 commit comments

Comments
 (0)