Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenAI Style Structured JSON outputs #8

Open
lukehinds opened this issue Oct 14, 2024 · 0 comments
Open

OpenAI Style Structured JSON outputs #8

lukehinds opened this issue Oct 14, 2024 · 0 comments

Comments

@lukehinds
Copy link
Contributor

lukehinds commented Oct 14, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant