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 · 308 天前 · 1556 次点击
    这是一个创建于 308 天前的主题,其中的信息可能已经有所发展或是发生改变。

    出于自己的需要,为了更好地使用 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  
       308 天前
    哈哈哈,看到帖子已经想到了是用 pydantic 。看了一下,function 描述生成可以考虑调用 pydantic 的,然后手动去除一些多余的信息。因为 pydantic 生成的描述对枚举类型之类的复杂类型有更好的支持。
    featureoverload
        2
    featureoverload  
       307 天前
    @abersheeran 确实,fastapi 的 Route 类已经处理过了。pip install fastapi; restapi = route(restapi); json.loads(restapi.xxx.json_schema());
    featureoverload
        3
    featureoverload  
       307 天前
    @featureoverload (没写完给发送了)

    差不多思路这样就可以:

    pip install fastapi; restapi = route(restapi); json.loads(restapi.xxx.json_schema());
    abersheeran
        4
    abersheeran  
       307 天前
    @featureoverload 几行代码搞定的东西非要下载个更大的库,而且我记得 fastapi 没有对外保证这个 API 吧?内部接口教人拿来用,
    lanlanye
        5
    lanlanye  
    OP
       306 天前
    @abersheeran #1 我没找到 pydantic 有可以处理函数的方法,目前用的__doc__是 Python 自己的特性,pydantic 只用来定义数据结构了,求问 pydantic 的哪个特性可以用来生成描述?
    abersheeran
        6
    abersheeran  
       306 天前
    lanlanye
        7
    lanlanye  
    OP
       306 天前
    @abersheeran #6 入参部分使用的话构造请求应该能更方便,但我的初衷是希望可以把现成的函数加上注释直接拿来用,如果强求用户这样定义入参或许不好,总之还是感谢。
    abersheeran
        8
    abersheeran  
       306 天前
    @lanlanye 就是加上类型标注直接生成了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2955 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:04 · PVG 22:04 · LAX 07:04 · JFK 10:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.