openai_api_wrapper/chat.rs
1use crate::client::OpenAIRequest;
2use reqwest::Method;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Deserialize, Debug)]
7pub struct Usage {
8 pub prompt_tokens: i64,
9 pub completion_tokens: i64,
10 pub total_tokens: i64,
11}
12
13#[derive(Deserialize, Serialize, Debug, Copy, Clone)]
14pub enum Role {
15 #[serde(rename = "system")]
16 System,
17 #[serde(rename = "assistant")]
18 Assistant,
19 #[serde(rename = "user")]
20 User,
21}
22
23impl ToString for Role {
24 fn to_string(&self) -> String {
25 match self {
26 Role::System => "system".to_string(),
27 Role::Assistant => "assistant".to_string(),
28 Role::User => "user".to_string(),
29 }
30 }
31}
32
33#[derive(Deserialize, Serialize, Debug, Clone)]
34pub struct Message {
35 pub role: Role,
36 pub content: String,
37}
38
39impl ToString for Message {
40 fn to_string(&self) -> String {
41 format!("{}: {}", self.role.to_string(), self.content)
42 }
43}
44
45impl AsRef<str> for Message {
46 fn as_ref(&self) -> &str {
47 self.content.as_ref()
48 }
49}
50
51#[derive(Deserialize, Debug, Clone)]
52pub struct Choice {
53 pub index: i64,
54 pub message: Message,
55 pub finish_reason: String,
56}
57
58#[derive(Deserialize, Debug)]
59pub struct Chat {
60 pub id: String,
61 pub object: String,
62 pub created: i64,
63 pub choices: Vec<Choice>,
64 pub usage: Usage,
65}
66
67#[derive(Serialize, Debug, Default)]
68pub struct ChatRequest {
69 // ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
70 pub model: String,
71 // The messages to generate chat completions for, in the chat format.
72 pub messages: Vec<Message>,
73 // What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. We generally recommend altering this or top_p but not both.
74 #[serde(skip_serializing_if = "Option::is_none")]
75 pub temperature: Option<f32>,
76 // An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.
77 #[serde(skip_serializing_if = "Option::is_none")]
78 pub top_p: Option<f32>,
79 // How many chat completion choices to generate for each input message.
80 #[serde(skip_serializing_if = "Option::is_none")]
81 pub n: Option<i32>,
82 // If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. See the OpenAI Cookbook for example code.
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub stream: Option<bool>,
85 // Up to 4 sequences where the API will stop generating further tokens.
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub stop: Option<Vec<String>>,
88 // The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length.
89 #[serde(skip_serializing_if = "Option::is_none")]
90 pub max_tokens: Option<i32>,
91 // Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. See more information about frequency and presence penalties.
92 #[serde(skip_serializing_if = "Option::is_none")]
93 pub presence_penalty: Option<f32>,
94 // Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. See more information about frequency and presence penalties.
95 #[serde(skip_serializing_if = "Option::is_none")]
96 pub frequency_penalty: Option<f32>,
97 // Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
98 #[serde(skip_serializing_if = "Option::is_none")]
99 pub logit_bias: Option<HashMap<String, f32>>,
100 // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.
101 #[serde(skip_serializing_if = "Option::is_none")]
102 pub user: Option<String>,
103}
104
105impl OpenAIRequest for ChatRequest {
106 type Response = Chat;
107
108 fn method() -> Method {
109 Method::POST
110 }
111
112 fn url() -> &'static str {
113 "https://api.openai.com/v1/chat/completions"
114 }
115}