Skip to content

Commit

Permalink
Merge pull request #52 from restlessronin/issue-50/gt-max-tokens
Browse files Browse the repository at this point in the history
respond gracefully to errors during streaming
  • Loading branch information
restlessronin authored Aug 21, 2023
2 parents 7e75be7 + c7bab37 commit 1066ed7
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ body:
id: lib-version
attributes:
label: Library version
placeholder: openai_ex v0.2.2
placeholder: openai_ex v0.2.3
validations:
required: true
5 changes: 5 additions & 0 deletions lib/openai_ex/http_sse.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule OpenaiEx.HttpSse do
@moduledoc false
require Logger

# based on
# https://gist.github.com/zachallaun/88aed2a0cef0aed6d68dcc7c12531649
Expand Down Expand Up @@ -34,6 +35,10 @@ defmodule OpenaiEx.HttpSse do
{[tokens], {next_acc, ref, task}}

{:done, ^ref} ->
if acc != "" do
Logger.warning(inspect(Jason.decode!(acc)))
end

{:halt, {acc, ref, task}}
end
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule OpenaiEx.MixProject do
use Mix.Project

@version "0.2.2"
@version "0.2.3"
@description "Community maintained OpenAI API Elixir client for Livebook"
@source_url "https://github.com/restlessronin/openai_ex"

Expand Down
4 changes: 2 additions & 2 deletions notebooks/completions.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```elixir
Mix.install([
{:openai_ex, "~> 0.2.2"},
{:openai_ex, "~> 0.2.3"},
{:kino, "~> 0.10.0"}
])

Expand Down Expand Up @@ -74,7 +74,7 @@ completion_stream = fn model, prompt, max_tokens, temperature, last_frame ->
end)

token_stream
|> Enum.reduce(fn out, acc ->
|> Enum.reduce("", fn out, acc ->
next = acc <> out
Kino.Frame.render(last_frame, Kino.Markdown.new("**Bot** #{next}"))
next
Expand Down
4 changes: 1 addition & 3 deletions notebooks/dlai_orderbot.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```elixir
Mix.install([
{:openai_ex, "~> 0.2.2"},
{:openai_ex, "~> 0.2.3"},
{:kino, "~> 0.10.0"}
])

Expand Down Expand Up @@ -166,5 +166,3 @@ Kino.listen(
end
)
```

<!-- livebook:{"offset":4847,"stamp":{"token":"QTEyOEdDTQ.pMrr-t5CE0Pvwb3eOKFSLos23e4QVe9ZcOp100QHWN8kYZv4wvWfadycV3Y.KOmZPk7CG2JEwnDl.1xc2Ioo-ttjzgKl2ljpAsO2WiFa3oYsdWjZLNZXT7gmMZ7sk9ia5Bztx08qZ95LkVg._8NPiAUhOdAN5OZdNSEszA","version":1}} -->
2 changes: 1 addition & 1 deletion notebooks/images.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```elixir
Mix.install([
{:openai_ex, "~> 0.2.2"},
{:openai_ex, "~> 0.2.3"},
{:kino, "~> 0.9.2"}
])

Expand Down
6 changes: 2 additions & 4 deletions notebooks/streaming_orderbot.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

```elixir
Mix.install([
{:openai_ex, "~> 0.2.2"},
{:openai_ex, "~> 0.2.3"},
{:kino, "~> 0.10.0"}
])

Expand All @@ -13,7 +13,7 @@ alias OpenaiEx.ChatMessage

## Setup

This notebook creates an Orderbot, similar to the one in [Deeplearning.AI Orderbot](https://hexdocs.pm/openai_ex/0.2.2/dlai_orderbot.html), but using the streaming version of the Chat Completion API.
This notebook creates an Orderbot, similar to the one in [Deeplearning.AI Orderbot](https://hexdocs.pm/openai_ex/0.2.3/dlai_orderbot.html), but using the streaming version of the Chat Completion API.

```elixir
openai = System.fetch_env!("LB_OPENAI_API_KEY") |> OpenaiEx.new()
Expand Down Expand Up @@ -121,5 +121,3 @@ context = [
```elixir
openai |> StreamingOrderbot.create_orderbot(context)
```

<!-- livebook:{"offset":3678,"stamp":{"token":"QTEyOEdDTQ.0jdhT2N5QwQcVLNGrRy4cztjpWvFpXapAuUyd7-C-kD_QeBTwdjAjqFYy_E.0JRwl7Qp_RXVC9kM.1jHtlyfPt5lTtSP5Wxo33cjVta8wkwmM0qTgPAhct0eE83mJzCCO1oZljKu8vwSOaQ.rKCLSYgYTnku_NzyqeNpow","version":1}} -->
10 changes: 4 additions & 6 deletions notebooks/userguide.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

```elixir
Mix.install([
# {:openai_ex, git: "https://github.com/restlessronin/openai_ex.git", tag: "v0.2.2"},
{:openai_ex, "~> 0.2.2"},
# {:openai_ex, git: "https://github.com/restlessronin/openai_ex.git", tag: "v0.2.3"},
{:openai_ex, "~> 0.2.3"},
# {:openai_ex, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.10.0"}
])
Expand Down Expand Up @@ -38,7 +38,7 @@ Add the following code to the first connection cell:
```elixir
Mix.install(
[
{:openai_ex, "~> 0.2.2"}
{:openai_ex, "~> 0.2.3"}
],
)
```
Expand All @@ -52,7 +52,7 @@ Add the following to your mix.exs file:
```elixir
def deps do
[
{:openai_ex, "~> 0.2.2"}
{:openai_ex, "~> 0.2.3"}
]
end
```
Expand Down Expand Up @@ -512,5 +512,3 @@ mod_res = openai |> Moderation.create(mod_req)
```

For more information on the moderation endpoints see the [Openai API Moderation Reference](https://platform.openai.com/docs/api-reference/moderations)

<!-- livebook:{"offset":15765,"stamp":{"token":"QTEyOEdDTQ.i8hJZvH1l-uetTiHLwSEBHdxTICxZc8W7c6szyPEaIjfiKtkaBezJgiUp_U.T73TwA2TrqgYC1Sa.9vRyRLz6lHsxNATaYEck6iDFkEnoZehRmzqWQHgIdma4IBK3bMH_nFT1jHCRBbIykA.FV5s7_pKPMji1GAG70Vdeg","version":1}} -->

0 comments on commit 1066ed7

Please sign in to comment.