与 Anyscale 和 Together 强制执行 JSON Schema
使用 Anyscale 和 Together AI 新引入的 JSON 模式,让 LLM 遵循您的 JSON 架构
大型语言模型(LLMs)擅长生成创意文本,但生产应用程序需要结构化输出以实现无缝集成。指示 LLMs 仅生成指定语法的输出可以帮助使其行为更可预测。JSON 是这里的首选格式 - 它足够灵活,并且被广泛用作标准数据交换格式。
几家 LLM 提供商提供了帮助强制执行 JSON 输出的功能:
OpenAI 有一个名为 JSON mode 的功能,确保输出是有效的 JSON 对象。
尽管这很好,但它并不能保证遵循您的自定义 JSON schema,只能保证输出是 JSON。
Anyscale 和 Together AI 更进一步 - 它们不仅强制输出为 JSON,还确保输出遵循任何给定的 JSON schema。
使用 Portkey,您可以轻松实验 Anyscale 和 Together AI 的模型,并探索其 JSON 模式的强大功能:
import Portkey from 'portkey-ai';
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY",
virtualKey: "ANYSCALE_VIRTUAL_KEY"// OR "TOGETHER_VIRTUAL_KEY"
})
asyn function main(){
const json_response = await portkey.chat.completions.create({
messages: [{role: "user",content: `Give me a recipe for making Ramen, in JSON format`}],
model: "mistralai/Mistral-7B-Instruct-v0.1",
response_format: {
type: "json_object",
schema: {
type: "object",
properties: {
title: { type: "string" },
description: { type: "string" },
steps: { type: "array" }
}
}
}
});
}
console.log(json_response.choices[0].message.content);
main()输出 JSON:
支持的模型
mistralai/Mistral-7B-Instruct-v0.1
Anyscale
✅
✅
mistralai/Mixtral-8x7B-Instruct-v0.1
Anyscale
✅
✅
mistralai/Mixtral-8x7B-Instruct-v0.1
Together AI
✅
✅
mistralai/Mistral-7B-Instruct-v0.1
Together AI
✅
✅
togethercomputer/CodeLlama-34b-Instruct
Together AI
✅
✅
gpt-4 和之前的版本
OpenAI / Azure OpenAI
✅
❌
gpt-3.5-turbo 和之前的版本
OpenAI / Azure OpenAI
✅
❌
Ollama 模型
✅
❌
创建嵌套 JSON 对象架构
这是一个示例,展示了如何创建嵌套的 JSON 架构并让 LLM 强制执行它:
将您的 Anyscale 或 Together AI 虚拟密钥添加到 Portkey vault,开始吧!
Last updated