Connect Riza’s Python code interpreter to Groq
groq
and rizaio
packages with pip
:
code.py
file:
code.py
:
tools
and tool_choice
parameters in our initial request:
"auto"
is the default value for tool_choice
, and signals to the model that it can choose whether or not
to use a tool. Setting this to "required"
will force the model to always use a tool, which gives you a similar
level of control to the alternative direct prompting method described below.
We wrap this request in a simple try
/except
because the Groq API occasionally fails to handle tool use requests and raises
groq.BadRequestError
with a message similar to "Failed to call a function. Please adjust your prompt. See 'failed_generation' for more details."
.
code.py
after making all of the above additions:
exec_python
tool with a valid Python script that
produces the required information for the model to provide a useful response.
Asking for something that doesn’t require code won’t trigger tool use:
code.py
:
response_format
parameter with the value {"type": "json_object"}
to enable JSON mode:
json
module to parse the model’s response:
exec()
or similar direct local execution.
code.py
after making all of the above additions: