forked from pljhonglu/ChatGPT-T
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt_access_token.rs
198 lines (179 loc) · 6.41 KB
/
gpt_access_token.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use eventsource_stream::{EventStreamError, Eventsource};
use futures::TryStreamExt;
use log::{error, info};
use reqwest;
use serde::{ser::Serializer, Deserialize, Serialize};
use serde_json::{json, Value};
use std::{env::consts::OS, time::Duration};
use tauri::{AppHandle, Manager};
use uuid::Uuid;
type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Request(#[from] reqwest::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Stream(#[from] EventStreamError<reqwest::Error>),
#[error("Custom Error: (code: {code:?}, message: {msg:?})")]
Custom { code: u16, msg: String },
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ProgressPayload {
pub id: u64,
pub detail: String,
pub role: String,
pub finish_reason: String,
pub conversation_id: Option<String>,
pub parent_message_id: String,
}
impl ProgressPayload {
pub fn emit_progress(&self, handle: &AppHandle) {
handle.emit_all("CHAT_FETCHEING_PROGRESS", &self).ok();
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Message {
pub role: String,
pub content: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[allow(non_snake_case)]
pub struct FetchOption {
pub proxy: Option<String>,
pub host: String,
pub apiKey: String,
pub model: String,
pub temperature: f32,
pub conversationId: Option<String>,
pub parentMessageId: Option<String>,
pub messageId: Option<String>,
pub action: Option<String>,
pub timeoutMs: Option<String>,
// pub onProgress?: (partialResponse: ChatMessage) => void
// pub abortSignal?: AbortSignal
}
#[tauri::command]
pub async fn fetch_chat_api_by_access_token(
handle: AppHandle,
id: u64,
messages: Vec<Message>,
option: FetchOption,
) -> Result<u64> {
// https://platform.openai.com/docs/guides/chat/introduction
// let url = "https://api.openai.com/v1/chat/completions";
log::info!(
"> send message: length: {}, option: {:?}",
messages.len(),
option,
);
let _messages_id = option.messageId.unwrap_or(Uuid::new_v4().to_string());
let _parent_message_id = option.parentMessageId.unwrap_or(Uuid::new_v4().to_string());
let conversation_id = option.conversationId;
let action = option.action.unwrap_or("next".to_string());
let last_message = messages.last().unwrap();
let mut body = json!({
"action": action,
"messages": [{
"id": _messages_id,
"role": "user".to_string(),
"content": {
"content_type": "text".to_string(),
"parts": [last_message.content]
}
}],
"model": option.model,
"parent_message_id": _parent_message_id,
});
info!("> conversation_id: {:?}", conversation_id);
if let Some(conversation_id) = conversation_id {
body["conversation_id"] = conversation_id.into();
}
log::info!("> send message: body {}", body);
let proxy_str = option.proxy.unwrap_or(String::from(""));
let client: reqwest::Client = {
log::info!("proxy is: {}", proxy_str);
let mut client_builder = reqwest::Client::builder();
if proxy_str.len() > 0 {
let proxy = reqwest::Proxy::all(proxy_str).unwrap();
client_builder = client_builder.proxy(proxy);
}
client_builder.build().unwrap()
};
info!("> body body: {}", body);
let res = client
.post(option.host)
.header("Accept", "text/event-stream")
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", option.apiKey))
.header(
reqwest::header::USER_AGENT,
format!("ChatGPT-Tauri ({})", OS),
)
.timeout(Duration::from_secs(600))
.body(body.to_string())
.send()
.await?;
info!("> receive message: {}", id);
let status_code = res.status().as_u16();
info!("> receive message status code: {}", status_code);
if status_code != 200 {
let error_msg = res.text().await?;
log::error!("{}", error_msg);
return Err(Error::Custom {
code: status_code,
msg: String::from(error_msg),
});
}
let mut stream = res.bytes_stream().eventsource();
while let Some(chunk) = stream.try_next().await? {
let chunk = chunk.data;
if chunk == "[DONE]" {
return Ok(id);
} else {
match serde_json::from_str::<Value>(&chunk) {
Ok(object) => {
// info!("> object: {:?}", object);
let _message = &object["message"];
let _conversation_id =
String::from(object["conversation_id"].as_str().unwrap_or("")); // 从 JSON 对象获取 conversationId
let content =
String::from(_message["content"]["parts"][0].as_str().unwrap_or(""));
let role = String::from(_message["author"]["role"].as_str().unwrap_or(""));
let finish_reason = String::from(
_message["metadata"]["finish_details"]["type"]
.as_str()
.unwrap_or(""),
);
let progress = ProgressPayload {
id,
detail: content,
role,
finish_reason,
conversation_id: Some(_conversation_id),
parent_message_id: _messages_id.clone(),
};
// info!("> progress: {:?}", progress);
progress.emit_progress(&handle);
}
Err(err) => {
// 处理 JSON 转换错误
info!("Failed to parse JSON object: {:?}", err); // 中途会打印一个时间戳,导致无法转换为 JSON
continue; // 跳过当前循环,继续下一个循环
}
}
}
}
Ok(id)
}