GithubHelp home page GithubHelp logo

openai's Introduction

OpenAi

Elixir OpenAi client library for with full support for streaming or SSE (Server Side Events).

Installation

Not available in Hex, but the package can be installed from git by adding openai to your list of dependencies in mix.exs:

def deps do
  [
    {:openai, git: "[email protected]:marinac-dev/openai.git", branch: "master"},
  ]
end

Documentation can be generated with ExDoc by running mix docs.

Configuration

config :openai,
  api_key: System.get_env("OPENAI_API_KEY"),
  organization_key: System.get_env("OPENAI_ORGANIZATION_KEY")

If you want to use retry mechanism, you can configure it like this:

config :openai,
  api_key: System.get_env("OPENAI_API_KEY"),
  organization_key: System.get_env("OPENAI_ORGANIZATION_KEY"),
  retry_config: %{
    retries: 5,
    delay: 200
  }

Usage

Once configured in your config.ex file, you can use the client to call the OpenAi API instantly.

prompt = %{model: "gpt-3.5-turbo", messages: [%{role: "user", content: "Hello!"}], stream: true}
OpenAi.chat_completion(prompt)
{:ok, "Hello! How may I assist you today?"}

openai's People

Contributors

marinac-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

openai's Issues

Mint Transport Error returning as tuple of tuple

Encountered this recently

  defp parse_response({:error, %Mint.TransportError{reason: :timeout}} = error) do
    Logger.error("OpenAi request timed out with error: #{inspect(error)}")
    {:error, error}
  end

Should be

  defp parse_response({:error, %Mint.TransportError{reason: :timeout} = error}) do
  Logger.error("OpenAi request timed out with error: #{inspect(error)}")
  {:error, error}
end

Add response structure

I think it will make future changes to API more manageable and usage of responses more Elixir friendly.

Make callback function optional in streaming request

Currently, stream() function for POST requests has a required callback function that handles responses.
However, there are situations where users might need to handle responses themselves with custom implementation, and would prefer to omit the built-in callback function.
Therefore, I would like to make the callback function as a parameter.

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.