mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
6ba7fc0317
* chore: create trait * test: add tests * chore: remove log * chore: disable log
31 lines
910 B
Rust
31 lines
910 B
Rust
use crate::text::TextCompletion;
|
|
use anyhow::Error;
|
|
use async_openai::config::OpenAIConfig;
|
|
use async_openai::types::{CreateCompletionRequest, CreateCompletionResponse};
|
|
use async_openai::Client;
|
|
use lib_infra::async_trait::async_trait;
|
|
|
|
pub struct OpenAITextCompletion {
|
|
client: Client<OpenAIConfig>,
|
|
}
|
|
|
|
impl OpenAITextCompletion {
|
|
pub fn new(api_key: &str) -> Self {
|
|
// https://docs.rs/async-openai/latest/async_openai/struct.Completions.html
|
|
let config = OpenAIConfig::new().with_api_key(api_key);
|
|
let client = Client::with_config(config);
|
|
Self { client }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl TextCompletion for OpenAITextCompletion {
|
|
type Input = CreateCompletionRequest;
|
|
type Output = CreateCompletionResponse;
|
|
|
|
async fn text_completion(&self, params: Self::Input) -> Result<Self::Output, Error> {
|
|
let response = self.client.completions().create(params).await?;
|
|
Ok(response)
|
|
}
|
|
}
|