Module response

Source
Expand description

OpenAI Chat Completions API Response Types

This module provides data structures for deserializing responses from the OpenAI Chat Completions API. It includes all the necessary types to handle complete API responses, including messages, choices, tool calls, log probabilities, and usage statistics.

§Examples

use serde_json;
use openai_tools::chat::response::Response;

// Deserialize a simple chat response
let json = r#"{
    "id": "chatcmpl-123",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "gpt-3.5-turbo-0613",
    "choices": [{
        "index": 0,
        "message": {
            "role": "assistant",
            "content": "Hello! How can I help you today?"
        },
        "finish_reason": "stop"
    }],
    "usage": {
        "prompt_tokens": 12,
        "completion_tokens": 8,
        "total_tokens": 20
    }
}"#;

let response: Response = serde_json::from_str(json).unwrap();
let text = response.choices[0].message.content.as_ref().unwrap().text.clone().unwrap();
assert_eq!(text, "Hello! How can I help you today?");

§Tool Calls

When the model makes function calls, the response will include tool calls:

let json_with_tools = r#"{
    "id": "chatcmpl-456",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "gpt-3.5-turbo-0613",
    "choices": [{
        "index": 0,
        "message": {
            "role": "assistant",
            "content": null,
            "tool_calls": [{
                "id": "call_abc123",
                "type": "function",
                "function": {
                    "name": "get_weather",
                    "arguments": "{\"location\": \"Tokyo\"}"
                }
            }]
        },
        "finish_reason": "tool_calls"
    }],
    "usage": {
        "prompt_tokens": 25,
        "completion_tokens": 15,
        "total_tokens": 40
    }
}"#;

let response: Response = serde_json::from_str(json_with_tools).unwrap();
let tool_calls = response.choices[0].message.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls[0].function.name, "get_weather");

Structs§

Choice
Choice structure representing a single response option
LogProbItem
Log probability item for a token
LogProbs
Log probabilities container
Response
Complete response structure from OpenAI Chat Completions API
TopLogProbItem
Top log probability item for a token