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内容生成函数
from llm2json.output import JSONParser
ernieResult = ernieChat(template)
parser = JSONParser()
print(parser.to_dict(ernieResult))
5. 生成效果
{
'title': '文心牌润唇膏,滋润保湿不粘腻!',
'context': '✨想要拥有水润双唇吗?试试文心牌润唇膏!💦质地轻盈,一抹即化,保湿效果超赞!💋',
'keywords': ['文心牌润唇膏', '保湿', '轻盈质地', '水润双唇']
}
📚演示案例
-
情感分类
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'}
-
地址提取
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': '武汉市'}
-
生成模拟数据
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' }
-
More examples will be comming soon…
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
337fb823b79031e7fdf3bca0ddd36ba0057a9153a2cd30e1bc7d212fa62ccf14
|
|
| MD5 |
ba32ca2ec9ed2260104b2f1ab399458e
|
|
| BLAKE2b-256 |
ddc97dd58ba984f73a320202230082457130fa83121e3f9448935897b9918f97
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd6884b79b7b74f9a6909eaec8d6452170afd9194c689a7cc429a01b986c3b9e
|
|
| MD5 |
ca3f96cf518b6befd3b2b038b78169db
|
|
| BLAKE2b-256 |
521f16262bbfb012e5eb7a77560bbdbcaa522a2a15b448da95df447544b7e5b2
|