Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Kino OpenAI

livebook/template.livemd

Kino OpenAI

Mix.install([
  {:kino_openai, path: "/Users/rakshan/projects/elixir/smartcells/kino_openai"}
])
==> kino_openai
Compiling 1 file (.ex)
warning: clauses with the same name and arity (number of arguments) should be grouped together, "defp parse_value/2" was previously defined (lib/kino_openai/task_cell.ex:127)
  lib/kino_openai/task_cell.ex:143
:ok

OpenAI

Application.put_env(:openai, :api_key, System.fetch_env!("LB_" <> "OPEN_AI_API_KEY"))

Application.put_env(
  :openai,
  :organization_key,
  System.fetch_env!("LB_" <> "OPEN_AI_ORGANIZATION_ID")
)

text_input = Kino.Input.textarea("Prompt", default: "Yesterday, I was reading a book and")
form = Kino.Control.form([text: text_input], submit: "Run")
frame = Kino.Frame.new()

form
|> Kino.Control.stream()
|> Kino.listen(fn %{data: %{text: text}} ->
  Kino.Frame.render(frame, Kino.Markdown.new("Running..."))
  opts = [temperature: 0.7, max_tokens: 500, prompt: text]

  case OpenAI.completions("text-davinci-003", opts) do
    {:ok, result} ->
      t = result |> Map.get(:choices) |> Enum.at(0) |> Map.get("text")
      Kino.Frame.render(frame, Kino.Markdown.new(t))

    {:error, error} ->
      Kino.Frame.render(frame, Kino.Markdown.new(error |> Kernel.inspect()))
  end
end)

Kino.Layout.grid([form, frame], boxed: true, gap: 16)