HuggingFace镜像/gorilla-openfunctions-v2
模型介绍文件和版本分析
下载使用量0

Gorilla OpenFunctions v2

💡 开源模型的最先进水平。与 GPT-4 不相上下。

🚀 查看 Berkeley Function Calling Leaderboard
📣 在我们的 OpenFunctions v2 发布博客 和 Berkeley Function Calling Leaderboard 博客 中了解更多信息
🟢 在 gorilla-llm/gorilla-openfunctions-v2-gguf 中查看量化的 GGUF 模型

简介

Gorilla OpenFunctions 扩展了大型语言模型(LLM)的聊天补全功能,能够根据自然语言指令和 API 上下文生成可执行的 API 调用。借助 OpenFunctions v2,我们现在支持:

  1. 多函数调用 - 在多个函数之间进行选择
  2. 并行函数调用 - 使用不同的参数值多次调用同一个函数
  3. 多函数与并行结合 - 在单次聊天补全调用(一次生成)中同时实现上述两种功能
  4. 相关性检测 - 聊天时正常对话,当被要求调用函数时则返回函数调用
  5. Python - 支持 string、number、boolean、list、tuple、dict 等参数数据类型,并对原生不支持的类型使用 Any
  6. JAVA - 支持 byte、short、int、float、double、long、boolean、char、Array、ArrayList、Set、HashMap、Hashtable、Queue、Stack 以及 Any 数据类型
  7. JavaScript - 支持 String、Number、Bigint、Boolean、dict(object)、Array、Date 以及 Any 数据类型
  8. REST - 原生 REST 支持

性能

模型总体准确率*
GPT-4-0125-Preview85.12%
Gorilla-openfunctions-v283.67%
GPT-3.5-turbo82.23%
Mistral-medium79.70%
Nexusflow Raven-v255.72%
GPT-4-061354.16%
*:总体准确率的定义详见 Berkeley Function Calling Leaderboard 博客,感兴趣的话可以阅读更多详细信息!

可用模型

模型功能
gorilla-openfunctions-v2多函数调用、并行函数调用、多函数并行调用、相关性检测、支持 Python + JAVA + JS + REST
gorilla-openfunctions-v1并行函数调用,并能在函数间进行选择
gorilla-openfunctions-v0给定函数和用户意图,返回格式正确且参数准确的 json

我们所有的模型均托管在 Huggingface 上的加州大学伯克利分校 gorilla-llm 组织中:gorilla-openfunctions-v2、gorilla-openfunctions-v1 和 gorilla-openfunctions-v0。

训练

Gorilla Openfunctions v2 是一个拥有 70 亿参数的模型,它构建于 deepseek coder LLM 之上。想要了解更多关于数据构成以及训练过程中的一些见解,请查看 openfunctions-v2 博客。

示例用法(托管版)

有关文件依赖项和所用工具的信息,请参考 https://github.com/ShishirPatil/gorilla/tree/main/openfunctions 中的 README.md。

  1. OpenFunctions 与 OpenAI Functions 兼容
!pip install openai==0.28.1
  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-v2",
      temperature=0.0,
      messages=[{"role": "user", "content": prompt}],
      functions=functions,
    )
    return completion.choices[0]
  except Exception as e:
    print(e, model, prompt)
  1. 传入用户参数和函数集,Gorilla OpenFunctions 会返回一个格式完整的 json
query = "What's the weather like in the two cities of Boston and San Francisco?"
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"],
        },
    }
]
get_gorilla_response(query, functions=functions)
  1. 预期输出 新增

Gorilla 返回一个易于访问的字符串 以及 与 Open-AI 兼容的 JSON。

{
  "index": 0,
  "message": {
    "role": "assistant",
    "content": "get_current_weather(location='Boston, MA'), get_current_weather(location='San Francisco, CA')",
    "function_call": [
      {
        "name": "get_current_weather",
        "arguments": {
          "location": "Boston, MA"
        }
      },
      {
        "name": "get_current_weather",
        "arguments": {
          "location": "San Francisco, CA"
        }
      }
    ]
  },
  "finish_reason": "stop"
}

