You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 responsetypeUsagestruct {
PromptTokensint`json:"prompt_tokens"`CompletionTokensint`json:"completion_tokens"`TotalTokensint`json:"total_tokens"`
}
typeMessagestruct {
Rolestring`json:"role"`Contentstring`json:"content"`
}
typeChoicestruct {
MessageMessage`json:"message"`FinishReasonstring`json:"finish_reason"`Indexint`json:"index"`
}
typeResponsestruct {
IDstring`json:"id"`Objectstring`json:"object"`Createdint`json:"created"`Modelstring`json:"model"`UsageUsage`json:"usage"`Choices []Choice`json:"choices"`
}
funcmain() {
// 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 responsevarresponseResponseerr:=json.Unmarshal([]byte(jsonResponse), &response)
iferr!=nil {
fmt.Println("Error parsing JSON:", err)
return
}
// Output the parsed informationfmt.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 responsetypeUsagestruct {
PromptTokensint`json:"prompt_tokens"`CompletionTokensint`json:"completion_tokens"`TotalTokensint`json:"total_tokens"`
}
typeChoicestruct {
Textstring`json:"text"`Indexint`json:"index"`Logprobsinterface{} `json:"logprobs"`FinishReasonstring`json:"finish_reason"`
}
typeResponsestruct {
IDstring`json:"id"`Objectstring`json:"object"`Createdint`json:"created"`Modelstring`json:"model"`Choices []Choice`json:"choices"`UsageUsage`json:"usage"`
}
funcmain() {
// Example JSON response from the Completions APIjsonResponse:=`{ "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 responsevarresponseResponseerr:=json.Unmarshal([]byte(jsonResponse), &response)
iferr!=nil {
fmt.Println("Error parsing JSON:", err)
return
}
// Output the parsed informationfmt.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
The text was updated successfully, but these errors were encountered:
The industry seems to be converging upon the OpenAI response format
We should implement the same approach.
Chat completions
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.
The text was updated successfully, but these errors were encountered: