|
| 1 | +// Copyright 2023 Datafuse Labs. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use common_exception::ErrorCode; |
| 16 | +use common_exception::Result; |
| 17 | +use log::trace; |
| 18 | +use openai_api_rust::chat::ChatApi; |
| 19 | +use openai_api_rust::chat::ChatBody; |
| 20 | +use openai_api_rust::Auth; |
| 21 | +use openai_api_rust::Message; |
| 22 | +use openai_api_rust::Role; |
| 23 | + |
| 24 | +use crate::metrics::metrics_completion_count; |
| 25 | +use crate::metrics::metrics_completion_token; |
| 26 | +use crate::AIModel; |
| 27 | +use crate::OpenAI; |
| 28 | + |
| 29 | +impl OpenAI { |
| 30 | + pub fn completion_text_request(&self, prompt: String) -> Result<(String, Option<u32>)> { |
| 31 | + let openai = openai_api_rust::OpenAI::new( |
| 32 | + Auth { |
| 33 | + api_key: self.api_key.clone(), |
| 34 | + organization: None, |
| 35 | + }, |
| 36 | + &self.api_base, |
| 37 | + ); |
| 38 | + |
| 39 | + let (max_tokens, stop) = (Some(1024), None); |
| 40 | + |
| 41 | + let body = ChatBody { |
| 42 | + model: AIModel::GPT35Turbo.to_string(), |
| 43 | + temperature: Some(0_f32), |
| 44 | + top_p: Some(1_f32), |
| 45 | + n: None, |
| 46 | + stream: None, |
| 47 | + stop, |
| 48 | + max_tokens, |
| 49 | + presence_penalty: None, |
| 50 | + frequency_penalty: None, |
| 51 | + logit_bias: None, |
| 52 | + user: None, |
| 53 | + messages: vec![Message { |
| 54 | + role: Role::User, |
| 55 | + content: prompt, |
| 56 | + }], |
| 57 | + }; |
| 58 | + |
| 59 | + trace!("openai text completion request: {:?}", body); |
| 60 | + |
| 61 | + let resp = openai.chat_completion_create(&body).map_err(|e| { |
| 62 | + ErrorCode::Internal(format!("openai completion text request error: {:?}", e)) |
| 63 | + })?; |
| 64 | + trace!("openai text completion response: {:?}", resp); |
| 65 | + |
| 66 | + let usage = resp.usage.total_tokens; |
| 67 | + let result = if resp.choices.is_empty() { |
| 68 | + "".to_string() |
| 69 | + } else { |
| 70 | + let message = resp |
| 71 | + .choices |
| 72 | + .get(0) |
| 73 | + .and_then(|choice| choice.message.as_ref()); |
| 74 | + |
| 75 | + match message { |
| 76 | + Some(msg) => msg.content.clone(), |
| 77 | + _ => "".to_string(), |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + // perf. |
| 82 | + { |
| 83 | + metrics_completion_count(1); |
| 84 | + metrics_completion_token(usage.unwrap_or(0)); |
| 85 | + } |
| 86 | + |
| 87 | + Ok((result, usage)) |
| 88 | + } |
| 89 | +} |
0 commit comments