Skip to content

OpenAI Style Structured JSON outputsΒ #8

@lukehinds

Description

@lukehinds

The industry seems to be converging upon the OpenAI response format

We should implement the same approach.

Chat completions

{
  "id": "chatcmpl-1234567890",
  "object": "chat.completion",
  "created": 1697011234,
  "model": "gpt-4",
  "usage": {
    "prompt_tokens": 56,
    "completion_tokens": 72,
    "total_tokens": 128
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "To prevent security vulnerabilities in your software, follow best practices such as using secure coding techniques, regularly updating dependencies, performing security audits, and implementing input validation and proper authentication mechanisms."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}
package main

import (
    "encoding/json"
    "fmt"
)

// Define the structure for parsing the JSON response
type Usage struct {
    PromptTokens     int `json:"prompt_tokens"`
    CompletionTokens int `json:"completion_tokens"`
    TotalTokens      int `json:"total_tokens"`
}

type Message struct {
    Role    string `json:"role"`
    Content string `json:"content"`
}

type Choice struct {
    Message     Message `json:"message"`
    FinishReason string  `json:"finish_reason"`
    Index       int     `json:"index"`
}

type Response struct {
    ID      string   `json:"id"`
    Object  string   `json:"object"`
    Created int      `json:"created"`
    Model   string   `json:"model"`
    Usage   Usage    `json:"usage"`
    Choices []Choice `json:"choices"`
}

func main() {
    // Example JSON response (this would come from the LLM API)
    jsonResponse := `{
      "id": "chatcmpl-1234567890",
      "object": "chat.completion",
      "created": 1697011234,
      "model": "gpt-4",
      "usage": {
        "prompt_tokens": 56,
        "completion_tokens": 72,
        "total_tokens": 128
      },
      "choices": [
        {
          "message": {
            "role": "assistant",
            "content": "To prevent security vulnerabilities in your software, follow best practices such as using secure coding techniques, regularly updating dependencies, performing security audits, and implementing input validation and proper authentication mechanisms."
          },
          "finish_reason": "stop",
          "index": 0
        }
      ]
    }`

    // Parse the JSON response
    var response Response
    err := json.Unmarshal([]byte(jsonResponse), &response)
    if err != nil {
        fmt.Println("Error parsing JSON:", err)
        return
    }

    // Output the parsed information
    fmt.Printf("Response ID: %s\n", response.ID)
    fmt.Printf("Model: %s\n", response.Model)
    fmt.Printf("Assistant's Response: %s\n", response.Choices[0].Message.Content)
    fmt.Printf("Total Tokens Used: %d\n", response.Usage.TotalTokens)
}

Text Completions

For one-shot text completions, the text field in the choices array holds the response generated by the model. This is a single block of text, unlike the more structured role-based messages in the Chat Completions API.

{
  "id": "cmpl-1234567890",
  "object": "text_completion",
  "created": 1697011234,
  "model": "gpt-4",
  "choices": [
    {
      "text": "To prevent security vulnerabilities in your software, you should follow best practices such as:\n\n1. Use secure coding standards.\n2. Regularly update dependencies to patch known vulnerabilities.\n3. Perform regular security audits and code reviews.\n4. Implement input validation and proper authentication mechanisms.\n5. Use automated security tools to scan for vulnerabilities.\n",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 60,
    "total_tokens": 70
  }
}
package main

import (
    "encoding/json"
    "fmt"
)

// Define the structure for parsing the JSON response
type Usage struct {
    PromptTokens     int `json:"prompt_tokens"`
    CompletionTokens int `json:"completion_tokens"`
    TotalTokens      int `json:"total_tokens"`
}

type Choice struct {
    Text         string `json:"text"`
    Index        int    `json:"index"`
    Logprobs     interface{} `json:"logprobs"`
    FinishReason string `json:"finish_reason"`
}

type Response struct {
    ID      string   `json:"id"`
    Object  string   `json:"object"`
    Created int      `json:"created"`
    Model   string   `json:"model"`
    Choices []Choice `json:"choices"`
    Usage   Usage    `json:"usage"`
}

func main() {
    // Example JSON response from the Completions API
    jsonResponse := `{
      "id": "cmpl-1234567890",
      "object": "text_completion",
      "created": 1697011234,
      "model": "gpt-4",
      "choices": [
        {
          "text": "To prevent security vulnerabilities in your software, you should follow best practices such as:\n\n1. Use secure coding standards.\n2. Regularly update dependencies to patch known vulnerabilities.\n3. Perform regular security audits and code reviews.\n4. Implement input validation and proper authentication mechanisms.\n5. Use automated security tools to scan for vulnerabilities.\n",
          "index": 0,
          "logprobs": null,
          "finish_reason": "stop"
        }
      ],
      "usage": {
        "prompt_tokens": 10,
        "completion_tokens": 60,
        "total_tokens": 70
      }
    }`

    // Parse the JSON response
    var response Response
    err := json.Unmarshal([]byte(jsonResponse), &response)
    if err != nil {
        fmt.Println("Error parsing JSON:", err)
        return
    }

    // Output the parsed information
    fmt.Printf("Response ID: %s\n", response.ID)
    fmt.Printf("Model: %s\n", response.Model)
    fmt.Printf("Generated Text: %s\n", response.Choices[0].Text)
    fmt.Printf("Total Tokens Used: %d\n", response.Usage.TotalTokens)
}
Response ID: cmpl-1234567890
Model: gpt-4
Generated Text: To prevent security vulnerabilities in your software, you should follow best practices such as:

1. Use secure coding standards.
2. Regularly update dependencies to patch known vulnerabilities.
3. Perform regular security audits and code reviews.
4. Implement input validation and proper authentication mechanisms.
5. Use automated security tools to scan for vulnerabilities.
Total Tokens Used: 70

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions