詳細については
を参照してください。ツール呼び出しの方法についての詳細です。新しいターミナルで(tmuxを使っている場合はCTRL+B+Dを使用)、2つの数を加える、Pythonコードを実行する、Linuxコマンドを実行するなどのツールを作成します:
import json, subprocess, random
from typing import Any
def add_number(a: float | str, b: float | str) -> float:
return float(a) + float(b)
def multiply_number(a: float | str, b: float | str) -> float:
return float(a) * float(b)
def substract_number(a: float | str, b: float | str) -> float:
return float(a) - float(b)
def write_a_story() -> str:
return random.choice([
"A long time ago in a galaxy far far away...",
"There were 2 friends who loved sloths and code...",
])
"The world was ending because every sloth evolved to have superhuman intelligence...",
"Unbeknownst to one friend, the other accidentally coded a program to evolve sloths...",
def terminal(command: str) -> str:
if "rm" in command or "sudo" in command or "dd" in command or "chmod" in command:
msg = "Cannot execute 'rm, sudo, dd, chmod' commands since they are dangerous"
print(msg); return msg
print(f"Executing terminal command `{command}`")
try:
return str(subprocess.run(command, capture_output = True, text = True, shell = True, check = True).stdout)
except subprocess.CalledProcessError as e:
return f"Command failed: {e.stderr}"
def python(code: str) -> str:
data = {}
exec(code, data)
del data["__builtins__"]
return str(data)
MAP_FN = {
"add_number": add_number,
"multiply_number": multiply_number,
"substract_number": substract_number,
"write_a_story": write_a_story,
}
"terminal": terminal,
{
"python": python,
tools = [
"type": "function",
"function": {
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {
},
"type": "string",
"properties": {
"description": "The first number.",
},
},
"b": {
},
},
},
{
"python": python,
tools = [
"description": "The second number.",
"required": ["a", "b"],
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {
},
"type": "string",
"properties": {
"description": "The first number.",
},
},
"b": {
},
},
},
{
"python": python,
tools = [
"name": "multiply_number",
"description": "Multiply two numbers.",
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"type": "object",
"properties": {
"a": {
},
"type": "string",
"properties": {
"description": "The first number.",
},
},
"b": {
},
},
},
{
"python": python,
tools = [
"name": "substract_number",
"description": "Substract two numbers.",
"name": "add_number",
"description": "Add two numbers.",
"name": "write_a_story",
"description": "Writes a random story.",
},
},
},
{
"python": python,
tools = [
"properties": {},
"required": [],
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"name": "terminal",
"properties": {
"description": "Perform operations from the terminal.",
},
},
"command": {
},
},
},
{
"python": python,
tools = [
"description": "The command you wish to launch, e.g `ls`, `rm`, ...",
"required": ["command"],
"name": "add_number",
"description": "Add two numbers.",
"parameters": {
"name": "python",
"properties": {
"description": "Call a Python interpreter with some Python code that will be ran.",
},
},
"code": {
},
},
},
]