Last active
November 28, 2024 05:31
-
-
Save taowen/66e9803a5a17760b871cedb490a696e1 to your computer and use it in GitHub Desktop.
根据 json 生成 python 的 typed dict。验证 haiku 对 few shot 的指令遵从性
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const YOUR_CLAUDE_KEY = '' | |
async function main() { | |
const resp = await fetch('https://api.anthropic.com/v1/messages', { | |
method: 'POST', | |
headers: { | |
"x-api-key": YOUR_CLAUDE_KEY, | |
"anthropic-version": '2023-06-01', | |
"content-type": "application/json", | |
}, | |
body: JSON.stringify({ | |
"model": 'claude-3-haiku-20240307', | |
"max_tokens": 4000, | |
"system": 'generate python TypedDict according to JSON examples', | |
"messages": [{ | |
role: "user", content: JSON.stringify([ | |
{ | |
"name": "张三", | |
"age": 25, | |
"is_student": true, | |
"height": 1.75, | |
"favorite_colors": ["红色", "蓝色", "绿色"], | |
"contact_info": { | |
"email": "[email protected]", | |
"phone": "123456789" | |
}, | |
"scores": { | |
"math": 92.5, | |
"english": 88.0 | |
}, | |
"hobbies": ["篮球", "阅读", "旅游"], | |
"birthdate": [1998, 5, 15] | |
}, | |
{ | |
"name": "李四", | |
"age": 32, | |
"is_student": false, | |
"height": 1.68, | |
"favorite_colors": ["黑色", "白色"], | |
"contact_info": { | |
"email": "[email protected]", | |
"phone": "987654321" | |
}, | |
"scores": { | |
"project_a": 95.0, | |
"project_b": 88.5 | |
}, | |
"hobbies": ["摄影", "烹饪", "园艺"], | |
"birthdate": [1991, 11, 22] | |
}, | |
{ | |
"name": "王五", | |
"age": 19, | |
"is_student": true, | |
"height": 1.82, | |
"favorite_colors": ["黄色", "紫色"], | |
"contact_info": { | |
"email": "[email protected]", | |
"phone": "456789012" | |
}, | |
"scores": { | |
"physics": 78.0, | |
"chemistry": 92.0 | |
}, | |
"hobbies": ["音乐", "绘画", "编程"], | |
"birthdate": [2004, 8, 3] | |
} | |
]) | |
}, { | |
role: 'assistant', content: ` | |
class TheModel(typing.TypedDict): | |
name: str | |
age: int | |
is_student: bool | |
height: float | |
favorite_colors: list[str] | |
contact_info: ContactInfo | |
scores: dict[str, float] | |
hobbies: set[str] | |
birthdate: tuple[int, int, int] | |
class ContactInfo(typing.TypedDict): | |
email: str | |
phone: str | |
` | |
}, { | |
role: 'user', content: JSON.stringify([ | |
{ | |
"shape_type": "RECT", | |
"direction": "L2R", | |
"relative_transform": [[1, 2, 3]], | |
"children": [] | |
}, | |
{ | |
"shape_type": "STAR", | |
"direction": "R2L", | |
"relative_transform": [[4, 5, 6], [7, 8, 9]], | |
"children": [ | |
{ | |
"shape_type": "CIRCLE", | |
"direction": "L2R", | |
"relative_transform": [[10, 11, 12]], | |
"children": [] | |
} | |
] | |
}, | |
{ | |
"shape_type": "CIRCLE", | |
"direction": "L2R", | |
"relative_transform": [], | |
"children": [ | |
{ | |
"shape_type": "RECT", | |
"direction": "R2L", | |
"relative_transform": [[13, 14, 15]], | |
"children": [] | |
}, | |
{ | |
"shape_type": "STAR", | |
"direction": "L2R", | |
"relative_transform": [[16, 17, 18], [19, 20, 21]], | |
"children": [] | |
} | |
] | |
} | |
]) | |
}, { | |
role: 'assistant', content: ` | |
class TheModel(typing.TypedDict): | |
shape_type: typing.Literal['RECT','STAR','CIRCLE'] | |
direction: typing.Literal['L2R', 'R2L'] | |
relative_transform: tuple[tuple[int, int, int]] | |
children: list['TheModel'] | |
` | |
}, { | |
role: 'user', content: JSON.stringify([ | |
{ | |
"orderId": "ORD2023032701", | |
"orderDate": "2023-03-27T15:22:18Z", | |
"customer": { | |
"name": "张三", | |
"email": "[email protected]" | |
}, | |
"items": [ | |
{ | |
"name": "电脑主机", | |
"price": 5999.99, | |
"quantity": 1 | |
}, | |
{ | |
"name": "显示器", | |
"price": 1299.00, | |
"quantity": 1 | |
} | |
], | |
"totalAmount": 7298.99, | |
"shippingAddress": { | |
"street": "软件园路1号", | |
"city": "北京市", | |
"province": "北京市", | |
"postalCode": "100089" | |
}, | |
"paymentMethod": "信用卡" | |
}, | |
{ | |
"orderId": "ORD2023032702", | |
"orderDate": "2023-03-27T09:15:32Z", | |
"customer": { | |
"name": "李四", | |
"email": "[email protected]" | |
}, | |
"items": [ | |
{ | |
"name": "运动鞋", | |
"price": 399.00, | |
"quantity": 2 | |
}, | |
{ | |
"name": "运动背包", | |
"price": 129.99, | |
"quantity": 1 | |
} | |
], | |
"totalAmount": 927.99, | |
"shippingAddress": { | |
"street": "学院路88号", | |
"city": "上海市", | |
"province": "上海市", | |
"postalCode": "200433" | |
}, | |
"paymentMethod": "支付宝" | |
}, | |
{ | |
"orderId": "ORD2023032703", | |
"orderDate": "2023-03-27T18:40:22Z", | |
"customer": { | |
"name": "王五", | |
"email": "[email protected]" | |
}, | |
"items": [ | |
{ | |
"name": "空气净化器", | |
"price": 1599.00, | |
"quantity": 1 | |
} | |
], | |
"totalAmount": 1599.00, | |
"shippingAddress": { | |
"street": "解放路666号", | |
"city": "广州市", | |
"province": "广东省", | |
"postalCode": "510000" | |
}, | |
"paymentMethod": "微信支付" | |
} | |
]) | |
}], | |
}) | |
}) | |
const respJson = await resp.json() | |
console.log(respJson.content[0].text) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
下面是 haiku 生成的效果,不仅仅速度快,而且效果好