Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ksylvest/omniai-anthropic

An implementation of the OmniAI interface for Anthropic.
https://github.com/ksylvest/omniai-anthropic

anthropic claude omniai ruby

Last synced: 29 days ago
JSON representation

An implementation of the OmniAI interface for Anthropic.

Awesome Lists containing this project

README

        

# OmniAI::Anthropic

[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ksylvest/omniai-anthropic/blob/main/LICENSE)
[![RubyGems](https://img.shields.io/gem/v/omniai-anthropic)](https://rubygems.org/gems/omniai-anthropic)
[![GitHub](https://img.shields.io/badge/github-repo-blue.svg)](https://github.com/ksylvest/omniai-anthropic)
[![Yard](https://img.shields.io/badge/docs-site-blue.svg)](https://omniai-anthropic.ksylvest.com)
[![CircleCI](https://img.shields.io/circleci/build/github/ksylvest/omniai-anthropic)](https://circleci.com/gh/ksylvest/omniai-anthropic)

An Anthropic implementation of the [OmniAI](https://github.com/ksylvest/omniai) APIs.

## Installation

```sh
gem install omniai-anthropic
```

## Usage

### Client

A client is setup as follows if `ENV['ANTHROPIC_API_KEY']` exists:

```ruby
client = OmniAI::Anthropic::Client.new
```

A client may also be passed the following options:

- `api_key` (required - default is `ENV['ANTHROPIC_API_KEY']`)
- `host` (optional)

### Configuration

Global configuration is supported for the following options:

```ruby
OmniAI::Anthropic.configure do |config|
config.api_key = '...' # default: ENV['ANTHROPIC_API_KEY']
config.host = '...' # default: 'https://api.anthropic.com'
end
```

### Chat

A chat completion is generated by passing in prompts using any a variety of formats:

```ruby
completion = client.chat('Tell me a joke!')
completion.text # 'Why did the chicken cross the road? To get to the other side.'
```

```ruby
completion = client.chat do |prompt|
prompt.system('You are a helpful assistant.')
prompt.user('What is the capital of Canada?')
end
completion.text # 'The capital of Canada is Ottawa.'
```

#### Model

`model` takes an optional string (default is `claude-3-haiku-20240307`):

```ruby
completion = client.chat('Provide code for fibonacci', model: OmniAI::Anthropic::Chat::Model::CLAUDE_SONNET)
completion.text # 'def fibonacci(n)...end'
```

[Anthropic API Reference `model`](https://docs.anthropic.com/en/api/messages)

#### Temperature

`temperature` takes an optional float between `0.0` and `1.0` (defaults is `0.7`):

```ruby
completion = client.chat('Pick a number between 1 and 5', temperature: 1.0)
completion.text # '3'
```

[Anthropic API Reference `temperature`](https://docs.anthropic.com/en/api/messages)

#### Stream

`stream` takes an optional a proc to stream responses in real-time chunks instead of waiting for a complete response:

```ruby
stream = proc do |chunk|
print(chunk.text) # 'Better', 'three', 'hours', ...
end
client.chat('Be poetic.', stream:)
```

[Anthropic API Reference `stream`](https://docs.anthropic.com/en/api/messages)

#### Format

`format` takes an optional symbol (`:json`) and modifies requests to send additional system text requesting JSON:

```ruby
completion = client.chat(format: :json) do |prompt|
prompt.system(OmniAI::Chat::JSON_PROMPT)
prompt.user('What is the name of the drummer for the Beatles?')
JSON.parse(completion.text) # { "name": "Ringo" }
```

[Anthropic API Reference `control-output-format`](https://docs.anthropic.com/en/docs/control-output-format)

### Computers

```bash
sudo apt-get install convert # screenshots
sudo apt-get install scrot # screenshots
sudo apt-get install xdotool # mouse / keyboard
```

```ruby
computer = OmniAI::Anthropic::Computer.new

completion = client.chat(tools: [computer]) do |prompt|
prompt.user('Please signup for reddit')
end
```