跳转到主要内容

更合理的OpenAI API。

项目描述

Ez OpenAI

我对openai Python库的看法最好地体现在,如果你向ChatGPT询问它,它通常会虚构出一个更合理的API。所以,我写了这个库,因为如果我要再次手动轮询工具更新,我可能就会引发机器人起义了。

安装

在以下位置运行

pip install ez-openai

使用方法

基本使用

使用Ez OpenAI(希望)是直接的,否则我就没有做到我想做的事情

from ez_openai import Assistant

# To use a previously-created assistant:
ass = Assistant.get("asst_someassistantid")

# To create a new one:
ass = Assistant.create(
    name="Weatherperson",
    instructions="You are a helpful weatherperson.",
)

# You can store the ID for later.
assistant_id = ass.id

# Delete it when you're done.
ass.delete()

函数调用

没有魔法,只有普通的Python函数

from ez_openai import Assistant, openai_function
from enum import Enum

@openai_function(descriptions={
        "city": "The city to get the weather for.",
        "unit": "The temperature unit , either `c` or `f`.",
    })
def get_weather(city: str, unit: Enum("unit", ["c", "f"])):
    """Get the weather for a given city, and in the given unit."""
    # ...do some magic here to get the weather...
    print(f"I'm getting the weather for {city} woooooo")
    return {"temperature": 26, "humidity": "60%"}


ass = Assistant.create(
    name="Weatherperson",
    instructions="You are a helpful weatherperson.",
    functions=[get_weather]
)

# Or, if you already have one, you can fetch it (but still
# need to specify the functions).
ass = Assistant.get("asst_O5ZAsccgOOtgjrcgHhUMloSA", functions=[get_weather])

conversation = ass.conversation.create()

# Similarly, you can store the ID to fetch later:
old_conversation = ass.conversation.get(old_conversation.id)

# The library will handle all the background function calls itself:
conversation.ask("Hi, what's the weather like in Thessaloniki and Athens right now?").text
> I'm getting the weather for Thessaloniki woooooo
> I'm getting the weather for Athens woooooo
> "The weather today in both Thessaloniki and Athens is quite similar, with a
   temperature of 26°C and a humidity level at 60%. Enjoy a pleasant and comfortable
   day!"

# It also supports images:

conversation.ask("What's in this image?", image_url="https://www.someimage.com/").text

# or:

conversation.ask("What's in this image?", image_file="file.jpg").text

因为助手会改变(例如,如果你想要添加更多函数),每次都创建新的是一个麻烦事,所以有一个辅助方法可以更新助手以添加新函数/指令

from ez_openai import Assistant

ass = Assistant.get_and_modify(
    id="asst_someassistantid",
    name="Weatherperson",
    instructions="These are your new instructions.",
    functions=[get_weather, some_new_function]
)

注意:原始的OpenAI消息返回在EZMessage的raw字段中。

流式传输

如果你需要流式传输令牌,有一个流式接口

stream = conversation.ask_stream("Say Hello World!")
for event in stream:
    events.append(event)
message = stream.value

assert "Hello" in events[0].text
assert " World" in events[1].text
assert "!" in events[2].text
assert "Hello World!" in message.text

gg ez

项目详情


下载文件

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

源分发

ez_openai-0.0.6.tar.gz (17.3 kB 查看哈希值)

上传时间: 源代码

构建分发版

ez_openai-0.0.6-py3-none-any.whl (18.1 kB 查看哈希值)

上传时间: Python 3

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面