https://github.com/marinac-dev/openai
Elixir OpenAi Library with streaming support
https://github.com/marinac-dev/openai
elixir openai openai-api streaming
Last synced: about 1 year ago
JSON representation
Elixir OpenAi Library with streaming support
- Host: GitHub
- URL: https://github.com/marinac-dev/openai
- Owner: marinac-dev
- License: mit
- Created: 2023-03-23T18:42:17.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-04-16T23:44:33.000Z (about 2 years ago)
- Last Synced: 2025-04-09T16:16:31.304Z (about 1 year ago)
- Topics: elixir, openai, openai-api, streaming
- Language: Elixir
- Homepage:
- Size: 140 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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`:
```elixir
def deps do
[
{:openai, git: "git@github.com:marinac-dev/openai.git", branch: "master"},
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) by running `mix docs`.
## Configuration
```elixir
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:
```elixir
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.
```elixir
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?"}
```