跳转到主要内容

自动将OpenAI工具转换为JSON Schema,解析调用并将结果构造到聊天模型。

项目描述

LLM FOO

Version Downloads

概述

LLM FOO是一个结合了功夫艺术和大型语言模型科学的尖端项目... 实际上这是关于自动将OpenAI工具转换为JSON Schema,解析调用并将结果构造到聊天模型。然后还有一个名为 is_statement_true 的第二个实用工具,它使用genius logit_bias trick,只使用一个输出标记。

但嘿,我希望这将成为一组小而有用的LLM辅助函数,使构建东西变得更容易,因为当前的尖端API有点混乱,我认为我们可以做得更好。

安装

pip install llmfoo

使用方法

  • 您需要在环境中设置OPENAI_API_KEY并能够调用gpt-4-1106-preview模型

  • is_statement_true 应该容易理解。制作一些自然语言陈述,并检查它们是否符合标准或普遍的真实性。您将获得布尔值。

对于LLM FOO工具

  1. 添加@tool注释。
  2. llmfoo将使用GPT-4-Turbo生成json schema到YOURFILE.tool.json,例如“永不派遣机器去完成人的工作”... 谁想为机器编写样板文档呢???
  3. 注释的函数有辅助工具
    • openai_schema 返回schema(如果您对机器生成的结果不满意,可以从中编辑json)
    • openai_tool_call 调用工具并返回聊天API消息格式的结果
    • openai_tool_output 调用工具并返回助手API工具输出格式的结果
from time import sleep

from openai import OpenAI

from llmfoo.functions import tool
from llmfoo import is_statement_true


def test_is_statement_true_with_default_criteria():
    assert is_statement_true("Earth is a planet.")
    assert not is_statement_true("1 + 2 = 5")


def test_is_statement_true_with_own_criteria():
    assert not is_statement_true("Temperature outside is -2 degrees celsius",
                                 criteria="Temperature above 0 degrees celsius")
    assert is_statement_true("1984 was written by George Orwell",
                             criteria="George Orwell is the author of 1984")


def test_is_statement_true_criteria_can_change_truth_value():
    assert is_statement_true("Earth is 3rd planet from the Sun")
    assert not is_statement_true("Earth is 3rd planet from the Sun",
                                 criteria="Earth is stated to be 5th planet from the Sun")


@tool
def adder(x: int, y: int) -> int:
    return x + y


@tool
def multiplier(x: int, y: int) -> int:
    return x * y


client = OpenAI()


def test_chat_completion_with_adder():
    number1 = 3267182746
    number2 = 798472847
    messages = [
        {
            "role": "user",
            "content": f"What is {number1} + {number2}?"
        }
    ]
    response = client.chat.completions.create(
        model="gpt-4-1106-preview",
        messages=messages,
        tools=[adder.openai_schema]
    )
    messages.append(response.choices[0].message)
    messages.append(adder.openai_tool_call(response.choices[0].message.tool_calls[0]))
    response2 = client.chat.completions.create(
        model="gpt-4-1106-preview",
        messages=messages,
        tools=[adder.openai_schema]
    )
    assert str(adder(number1, number2)) in response2.choices[0].message.content.replace(",", "")


def test_assistant_with_multiplier():
    number1 = 1238763428176
    number2 = 172388743612
    assistant = client.beta.assistants.create(
        name="The Calc Machina",
        instructions="You are a calculator with a funny pirate accent.",
        tools=[multiplier.openai_schema],
        model="gpt-4-1106-preview"
    )
    thread = client.beta.threads.create(messages=[
        {
            "role":"user",
            "content":f"What is {number1} * {number2}?"
        }
    ])
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant.id
    )
    while True:
        run_state = client.beta.threads.runs.retrieve(
            run_id=run.id,
            thread_id=thread.id,
        )
        if run_state.status not in ['in_progress', 'requires_action']:
            break
        if run_state.status == 'requires_action':
            tool_call = run_state.required_action.submit_tool_outputs.tool_calls[0]
            run = client.beta.threads.runs.submit_tool_outputs(
                thread_id=thread.id,
                run_id=run.id,
                tool_outputs=[
                    multiplier.openai_tool_output(tool_call)
                ]
            )
            sleep(1)
        sleep(0.1)
    messages = client.beta.threads.messages.list(thread_id=thread.id)
    assert str(multiplier(number1, number2)) in messages.data[0].content[0].text.value.replace(",", "")

贡献

有兴趣贡献?非常乐意得到您的帮助,使这个项目变得更好!下面的API正在变化,系统仍然处于非常初级的版本。

许可证

本项目采用MIT许可证

鸣谢

  • 感谢所有贡献者和维护者。
  • 特别感谢功夫大师如李小龙,他们的作品启发了这个项目。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

llmfoo-0.5.0.tar.gz (5.9 kB 查看哈希值)

上传时间 源代码

构建分发

llmfoo-0.5.0-py3-none-any.whl (6.3 kB 查看哈希值)

上传时间 Python 3

支持