【提示工程】询问GPT返回Json结构数据


theme: orange

众所周知,我们可以通过构建的Prompt获取期望的内容,但是通常都是以自然语言返回的,假如我们想得到结构化的数据,比如Json,XML那么怎么办,这篇文章给你一个思路。

理所当然的想法

要实现询问大模型后返回结构化的数据,首先能想到的是可以通过在提示末尾添加以 JSON 格式提供您的响应来进行一些“提示工程”,从而获得字符串化的 JSON。问题是这些响应通常包括错误的尾随逗号或介绍性的文字,导致中断错误。

接下来我们通过食谱应用中来实验下,用户在输入框中输入菜名,然后点击“获取食谱”。当您点击此按钮时,我们将运行 getRecipe() 函数:

``typescript function getRecipe() { // Create prompt text with user input. Include data model schema description. const prompt =return a recipe for ${userInput}. Provide your response as a JSON object with the following schema: {"dish": ${userInput}, "ingredients": ["", "", ...], "instructions": ["", "", ... ]}`; openai.createChatCompletion({ model: "gpt-3.5-turbo", messages: [ { role: "system", "content": "You are a helpful recipe assistant." }, { role: "user", content: prompt } ], }) .then((completion) => { // Handle API response co

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值