Skip to content

Commit

Permalink
Fix Prompt deserialization defaults. (#6)
Browse files Browse the repository at this point in the history
* Fix Prompt deserialization defaults. - Prompt now deserializes from an empty map or with fields missing,
* Remove caching from CI - Writing the cache to disk took for CI on a crate this small hurt performance.
  • Loading branch information
mdegans authored Oct 12, 2024
1 parent f82988b commit a2a1135
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
26 changes: 1 addition & 25 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,10 @@ jobs:
with:
components: llvm-tools-preview

- name: Cache cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Cache cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-index-
- name: Cache cargo build
uses: actions/cache@v2
with:
path: target
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-build-
- name: Build
run: cargo build --all-features --verbose

- name: Run tests
- name: Test
run: cargo test --all-features --verbose

# This should only happen on push to main. PRs should not upload coverage.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "misanthropic"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Michael de Gans <[email protected]>"]
description = "An async, ergonomic, client for Anthropic's Messages API"
Expand Down
17 changes: 16 additions & 1 deletion src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use message::Message;
/// [Anthropic Messages API]: <https://docs.anthropic.com/en/api/messages>
#[derive(Serialize, Deserialize)]
#[cfg_attr(any(feature = "partial_eq", test), derive(PartialEq))]
#[serde(default)]
pub struct Prompt<'a> {
/// [`Model`] to use for inference.
pub model: Model,
Expand All @@ -37,7 +38,7 @@ pub struct Prompt<'a> {
pub max_tokens: NonZeroU16,
/// Optional info about the request, for example, `user_id` to help
/// Anthropic detect and prevent abuse. Do not use PII here (email, phone).
#[serde(skip_serializing_if = "serde_json::Map::is_empty")]
#[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
pub metadata: serde_json::Map<String, serde_json::Value>,
/// Optional stop sequences. If the model generates any of these sequences,
/// the completion will stop with [`StopReason::StopSequence`].
Expand Down Expand Up @@ -794,6 +795,20 @@ mod tests {
.is_cached());
}

#[test]
fn test_serde() {
// Test default deserialization.
const JSON: &str = r#"{}"#;

let defaults = serde_json::from_str::<Prompt>(JSON).unwrap();

// Another round trip to ensure serialization works.
let json = serde_json::to_string(&defaults).unwrap();
let _ = serde_json::from_str::<Prompt>(&json).unwrap();

// TODO: impl Default and PartialEq when `cfg(test)`
}

#[test]
fn test_tools() {
// A tool can be added from a json object. This is fallible. It must
Expand Down

0 comments on commit a2a1135

Please sign in to comment.