openai_tools/chat/response.rs
1//! OpenAI Chat Completions API Response Types
2//!
3//! This module provides data structures for deserializing responses from the
4//! OpenAI Chat Completions API. It includes all the necessary types to handle
5//! complete API responses, including messages, choices, tool calls, log probabilities,
6//! and usage statistics.
7//!
8//! # Examples
9//!
10//! ```rust,no_run
11//! use serde_json;
12//! use openai_tools::chat::response::Response;
13//!
14//! // Deserialize a simple chat response
15//! let json = r#"{
16//! "id": "chatcmpl-123",
17//! "object": "chat.completion",
18//! "created": 1677652288,
19//! "model": "gpt-3.5-turbo-0613",
20//! "choices": [{
21//! "index": 0,
22//! "message": {
23//! "role": "assistant",
24//! "content": "Hello! How can I help you today?"
25//! },
26//! "finish_reason": "stop"
27//! }],
28//! "usage": {
29//! "prompt_tokens": 12,
30//! "completion_tokens": 8,
31//! "total_tokens": 20
32//! }
33//! }"#;
34//!
35//! let response: Response = serde_json::from_str(json).unwrap();
36//! let text = response.choices[0].message.content.as_ref().unwrap().text.clone().unwrap();
37//! assert_eq!(text, "Hello! How can I help you today?");
38//! ```
39//!
40//! # Tool Calls
41//!
42//! When the model makes function calls, the response will include tool calls:
43//!
44//! ```rust,no_run
45//! # use serde_json;
46//! # use openai_tools::chat::response::Response;
47//! let json_with_tools = r#"{
48//! "id": "chatcmpl-456",
49//! "object": "chat.completion",
50//! "created": 1677652288,
51//! "model": "gpt-3.5-turbo-0613",
52//! "choices": [{
53//! "index": 0,
54//! "message": {
55//! "role": "assistant",
56//! "content": null,
57//! "tool_calls": [{
58//! "id": "call_abc123",
59//! "type": "function",
60//! "function": {
61//! "name": "get_weather",
62//! "arguments": "{\"location\": \"Tokyo\"}"
63//! }
64//! }]
65//! },
66//! "finish_reason": "tool_calls"
67//! }],
68//! "usage": {
69//! "prompt_tokens": 25,
70//! "completion_tokens": 15,
71//! "total_tokens": 40
72//! }
73//! }"#;
74//!
75//! let response: Response = serde_json::from_str(json_with_tools).unwrap();
76//! let tool_calls = response.choices[0].message.tool_calls.as_ref().unwrap();
77//! assert_eq!(tool_calls[0].function.name, "get_weather");
78//! ```
79
80use crate::common::{message::Message, usage::Usage};
81use core::str;
82use serde::{Deserialize, Serialize};
83
84/// Top log probability item for a token
85///
86/// Contains information about a high-probability token alternative.
87#[derive(Debug, Clone, Deserialize, Serialize)]
88pub struct TopLogProbItem {
89 /// The token string
90 pub token: String,
91 /// The log probability of this token
92 pub logprob: f32,
93}
94
95/// Log probability item for a token
96///
97/// Contains detailed probability information for a single token,
98/// including alternatives.
99#[derive(Debug, Clone, Deserialize, Serialize)]
100pub struct LogProbItem {
101 /// The token string
102 pub token: String,
103 /// The log probability of this token
104 pub logprob: f32,
105 /// Optional list of top alternative tokens with their probabilities
106 pub top_logprobs: Option<Vec<TopLogProbItem>>,
107}
108
109/// Log probabilities container
110///
111/// Contains log probability information for all tokens in the response.
112#[derive(Debug, Clone, Deserialize, Serialize)]
113pub struct LogProbs {
114 /// List of log probability items for each token
115 pub content: Vec<LogProbItem>,
116}
117
118/// Choice structure representing a single response option
119///
120/// Each choice represents one possible completion generated by the model.
121/// When `n > 1` is specified in the request, multiple choices may be returned.
122#[derive(Debug, Clone, Deserialize, Serialize)]
123pub struct Choice {
124 /// The index of this choice in the list of choices
125 pub index: u32,
126 /// The message content of this choice
127 pub message: Message,
128 /// Optional log probability information for this choice
129 pub logprobs: Option<LogProbs>,
130 /// The reason why the generation finished (e.g., "stop", "length")
131 pub finish_reason: String,
132 pub refusal: Option<String>,
133 pub annotations: Option<Vec<String>>,
134}
135
136/// Complete response structure from OpenAI Chat Completions API
137///
138/// This structure contains all information returned by the OpenAI API,
139/// including generated choices, usage statistics, and metadata.
140#[derive(Debug, Clone, Deserialize, Serialize)]
141pub struct Response {
142 /// Unique identifier for the chat completion
143 pub id: String,
144 /// Object type, typically "chat.completion"
145 pub object: String,
146 /// Unix timestamp of when the completion was created
147 pub created: u64,
148 /// The model used for the completion
149 pub model: String,
150 /// List of completion choices generated by the model
151 pub choices: Vec<Choice>,
152 /// Token usage statistics for the request
153 pub usage: Usage,
154 /// Optional service tier used for the request
155 pub service_tier: Option<String>,
156 /// Fingerprint representing the model configuration
157 pub system_fingerprint: Option<String>,
158}