Skip to content

Commit

Permalink
feat: support extractor for deepseek (#255)
Browse files Browse the repository at this point in the history
* feat: support extractor for deepseek

* fixed: cargo fmt

* make code tidy

* add DEEPSEEK_REASONER completion model
  • Loading branch information
lispking authored Jan 31, 2025
1 parent a8be263 commit 6dde05f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
36 changes: 36 additions & 0 deletions rig-core/examples/extractor_with_deepseek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use rig::providers::deepseek;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, JsonSchema, Serialize)]
/// A record representing a person
struct Person {
/// The person's first name, if provided (null otherwise)
pub first_name: Option<String>,
/// The person's last name, if provided (null otherwise)
pub last_name: Option<String>,
/// The person's job, if provided (null otherwise)
pub job: Option<String>,
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Create DeepSeek client
let deepseek_client = deepseek::Client::from_env();

// Create extractor
let data_extractor = deepseek_client
.extractor::<Person>(deepseek::DEEPSEEK_CHAT)
.build();

let person = data_extractor
.extract("Hello my name is John Doe! I am a software engineer.")
.await?;

println!(
"DeepSeek: {}",
serde_json::to_string_pretty(&person).unwrap()
);

Ok(())
}
12 changes: 12 additions & 0 deletions rig-core/src/providers/deepseek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
//! ```
use crate::{
completion::{CompletionModel, CompletionRequest, CompletionResponse},
extractor::ExtractorBuilder,
json_utils,
};
use reqwest::Client as HttpClient;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::json;

Expand Down Expand Up @@ -66,6 +68,14 @@ impl Client {
pub fn agent(&self, model_name: &str) -> crate::agent::AgentBuilder<DeepSeekCompletionModel> {
crate::agent::AgentBuilder::new(self.completion_model(model_name))
}

/// Create an extractor builder with the given completion model.
pub fn extractor<T: JsonSchema + for<'a> Deserialize<'a> + Serialize + Send + Sync>(
&self,
model: &str,
) -> ExtractorBuilder<T, DeepSeekCompletionModel> {
ExtractorBuilder::new(self.completion_model(model))
}
}

/// The response shape from the DeepSeek API
Expand Down Expand Up @@ -274,3 +284,5 @@ impl CompletionModel for DeepSeekCompletionModel {
// ================================================================
/// `deepseek-chat` completion model
pub const DEEPSEEK_CHAT: &str = "deepseek-chat";
/// `deepseek-reasoner` completion model
pub const DEEPSEEK_REASONER: &str = "deepseek-reasoner";

0 comments on commit 6dde05f

Please sign in to comment.