🚀 Gorilla OpenFunctions
Gorilla OpenFunctionsは、大規模言語モデル(LLM)のチャット補完機能を拡張し、自然言語の指示とAPIコンテキストを元に実行可能なAPI呼び出しを生成します。
🚀 Colab で試してみましょう
📣 OpenFunctionsブログのリリース で詳細を読むことができます
🚀 クイックスタート
Gorilla OpenFunctionsは、自然言語の指示とAPIコンテキストを元に、実行可能なAPI呼び出しを生成するために、大規模言語モデル(LLM)のチャット補完機能を拡張します。
✨ 主な機能
利用可能なモデル
モデル |
機能 |
gorilla - openfunctions - v0 |
関数とユーザーの意図を与えると、適切な引数を持つJSONを返します |
gorilla - openfunctions - v1 |
並列関数に対応し、関数を選択できます |
📦 インストール
OpenFunctionsはOpenAI Functionsと互換性があります。
!pip install openai==0.28.1
💻 使用例
ホスト型の使用例
- OpenAI Functionsとの互換性を利用してインストールします。
!pip install openai==0.28.1
- Gorillaのホストサーバーを指定します。
import openai
def get_gorilla_response(prompt="Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes", model="gorilla-openfunctions-v0", functions=[]):
openai.api_key = "EMPTY"
openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
try:
completion = openai.ChatCompletion.create(
model="gorilla-openfunctions-v0",
temperature=0.0,
messages=[{"role": "user", "content": prompt}],
functions=functions,
)
return completion.choices[0].message.content
except Exception as e:
print(e, model, prompt)
- ユーザー引数と関数のセットを渡すと、Gorilla OpenFunctionsは完全にフォーマットされたJSONを返します。
query = "Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes"
functions = [
{
"name": "Uber Carpool",
"api_name": "uber.ride",
"description": "Find suitable ride for customers given the location, type of ride, and the amount of time the customer is willing to wait as parameters",
"parameters": [{"name": "loc", "description": "location of the starting place of the uber ride"}, {"name":"type", "enum": ["plus", "comfort", "black"], "description": "types of uber ride user is ordering"}, {"name": "time", "description": "the amount of time in minutes the customer is willing to wait"}]
}
]
get_gorilla_response(query, functions=functions)
- 期待される出力
uber.ride(loc="berkeley", type="plus", time=10)
ローカル実行の使用例
import json
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
def get_prompt(user_query: str, functions: list = []) -> str:
"""
Generates a conversation prompt based on the user's query and a list of functions.
Parameters:
- user_query (str): The user's query.
- functions (list): A list of functions to include in the prompt.
Returns:
- str: The formatted conversation prompt.
"""
if len(functions) == 0:
return f"USER: <<question>> {user_query}\nASSISTANT: "
functions_string = json.dumps(functions)
return f"USER: <<question>> {user_query} <<function>> {functions_string}\nASSISTANT: "
device : str = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model_id : str = "gorilla-llm/gorilla-openfunctions-v0"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True)
model.to(device)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=128,
batch_size=16,
torch_dtype=torch_dtype,
device=device,
)
query: str = "Call me an Uber ride type \"Plus\" in Berkeley at zipcode 94704 in 10 minutes"
functions = [
{
"name": "Uber Carpool",
"api_name": "uber.ride",
"description": "Find suitable ride for customers given the location, type of ride, and the amount of time the customer is willing to wait as parameters",
"parameters": [
{"name": "loc", "description": "Location of the starting place of the Uber ride"},
{"name": "type", "enum": ["plus", "comfort", "black"], "description": "Types of Uber ride user is ordering"},
{"name": "time", "description": "The amount of time in minutes the customer is willing to wait"}
]
}
]
prompt = get_prompt(query, functions=functions)
output = pipe(prompt)
print(output)
🤝 コントリビューション
すべてのモデルと、モデルのトレーニングに使用されるデータは、Apache 2.0ライセンスの下で公開されています。GorillaはUC Berkeleyによるオープンソースプロジェクトであり、コントリビューターを歓迎します。コメント、批判、質問などはメールでお寄せください。プロジェクトの詳細については、https://gorilla.cs.berkeley.edu/ を参照してください。
📄 ライセンス
このプロジェクトはApache 2.0ライセンスの下で公開されています。