見出し画像

Function calling に対応したLlama API(llama2)

OpenAI互換のAPIでllama2モデルをホストする、LLamaAPIが公開されていたので、さっそく試してみました。

Llama API のページでユーザー登録してAPIキーを取得します。

OpenAIのFunction Callingのサンプルを入力してみます。

!pip install llamaapi -q
from llamaapi import LlamaAPI

# Replace 'Your_API_Token' with your actual API token
llama = LlamaAPI(`取得したAPI KEY')
import json

schema ={
    "messages": [
        {"role": "user", "content": "What is the weather like in Boston?"}
    ],
    "functions": [
        {
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
            "type": "object",
            "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {
                "type": "string",
                "enum": ["celsius", "fahrenheit"]
            }
            },
            "required": ["location"]
        }
        }
    ]
}

 実行!

response = llama.run(schema)
response.json()['choices'][0]

{'index': 0,
 'message': {
  'role': 'assistant',
  'content': None,
  'function_call': {
   'name': 'get_current_weather',
   'arguments': {'location': 'Boston', 'unit': 'celsius'}
  }
 },
 'finish_reason': 'function_call'
}

いい感じです。
おそらく、近いうちにLangChainのAgentなども対応してくれそうです。
また、せっかくllama2なので、今後、ローカル環境でも動くFunction Calling対応のAPIの登場にも期待したいですね。

お読みいただきありがとうございました。

この記事が気に入ったらサポートをしてみませんか?