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([
"遠い昔、はるか彼方の銀河で...",
"ナマケモノとコードを愛する二人の友人がいました...",
"世界はすべてのナマケモノが超人的知能を獲得したため終わりを迎えていた...",
"一方の友人が知らないうちに、もう一方がナマケモノを進化させるプログラムを誤って作成してしまった...",
])
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"ターミナルコマンド `{command}` を実行しています")
try:
return str(subprocess.run(command, capture_output = True, text = True, shell = True, check = True).stdout)
except subprocess.CalledProcessError as e:
return f"コマンドが失敗しました: {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": "二つの数を加えます。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "二番目の数。",
},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "multiply_number",
"description": "二つの数を掛けます。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "二番目の数。",
},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "substract_number",
"description": "二つの数を引きます。",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "string",
"description": "最初の数。",
},
"b": {
"type": "string",
"description": "二番目の数。",
},
},
"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"],
},
},
},
]