页面简介
Send images in a System One request: in the state, inside a question's instructions, and as the criteria a question is judged against.
正文摘录
Dreamer-ready. Images and Box questions aren’t yet up to our standards to call ready for production, but we’re excited for dreamers and builders to play with them. Use them to build prototypes and try ideas. The request format, the answers, and the limits on this page can change between releases, and accuracy will change as we update the models. Tell us what you build, and what breaks, on Discord.To use images or Box questions, send the TypeSafe-Danger-Zone-Images: true header with each request that contains an image or a Box question. See Turning on images. An image can go anywhere text can go in a System One request: in the state, in a question’s instructions, and in its criteria. Every question type accepts images, and the Box question type returns where something is in an image. Turning on images Images and Box questions are off by default. To use them, send the TypeSafe-Danger-Zone-Images: true header with each request that contains an image or a Box question. The examples under Uploading images show how. The other examples on this page show only the request body. Sending images in a request Each image is listed once, in the request’s top-level assets map. Its key in that map is a reference string. Write the reference wherever the image belongs, as a whole string value or inside a longer string, and the model receives the image in its place. This request sends a photo of a found dog as the state, and puts the photo from a lost-dog poster directly into one question: { "model": "jev-latest", "state": { "photo": "a3f1c2e8-found" }, "assets": { "a3f1c2e8-found": {"type": "image", "format": "jpg", "data_base64": "/9j/4AAQSkZJRg..."}, "77b0d4a1-poster": {"type": "image", "format": "jpg", "data_base64": "/9j/4AAQSkZJRg..."} }, "questions": { "same_dog": { "type": "noul", "instructions": "Is the dog in `photo` the same dog as in this picture: 77b0d4a1-poster?" }, "has_collar": { "type": "noul", "instructions": "Is the dog in `photo` wearing a collar?" }, "coat": { "type": "choice", "instructions": "What is the coat of the dog in `photo` like?", "criteria": { "short": "Short and smooth.", "long": "Long or feathered.", "curly": "Curly or wiry." } } } } Questions can refer to an image in the state by name or path in backticks, the same way they refer to any other field (see Reference specific fields). An image that only one question uses goes directly in that question’s instructions or criteria, as the poster photo does above. Jev 1.13 and Jev 1.14 both accept images. Box questions need Jev 1.14. Asset fields assetsmap<string, Asset>The images in the request, keyed by reference string. Every reference must appear somewhere in the state, instructions, or criteria.Show Asset propertiestype"image"The kind of asset. Optional. If you send it, it must be "image".partstringIn a multipart request, the name of the file part that holds the image. See Uploading images.data_base64stringThe image file, base64-encoded. Required unless part is set. Use the standard base64 alphabet with = padding, no line breaks or other whitespace, and no data: prefix.format"png" | "jpg" | "jpeg"The image’s format, in lowercase. Required with data_base64. Optional with part. "jpeg" is another name for "jpg". Send each image one way, with part or data_base64. If an asset has both, the API uses part. References References are matched as plain text anywhere in the request’s strings, so a reference works mid-sentence as well as as a whole value. A short reference such as cat could also match inside category. Use references that can’t occur in your text by accident. A new UUID for each image in each request is the safest choice. Every asset must be referenced. A request with an asset that is not referenced anywhere returns 400. Whole values match first. A string that is exactly a reference becomes that image. In longer strings, references are replaced from left to right, and matches never overlap. Each use adds the image again. The file is uploaded once, but every place its reference appears adds another copy of the image to what the model reads, and every copy counts toward the expanded request limit. Object keys are not replaced. A Choice option name, a question ID, or any other object key stays text. To define a Choice option with a picture, put the reference in the option’s description, as in Criteria. Answers contain the reference, not the image, wherever they repeat your criteria, such as a Score’s legend. Keep keys short. Each reference can be at most 1,024 bytes of UTF-8, and all references in a request together at most 65,536 bytes. These limits count bytes, not characters. A reference can’t be empty or only whitespace. The Python and JavaScript SDKs create references for you. See Uploading images. Uploading images With the latest Python or JavaScript SDK, put an image object anywhere text can go. The SDK creates a reference for it and uploads the file. This example sends one doorbell photo as the state and asks two questions about it: PythonJavaScriptfrom pathlib import Path from typesafe_sdk import Choice, Image, Noul, TypeSafeClient with TypeSafeClient() as client: result = client.system_one( state={"door": Image(Path("door-camera.jpg"))}, questions={ "person_present": Noul(instructions="Is there a person in `door`?"), "carrying": Choice( instructions="What is the person in `door` carrying?", criteria={ "package": "A box or parcel.", "bag": "A bag of groceries or shopping.", "nothing": "Empty hands.", }, ), }, extra_headers={"TypeSafe-Danger-Zone-Images": "true"}, ) print(result.answers) To put an image inside a sentence, or to choose your own references, see the SDK guides. Over HTTP Without an SDK, send each image as a file part in a multipart/form-data request. Multipart is the better choice: it sends the file as it is, while base64 makes each image about a third larger, and that extra size counts toward the 32 MiB request body limit. Base64-encode images into the JSON only when you can’t send multipart. Multipart (recommended) Base64 in JSONSend the JSON body as the first part, named request, with content type application/json. Send each image as its own file part, and set that image’s part in assets to the file part’s name:curl https://api.typesafe.ai/v1/systemone \ -H "Authorization: Bearer $TYPESAFE_API_KEY" \ -H "TypeSafe-Danger-Zone-Images: true" \ -F 'request={ "model": "jev-latest", "state": {"door": "a3f1c2e8-door"}, "assets": {"a3f1c2e8-door": {"type": "image", "part": "door-photo"}}, "questions": { "person_present": {"type": "noul", "instructions": "Is there a person in `door`?"} } };type=application/json' \ -F 'door-photo=@door-camera.jpg;type=image/jpeg' The rules for multipart requests: The first part is named request and holds the JSON body. Part names are unique and not empty. request is reserved for the JSON body. Each image part has content type image/png, image/jpeg, image/jpg, or application/octet-stream, or no content type. Any other content type returns 400. Each file part belongs to exactly one asset. A part that no asset names, two assets that name the same part, and an asset whose part is missing all return 400. To use the same image in several places, write its reference in each of them.Base64-encode the file into data_base64 and set format, following the rules in Asset fields. Python’s base64.b64encode produces the right encoding. This Python example sends one photo and asks two questions about it:import base64 import os import uuid import requests def image_asset(path: str) -> tuple[str, dict]: """Return a new reference and the asset entry for one image file.""" fmt = "png" if path.lower().endswith(".png") else "jpg" with open(path, "rb") as f: data = base64.b64encode(f.read()).decode("ascii") return str(uuid.uuid4()), {"type": "image", "format": fmt, "data_base64": data} photo_key, photo = image_asset("door-camera.jpg") response = requests.post( "https://api.typesafe.ai/v1/systemone", headers={ "Authorization": f"Bearer {os.environ['TYPESAFE_API_KEY']}", "TypeSafe-Danger-Zone-Images": "true", }, json={ "model": "jev-latest", "state": {"door": photo_key}, "assets": {photo_key: photo}, "questions": { "person_present": { "type": "noul", "instructions": "Is there a person in `door`?", }, "carrying": { "type": "choice", "instructions": "What is the person in `door` carrying?", "criteria": { "package": "A box or parcel.", "bag": "A bag of groceries or shopping.", "nothing": "Empty hands.", }, }, }, }, ) print(response.json()["answers"]) Image placement State Images in the state are part of the state, the same as any text or JSON there. The model evaluates them together with the rest of the state, and every question in the request is evaluated against them. A state can mix images with text, and can hold several images, labeled or nested like any other state data. This state holds a short history of camera frames, oldest first: { "state": { "frames": [ {"seconds_ago": 2, "frame": "5c0e9a12-f1"}, {"seconds_ago": 1, "frame": "1d7be0c4-f2"}, {"seconds_ago": 0, "frame": "e48a6f30-f3"} ] } } A question can ask “Did the door in frames open during these frames?” or refer to one frame by path, such as `frames[2].frame`. Instructions An image in a question’s instructions is part of that question and is sent only with that question. Put the reference in the sentence where the question needs the image: { "model": "jev-latest", "state": {"door": "e48a6f30-door"}, "questions": { "is_badge_holder": { "type": "noul", "instructions": "Is the person in `door` the same person as in this badge photo: 9a21c7d3-badge?" } } } An image in instructions is sent to the model once for every question that includes it. When several questions use the same image, put it in the state once and refer to it by path from each question. That uses fewer tokens and less of the size limits. Criteria Choice options, Score levels, and Noul true and false definitions can each include images. This Choice classifies a hand gesture, with each option defined by