AppFlowy/frontend/rust-lib/flowy-ai/src/text/open_ai.rs
Nathan.fooo 6ba7fc0317
feat: openai and stabilityai integration (#3439)
* chore: create trait

* test: add tests

* chore: remove log

* chore: disable log
2023-09-21 12:41:52 +08:00

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)
}
}