open_ai.rs 910 B

123456789101112131415161718192021222324252627282930
  1. use crate::text::TextCompletion;
  2. use anyhow::Error;
  3. use async_openai::config::OpenAIConfig;
  4. use async_openai::types::{CreateCompletionRequest, CreateCompletionResponse};
  5. use async_openai::Client;
  6. use lib_infra::async_trait::async_trait;
  7. pub struct OpenAITextCompletion {
  8. client: Client<OpenAIConfig>,
  9. }
  10. impl OpenAITextCompletion {
  11. pub fn new(api_key: &str) -> Self {
  12. // https://docs.rs/async-openai/latest/async_openai/struct.Completions.html
  13. let config = OpenAIConfig::new().with_api_key(api_key);
  14. let client = Client::with_config(config);
  15. Self { client }
  16. }
  17. }
  18. #[async_trait]
  19. impl TextCompletion for OpenAITextCompletion {
  20. type Input = CreateCompletionRequest;
  21. type Output = CreateCompletionResponse;
  22. async fn text_completion(&self, params: Self::Input) -> Result<Self::Output, Error> {
  23. let response = self.client.completions().create(params).await?;
  24. Ok(response)
  25. }
  26. }