を参照)、いくつかのツール呼び出しを行うことができます:
"role": "user",
"content": [{"type": "text", "text": "Write 'I'm a happy Sloth' to a file, then print it back to me."}],
}]
messages = unsloth_inference(messages, temperature = 0.15, top_p = 1.0, top_k = -1, min_p = 0.00)
詳細については
を参照してください。ツール呼び出しの方法についての詳細です。新しいターミナルで(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": {
},
},
},
]
from openai import OpenAI
"required": ["code"],
次に以下の関数を使用します(コピーして貼り付けて実行してください)。これらは関数呼び出しを自動的に解析し、任意のモデルに対してOpenAIエンドポイントを呼び出します:
temperature = 1.0,
messages,
temperature = 0.7,
top_p = 0.95,
top_k = 40,
):
min_p = 0.01,
from openai import OpenAI
import json
openai_client = OpenAI(
)
repetition_penalty = 1.0,
messages = messages.copy()
model_name = next(iter(openai_client.models.list())).id
print(f"Using model = {model_name}")
has_tool_calls = True
original_messages_len = len(messages)
while has_tool_calls:
print(f"Current messages = {messages}")
response = openai_client.chat.completions.create(
model = model_name,
messages = messages,
temperature = temperature,
top_p = top_p,
tools = tools if tools else None,
)
tool_choice = "auto" if tools else None,
extra_body = {"top_k": top_k, "min_p": min_p, "repetition_penalty" :repetition_penalty,}
tool_calls = response.choices[0].message.tool_calls or []
content = response.choices[0].message.content or ""
tool_calls_dict = [tc.to_dict() for tc in tool_calls] if tool_calls else tool_calls
messages.append({"role": "assistant", "tool_calls": tool_calls_dict, "content": content,})
for tool_call in tool_calls:
fx, args, _id = tool_call.function.name, tool_call.function.arguments, tool_call.id
out = MAP_FN[fx](**json.loads(args))
messages.append({"role": "tool", "tool_call_id": _id, "name": fx, "content": str(out),})
else:
を参照)、いくつかのツール呼び出しを行うことができます:
"role": "user",
"content": [{"type": "text", "text": "Write 'I'm a happy Sloth' to a file, then print it back to me."}],
}]
messages = unsloth_inference(messages, temperature = 1.0, top_p = 1.0, top_k = 40, min_p = 0.00)