我们保留了社区喜爱的 OpenFunctions v1 中的字符串功能,例如上文的 get_current_weather(location='Boston, MA'), get_current_weather(location='San Francisco, CA')。同时,请注意 JSON 中的 function_call 键与 OpenAI 兼容。

这在 OpenFunctions v2 中得以实现,因为我们确保输出包含参数名称而非仅包含值。这使我们能够将输出解析为 JSON。在输出无法解析为 JSON 的情况下,我们将始终返回函数调用字符串。

端到端示例

运行 [inference_hosted.py](https://github.com/ShishirPatil/gorilla/tree/main/openfunctions) 中的示例代码,了解模型的工作原理。

python inference_hosted.py

预期输出:

(.py3) shishir@dhcp-132-64:~/Work/Gorilla/openfunctions/$ python inference_hosted.py
--------------------
Function call strings(s): get_current_weather(location='Boston, MA'), get_current_weather(location='San Francisco, CA')
--------------------
OpenAI compatible `function_call`: [<OpenAIObject at 0x1139ba890> JSON: 
{
  "name": "get_current_weather",
  "arguments": 
  {
    "location": "Boston, MA"
  }
}, <OpenAIObject at 0x1139ba930> JSON: {
  "name": "get_current_weather",
  "arguments": 
  {
    "location": "San Francisco, CA"
  }
}]

在本地运行 OpenFunctions

如果您想在本地运行 OpenFunctions,以下是我们使用的提示词格式:

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.
    """
    system = "You are an AI programming assistant, utilizing the Gorilla LLM model, developed by Gorilla LLM, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer."
    if len(functions) == 0:
        return f"{system}\n### Instruction: <<question>> {user_query}\n### Response: "
    functions_string = json.dumps(functions)
    return f"{system}\n### Instruction: <<function>>{functions_string}\n<<question>>{user_query}\n### Response: "

此外,以下是我们格式化响应的方式:

使用以下命令安装依赖:

pip3 install tree_sitter
git clone https://github.com/tree-sitter/tree-sitter-java.git
git clone https://github.com/tree-sitter/tree-sitter-javascript.git

您可以使用以下代码来格式化响应:


from openfunctions_utils import strip_function_calls, parse_function_call

def format_response(response: str):
    """
    Formats the response from the OpenFunctions model.

    Parameters:
    - response (str): The response generated by the LLM.

    Returns:
    - str: The formatted response.
    - dict: The function call(s) extracted from the response.

    """
    function_call_dicts = None
    try:
        response = strip_function_calls(response)
        # Parallel function calls returned as a str, list[dict]
        if len(response) > 1: 
            function_call_dicts = []
            for function_call in response:
                function_call_dicts.append(parse_function_call(function_call))
            response = ", ".join(response)
        # Single function call returned as a str, dict
        else:
            function_call_dicts = parse_function_call(response[0])
            response = response[0]
    except Exception as e:
        # Just faithfully return the generated response str to the user
        pass
    return response, function_call_dicts
        

在当前目录下,运行 inference_local.py 中的示例代码,即可了解模型的工作方式。

python inference_local.py

注意: 仅当您在本地部署时,才使用 get_prompt 和 format_response。如果您通过聊天补全 API 使用伯克利托管的模型,我们会在后端处理这些步骤,因此您无需手动操作。该模型支持 Hugging Face 🤗 Transformers,可在本地运行:

许可证

Gorilla OpenFunctions v2 基于 Apache 2.0 许可证分发。本软件包含来自 Deepseek 模型的元素。因此,Gorilla OpenFunctions v2 的许可遵循 Apache 2.0 许可证,并附加 Deepseek 许可证的 附录 A 中规定的条款。

贡献

Gorilla 是加州大学伯克利分校的开源项目,我们欢迎贡献者。 请通过电子邮件向我们发送您的意见、建议和问题。有关项目的更多信息,请访问 https://gorilla.cs.berkeley.edu/