Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 31, 2024
1 parent beaaf2a commit 00229bb
Show file tree
Hide file tree
Showing 76 changed files with 2,082 additions and 451 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [sferik]
47 changes: 47 additions & 0 deletions .github/workflows/jekyll-gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Deploy Jekyll

on:
push:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: read
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Pages
uses: actions/configure-pages@v3
- name: Build with Jekyll
uses: actions/jekyll-build-pages@v1
with:
source: ./
destination: ./_site
- name: Upload artifact
uses: actions/upload-pages-artifact@v2

deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Linter
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- run: bundle exec rake lint
12 changes: 12 additions & 0 deletions .github/workflows/mutant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Mutant
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- run: bundle exec rake mutant
12 changes: 12 additions & 0 deletions .github/workflows/steep.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Type Checker
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "3.1"
bundler-cache: true
- run: bundle exec rake steep
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Tests
on: [push, pull_request]
jobs:
build:
strategy:
matrix:
ruby: ["3.1", "3.2", "3.3"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- run: bundle exec rake test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/spec/reports/
/tmp/
Gemfile.lock

# ASDF-VM / rbenv
.tool-versions
16 changes: 16 additions & 0 deletions .mutant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
coverage_criteria:
process_abort: true
fail_fast: true
includes:
- lib
integration:
name: minitest
matcher:
subjects:
- OpenAI*
mutation:
operators: full
timeout: 10.0
requires:
- openai
57 changes: 57 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require:
- rubocop-minitest
- rubocop-performance
- rubocop-rake
- standard
- standard-performance

AllCops:
NewCops: enable
TargetRubyVersion: 3.1

Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
IndentationWidth: 2

Layout/CaseIndentation:
EnforcedStyle: end

Layout/EndAlignment:
EnforcedStyleAlignWith: start_of_line

Layout/LineLength:
Max: 140

Layout/ParameterAlignment:
EnforcedStyle: with_fixed_indentation
IndentationWidth: 2

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Metrics/ParameterLists:
CountKeywordArgs: false

Minitest/MultipleAssertions:
Max: 5

Style/Alias:
EnforcedStyle: prefer_alias_method

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
EnforcedStyle: never

Style/OpenStructUse:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

Style/TernaryParentheses:
EnforcedStyle: require_parentheses
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## [0.4.0] - 2024-04-01
* Rewrite
14 changes: 14 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@ source "https://rubygems.org"

# Specify your gem's dependencies in openai.gemspec
gemspec

gem "minitest", ">= 5.19"
gem "mutant-license", source: "https://oss:b78oB5NQSVVIXx8WVdyC4sYf4qvTUqso@gem.mutant.dev"
gem "mutant-minitest", ">= 0.11.24"
gem "rake", ">= 13.0.6"
gem "rbs", ">= 3.2.1"
gem "rubocop", ">= 1.21"
gem "rubocop-minitest", ">= 0.31"
gem "rubocop-performance", ">= 1.18"
gem "rubocop-rake", ">= 0.6"
gem "simplecov", ">= 0.22"
gem "standard", ">= 1.30.1"
gem "steep", ">= 1.5.3"
gem "webmock", ">= 3.18.1"
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Nilesh Trivedi, Erik Berlin
Copyright (c) 2020-2024 Erik Berlin, Nilesh Trivedi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
109 changes: 83 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,113 @@
# OpenAI API client library to access GPT-3 in Ruby
[![Tests](https://github.com/sferik/openai-ruby/actions/workflows/test.yml/badge.svg)](https://github.com/sferik/openai-ruby/actions/workflows/test.yml)
[![Linter](https://github.com/sferik/openai-ruby/actions/workflows/lint.yml/badge.svg)](https://github.com/sferik/openai-ruby/actions/workflows/lint.yml)
[![Mutant](https://github.com/sferik/openai-ruby/actions/workflows/mutant.yml/badge.svg)](https://github.com/sferik/openai-ruby/actions/workflows/mutant.yml)
[![Typer Checker](https://github.com/sferik/openai-ruby/actions/workflows/steep.yml/badge.svg)](https://github.com/sferik/openai-ruby/actions/workflows/steep.yml)
[![Gem Version](https://badge.fury.io/rb/openai.svg)](https://rubygems.org/gems/openai)

This is a wrapper for calling OpenAI and GPT-3's HTTP APIs. API docs are available here: https://beta.openai.com/api-docs
# A [Ruby](https://www.ruby-lang.org) interface to the [OpenAI API](https://platform.openai.com)

## Installation
## Follow

Add this line to your application's Gemfile:
For updates and announcements, follow [@sferik](https://x.com/sferik) on X.

```ruby
gem 'openai'
```
## Installation

And then execute:
Install the gem and add to the application's Gemfile:

$ bundle
bundle add openai

Or install it yourself as:
Or, if Bundler is not being used to manage dependencies:

$ gem install openai
gem install openai

## Usage

First, obtain an API key from <https://platform.openai.com/api-keys>.

```ruby
require "openai"

openai_client = OpenAI::Client.new(api_key: ENV.fetch("OPENAI_API_KEY"), default_engine: "ada")
# Initialize an OpenAI API client with your API secret key
openai_client = OpenAI::Client.new(bearer_token: "sk-thats1smallStep4man1giantLeap4mankind")

# List Engines
openai_client.engines
# Retrieve a model instance
openai_client.get("models/gpt-4")
# {"id"=>"gpt-4", "object"=>"model", "created"=>1687882411, "owned_by"=>"openai"}
```

# Retrieve Engine
openai_client.engine("babbage")
## Features
* OAuth 2.0 Bearer Token
* Thread safety
* HTTP redirect following
* HTTP proxy support
* HTTP logging
* HTTP timeout configuration
* HTTP error handling
* Rate limit handling
* Parsing JSON into custom response objects (e.g. OpenStruct)
* Configurable base URLs for accessing different APIs/versions

# Search
openai_client.search(documents: ["White House", "hospital", "school"], query: "the president")
## Sponsorship

# Create Completion
openai_client.completions(prompt: "Once upon a time", max_tokens: 5)
```
By contributing to the project, you help:

## TODO
1. Maintain the library: Keeping it up-to-date and secure.
2. Add new features: Enhancements that make your life easier.
3. Provide support: Faster responses to issues and feature requests.

* Stream Completion
⭐️ Bonus: Sponsors will get priority support and influence over the project roadmap. We will also list your name or your company's logo on our GitHub page.

Building and maintaining an open-source project like this takes a considerable amount of time and effort. Your sponsorship can help sustain this project. Even a small monthly donation makes a huge difference!

[Click here to sponsor this project.](https://github.com/sponsors/sferik)

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
1. Checkout and repo:

git checkout git@github.com:sferik/openai-ruby.git

2. Enter the repo’s directory:

cd openai-ruby

3. Install dependencies via Bundler:

bin/setup

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
4. Run the default Rake task to ensure all tests pass:

bundle exec rake

5. Create a new branch for your feature or bug fix:

git checkout -b my-new-branch

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/nileshtrivedi/openai.
Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/openai-ruby.

Pull requests will only be accepted if they meet all the following criteria:

1. Code must conform to [Standard Ruby](https://github.com/standardrb/standard#readme). This can be verified with:

bundle exec rake standard

2. Code must conform to the [RuboCop rules](https://github.com/rubocop/rubocop#readme). This can be verified with:

bundle exec rake rubocop

3. 100% C0 code coverage. This can be verified with:

bundle exec rake test

4. 100% mutation coverage. This can be verified with:

bundle exec rake mutant

5. RBS type signatures (in `sig/openai.rbs`). This can be verified with:

bundle exec rake steep

## License

Expand Down
Loading

0 comments on commit 00229bb

Please sign in to comment.