一个方便使用 Function calling 的小工具 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lanlanye
V2EX    Python

一个方便使用 Function calling 的小工具

  •  
  •   lanlanye
    laipz8200 2023-06-24 18:14:19 +08:00 2541 次点击
    这是一个创建于 914 天前的主题,其中的信息可能已经有所发展或是发生改变。

    出于自己的需要,为了更好地使用 OpenAI 的 Function calling 功能,做了一些微小的工作。

    思路是读取函数的类型注解和文档,从中解析出函数注释、参数类型和描述,然后自动构建出能够用于 Function calling 的参数。

    目前仅支持我用到的一些基本类型,发出来希望可以帮助到有同样需要的人,也希望能帮助我一起完善。

    仓库: https://github.com/laipz8200/openai-agent

    Function calling 文档: https://platform.openai.com/docs/guides/gpt/function-calling

    8 条回复    2023-06-26 20:35:51 +08:00
    abersheeran
        1
    abersheeran  
       2023-06-24 18:52:55 +08:00
    哈哈哈,看到帖子已经想到了是用 pydantic 。看了一下,function 描述生成可以考虑调用 pydantic 的,然后手动去除一些多余的信息。因为 pydantic 生成的描述对枚举类型之类的复杂类型有更好的支持。
    featureoverload
        2
    featureoverload  
       2023-06-25 10:09:18 +08:00
    @abersheeran 确实,fastapi 的 Route 类已经处理过了。pip install fastapi; restapi = route(restapi); json.loads(restapi.xxx.json_schema());
    featureoverload
        3
    featureoverload  
       2023-06-25 10:10:07 +08:00
    @featureoverload (没写完给发送了)

    差不多思路这样就可以:

    pip install fastapi; restapi = route(restapi); json.loads(restapi.xxx.json_schema());
    abersheeran
        4
    abersheeran  
       2023-06-25 15:13:10 +08:00
    @featureoverload 几行代码搞定的东西非要下载个更大的库,而且我记得 fastapi 没有对外保证这个 API 吧?内部接口教人拿来用,
    lanlanye
        5
    lanlanye  
    OP
       2023-06-26 17:28:03 +08:00
    @abersheeran #1 我没找到 pydantic 有可以处理函数的方法,目前用的__doc__是 Python 自己的特性,pydantic 只用来定义数据结构了,求问 pydantic 的哪个特性可以用来生成描述?
    abersheeran
        6
    abersheeran  
       2023-06-26 17:39:17 +08:00
    lanlanye
        7
    lanlanye  
    OP
       2023-06-26 18:44:54 +08:00
    @abersheeran #6 入参部分使用的话构造请求应该能更方便,但我的初衷是希望可以把现成的函数加上注释直接拿来用,如果强求用户这样定义入参或许不好,总之还是感谢。
    abersheeran
        8
    abersheeran  
       2023-06-26 20:35:51 +08:00
    @lanlanye 就是加上类型标注直接生成了。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3190 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 10:56 PVG 18:56 LAX 02:56 JFK 05:56
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86