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

最低0.47元/天 解锁文章
5488

被折叠的 条评论
为什么被折叠?



