Coder Social home page Coder Social logo

24rr / rusty-anthropic Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 9 KB

An unofficial Rust SDK for interacting with the Anthropic API

Home Page: https://pleaseful.github.io/rusty-anthropic/

License: MIT License

Rust 100.00%
anthropic claude-3-sonnet claude-ai claude-sdk

rusty-anthropic's Introduction

Rusty Anthropic Client

A Rust client for interacting with the Anthropic API, featuring examples for messages and text completions. This client utilizes the dotenv crate to manage environment variables securely.

Table of Contents

Features

  • Supports the Messages and Text Completions endpoints of the Anthropic API
  • Loads API keys from environment variables using the dotenv crate
  • Handles errors gracefully and returns responses in JSON format

Installation

To use the Rusty Anthropics Client, follow these steps:

  1. Add the required dependencies to your Cargo.toml:
[dependencies]
rusty-anthropic = "0.1.0"
reqwest = { version = "0.12.5", features = ["json"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
dotenv = "0.15.0"
  1. Create a .env file in the root directory of your project to store your API key:
API_KEY=your_api_key_here

Configuration

The client uses the dotenv crate to load environment variables. Ensure that you have a .env file in the root directory of your project with the following content:

API_KEY=your_api_key_here

This file should be included in your .gitignore to prevent your API key from being committed to version control.

Usage

Below is an example showing how to use the Messages API and the Text Completions API.

Messages API Example

  1. Create a new file named main.rs and add the following content:
use rusty_anthropic::anthropic_api::client::AnthropicClient;
use rusty_anthropic::anthropic_api::messages::{MessagesApi, MessageRequest};
use rusty_anthropic::request_client::RequestClient;
use serde_json::json;
use dotenv::dotenv;
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Load environment variables from .env file
    dotenv().ok();

    // Retrieve the API key from the environment variable
    let api_key = env::var("API_KEY").expect("API_KEY environment variable not set");

    // Initialize the RequestClient with your API key
    let request_client = RequestClient::new(api_key);

    // Create an Anthropic client instance
    let base_url = "https://api.anthropic.com/v1";
    let anthropic_client = AnthropicClient::new(&request_client, base_url);

    // Create a Messages API instance
    let messages_api = MessagesApi::new(&anthropic_client);

    // Create a message request
    let model = "claude-3-5-sonnet-20240620".to_string();
    let messages = vec![json!({"role": "user", "content": "Hello, Claude"})];
    let request = MessageRequest::new(model, messages)
        .max_tokens(1024)
        .temperature(1.0);

    // Send the request and get the response
    let response_result = messages_api.create(request).await;

    // Handle and print the response as JSON
    match response_result {
        Ok(response) => {
            println!("{}", response.to_string());
        },
        Err(e) => {
            let error_response = json!({
                "error": e.to_string()
            });
            println!("{}", error_response.to_string());
        }
    }

    Ok(())
}

Documentation

Please visit this link for the documentation (WIP): here

License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

rusty-anthropic's People

Contributors

24rr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.