コピー
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 subtract_number(a: float | str, b: float | str) -> float:
return float(a) - float(b)
def write_a_story() -> str:
return random.choice([
"はるか昔、はるか遠くの銀河で...",
"スロースとコードを愛する2人の友人がいました...",
"すべてのスロースが超人的な知能を持つよう進化したため、世界は終わろうとしていた...",
"ある友人が気づかないうちに、もう一人が誤ってスロースを進化させるプログラムをコードしていた...",
])
def terminal(command: str) -> str:
if "rm" in command or "sudo" in command or "dd" in command or "chmod" in command:
msg = "'rm, sudo, dd, chmod' コマンドは危険なので実行できません"
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,
"subtract_number": subtract_number,
"write_a_story": write_a_story,
"terminal": terminal,
"python": python,
}
tools = [
{
"type": "function",
"function": {
"name": "add_number",
"description": "2つの数を足します。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "2番目の数。",
},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "multiply_number",
"description": "2つの数を掛けます。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "2番目の数。",
},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "subtract_number",
"description": "2つの数を引きます。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "2番目の数。",
},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "write_a_story",
"description": "ランダムな物語を書きます。",
"parameters": {
"type": "object",
"properties": {},
"required": [],
},
},
},
{
"type": "function",
"function": {
"name": "terminal",
"description": "ターミナルから操作を実行します。",
"parameters": {
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "起動したいコマンド。例: `ls`, `rm`, ...",
},
},
"required": ["command"],
},
},
},
{
"type": "function",
"function": {
"name": "python",
"description": "実行される Python コードを使って Python インタープリタを呼び出します。",
"parameters": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "実行する Python コード",
},
},
"required": ["code"],
},
},
},
]