Skip to main content

Web Based Multi Purpose Annotation Software

Project description

⚡LLM2Json⚡是一个易于使用的格式化大语言模型输出工具包,它的主要设计思想和部分实现代码参考 Langchain 的output parser(但初步输出结果测试优于Langchain)。它可以通过自动构建prompts引导大语言模型输出符合JSON语法的返回数据,解决了大语言模型格式化输出、数据交互、前端开发等遇到的问题,使下游的应用程序开发更加方便快捷。

🚀快速开始

pip 安装

pip install llm2json

快速上手

1. 构造返回数据结构体

from llm2json.prompts.schema import BaseModel, Field

class Xiaohongshu(BaseModel):
    title: str = Field(description="文章标题")
    context: str = Field(description = "正文内容")
    keywords: list = Field(description = "关键词")

2.  给定返回数据正确案例(可选项,但建议配置)

correct_example = '{"title":"文章标题", "context":"正文内容", "keywords":["关键词1","关键词2"]}'

3. 生成模板

from llm2json.prompts import Templates

t = Templates(prompt="""
              请你为商品:<{topic}>写一篇小红书文案。
              包括文章标题、正文内容和关键词,同时正文需要包含emoji表情
              """, 
          field=Xiaohongshu,
          correct_example=correct_example)mple)

template = t.invoke(topic="文心牌润唇膏")

4. 格式化返回结果

[!NOTE]

ernieChat() 是基于文心ErnieBot SDK封装的LLM内容生成函数

具体实现可参考:GitHub - PaddlePaddle/ERNIE-SDK: ERNIE Bot Agent is a Large Language Model (LLM) Agent Framework, powered by the advanced capabilities of ERNIE Bot and the platform resources of Baidu AI Studio.

from llm2json.output import JSONParser

ernieResult = ernieChat(template)
parser = JSONParser()
print(parser.to_dict(ernieResult))

5. 生成效果

{
    'title': '文心牌润唇膏,滋润保湿不粘腻!',
    'context': '✨想要拥有水润双唇吗?试试文心牌润唇膏!💦质地轻盈,一抹即化,保湿效果超赞!💋',
    'keywords': ['文心牌润唇膏', '保湿', '轻盈质地', '水润双唇']
}

📚演示案例

  1. 情感分类

    class Senta(BaseModel):
        sentiment: str = Field(description="情感倾向,取值为positive或negative")
    
    t = Templates(prompt="""
                  我会给你一段评论,请你判断这段评论是正面还是负面的。
                  评论内容是:{sentiment}
                  """, 
                  field=Senta,
              )
    
    template = t.invoke(sentiment="蛋糕味道不错,店家服务也很热情")
    
    ernieResult = ernieChat(template)
    parser = JSONParser()
    print(parser.to_dict(ernieResult))
    
    {'sentiment': 'positive'}
    
  2. 地址提取

    class Address(BaseModel):
        city: str = Field(description="地级市")
    
    t = Templates(prompt="""
                  我会给你一个地址,请你从中提取出地级市名称。
                  地址是:{address}
                  """, 
                  field=Address,
              )
    
    template = t.invoke(address="湖北省武汉市汉阳区琴台大道附近")
    
    ernieResult = ernieChat(template)
    parser = JSONParser()
    print(parser.to_dict(ernieResult))
    
    {'city': '武汉市'}
    
  3. 生成模拟数据

    class Data(BaseModel):
        name: str = Field(description="姓名")
        idcode: str = Field(description="18位数的身份证号")
        sex: str = Field(description="性别")
        phone: str = Field(description="手机号")
        email: str = Field(description="邮箱")
        address: str = Field(description="居住地址")
        date: str = Field(description="入职日期")
    
    t = Templates(prompt="""
                  请你根据模板生成入职人员测试数据
                  """, 
              field=Data,
              )
    
    template = t.invoke()
    
    ernieResult = ernieChat(template)
    parser = JSONParser()
    print(parser.to_dict(ernieResult))
    
    {
        'name': '张三',
        'idcode': '123456789012345678',
        'sex': '男',
        'phone': '13800138000',
        'email': 'zhangsan@example.com',
        'address': '北京市朝阳区建国门外大街甲8号',
        'date': '2021-11-21'
    }
    
  4. More examples will be comming soon…

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm2json-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm2json-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file llm2json-0.1.0.tar.gz.

File metadata

  • Download URL: llm2json-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for llm2json-0.1.0.tar.gz
Algorithm Hash digest
SHA256 337fb823b79031e7fdf3bca0ddd36ba0057a9153a2cd30e1bc7d212fa62ccf14
MD5 ba32ca2ec9ed2260104b2f1ab399458e
BLAKE2b-256 ddc97dd58ba984f73a320202230082457130fa83121e3f9448935897b9918f97

See more details on using hashes here.

File details

Details for the file llm2json-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llm2json-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for llm2json-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd6884b79b7b74f9a6909eaec8d6452170afd9194c689a7cc429a01b986c3b9e
MD5 ca3f96cf518b6befd3b2b038b78169db
BLAKE2b-256 521f16262bbfb012e5eb7a77560bbdbcaa522a2a15b448da95df447544b7e5b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page