PageSourceSearch

https://innovationlab.fetch.ai/resources/assets/js/692c40e2.d1e6b7c4.js

js fetch.ai collected 2026-09-24 09:40:01 UTC 7,252 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkinnovation_labs=self.webpackChunkinnovation_labs||[]).push([[2544],{28264:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>i,default:()=>p,frontMatter:()=>o,metadata:()=>r,toc:()=>u});const r=JSON.parse('{"id":"asione/build/structured-data","title":"Structured Data Extraction","description":"ASI:One can return responses that conform to a JSON schema you define. This example shows how to extract structured information\u2014an ordersummary\u2014directly from natural language using the responseformat parameter with a JSON schema.","source":"@site/versioned_docs/version-1.0.5/asione/build/structured-data.md","sourceDirName":"asione/build","slug":"/asione/build/structured-data","permalink":"/resources/docs/asione/build/structured-data","draft":false,"unlisted":false,"tags":[],"version":"1.0.5","frontMatter":{"id":"structured-data","title":"Structured Data Extraction","sidebar_label":"Structured Data"},"sidebar":"tutorialSidebar","previous":{"title":"Function Calling","permalink":"/resources/docs/asione/build/function-calling"},"next":{"title":"Image Generation","permalink":"/resources/docs/asione/build/image-generation"}}');var s=t(74848),a=t(28453);const o={id:"structured-data",title:"Structured Data Extraction",sidebar_label:"Structured Data"},i="Structured Data with ASI",c={},u=[{value:"Expected Output",id:"expected-output",level:2},{value:"Next Steps",id:"next-steps",level:3}];function d(e){const n={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",hr:"hr",li:"li",p:"p",pre:"pre",ul:"ul",...(0,a.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.header,{children:(0,s.jsxs)(n.h1,{id:"structured-data-with-asi",children:["Structured Data with ASI",":One"]})}),"\n",(0,s.jsxs)(n.p,{children:["ASI",":One"," can return responses that conform to a JSON schema you define. This example shows how to extract structured information\u2014an ",(0,s.jsx)(n.code,{children:"order_summary"}),"\u2014directly from natural language using the ",(0,s.jsx)(n.code,{children:"response_format"})," parameter with a JSON schema."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-python",children:'import os\nimport json\nimport requests\nfrom typing import List\nfrom openai import OpenAI\n\n\n# ---- Config from environment ----\nLLM_API_ENDPOINT = os.getenv("LLM_API_ENDPOINT", "https://api.asi1.ai/v1")\nLLM_API_KEY = os.getenv("LLM_API_KEY", "your-api-key")\nLLM_MODEL = os.getenv("LLM_MODEL", "asi1")\n\n# Initialize OpenAI client with ASI1 API\nclient = OpenAI(\n    api_key=LLM_API_KEY,\n    base_url=LLM_API_ENDPOINT\n)\n\nheaders = {\n    "Authorization": f"Bearer {LLM_API_KEY}",\n    "Content-Type": "application/json",\n}\n\n# ---- Target JSON schema (order summary) ----\nresponse_format = {\n    "type": "json_schema",\n    "json_schema": {\n        "name": "order_summary",\n        "strict": True,\n        "schema": {\n            "type": "object",\n            "additionalProperties": False,\n            "properties": {\n                "order_id": {"type": "string"},\n                "total": {"type": "number"},\n                "currency": {"type": "string"},\n                "items": {\n                    "type": "array",\n                    "minItems": 1,\n                    "items": {\n                        "type": "object",\n                        "additionalProperties": False,\n                        "properties": {\n                            "sku": {"type": "string"},\n                            "name": {"type": "string"},\n                            "qty": {"type": "integer", "minimum": 1},\n                            "unit_price": {"type": "number"}\n                        },\n                        "required": ["sku", "name", "qty", "unit_price"]\n                    }\n                }\n            },\n            "required": ["order_id", "total", "currency", "items"]\n        }\n    }\n}\n\nprompt = (\n    "Generate a realistic example order summary with 2\u20133 items. "\n    "Use USD as ISO currency code. Fill every required field. "\n    "The order id is 1234567890. "\n    "The total is 100. "\n    "The currency is USD. "\n    "The items are 100. "\n    "The unit price is 1."\n)\n\n# ---- Chat Completions payload ----\npayload = {\n    "model": LLM_MODEL,\n    "messages": [\n        {"role": "system", "content": "Return ONLY valid JSON matching the provided schema."},\n        {"role": "user", "content": prompt},\n    ],\n    "response_format": response_format,\n}\n\nresp = requests.post(\n    f"{LLM_API_ENDPOINT}/chat/completions",\n    headers=headers,\n    json=payload,\n    timeout=60,\n)\n\n# Raw response\nif not resp.ok:\n    print(resp.status_code, resp.text)\n    resp.raise_for_status()\n\ndata = resp.json()\n\n# Simple parse of final JSON content\ntry:\n    content = (\n        (data.get("choices") or [{}])[0]\n        .get("message", {})\n        .get("content", "")\n    )\n    parsed = json.loads(content) if content else None\nexcept Exception:\n    parsed = None\n\nprint("\\n=== Output ===")\nprint(json.dumps(parsed, indent=2) if parsed is not None else "None")\n'})}),"\n",(0,s.jsx)(n.h2,{id:"expected-output",children:"Expected Output"}),"\n",(0,s.jsxs)(n.p,{children:["The model returns a response that c
1onforms to the JSON schema you defined. The response is automatically structured as an ",(0,s.jsx)(n.code,{children:"order_summary"})," object:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-json",children:'=== Output ===\n{\n  "currency": "USD",\n  "items": [\n    {\n      "name": "Wireless Bluetooth Earbuds",\n      "qty": 40,\n      "sku": "WBE-001",\n      "unit_price": 1.0\n    },\n    {\n      "name": "USB-C Charging Cable (6ft)",\n      "qty": 35,\n      "sku": "UCC-002",\n      "unit_price": 1.0\n    },\n    {\n      "name": "Phone Stand - Adjustable",\n      "qty": 25,\n      "sku": "PSA-003",\n      "unit_price": 1.0\n    }\n  ],\n  "order_id": "1234567890",\n  "total": 100.0\n}\n'})}),"\n",(0,s.jsx)(n.p,{children:"You now have structured order data that conforms to your schema, ready for further processing such as saving to a database, generating invoices, triggering notifications, or integrating with payment systems."}),"\n",(0,s.jsx)(n.hr,{}),"\n",(0,s.jsx)(n.h3,{id:"next-steps",children:"Next Steps"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["Learn how to ",(0,s.jsx)(n.a,{href:"./function-calling#defining-functions-schema-cheatsheet",children:"define function schemas"})]}),"\n",(0,s.jsxs)(n.li,{children:["See the ",(0,s.jsx)(n.a,{href:"./chat-completion",children:"Chat 
1Completion"})," guide for more request/response patterns"]}),"\n"]})]})}function p(e={}){const{wrapper:n}={...(0,a.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}},28453:(e,n,t)=>{t.d(n,{R:()=>o,x:()=>i});var r=t(96540);const s={},a=r.createContext(s);function o(e){const n=r.useContext(a);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function i(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),r.createElement(a.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.