View Source ExOpenAi.Parser (ex_open_ai v0.1.8)
Parses the response from the OpenAI API.
Link to this section Summary
Functions
Parse a response expected to contain a resource. If you pass in a
module as the first argument, the JSON will be parsed into that module's
__struct__
.
Link to this section Types
Link to this section Functions
Parse a response expected to contain a resource. If you pass in a
module as the first argument, the JSON will be parsed into that module's
__struct__
.
examples
Examples
Given you have a module named Resource
, defined like this:
defmodule Resource do
defstruct sid: nil
end
You can parse JSON into that module's struct like so:
iex> response = %{body: "{ \"id\": \"cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7\" }", status_code: 200}
...> ExOpenAi.Parser.parse(response, Resource, nil)
{:ok, %Resource{id: "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7"}}
You can also parse into a regular map if you want.
iex> response = %{body: "{ \"id\": \"cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7\" }", status_code: 200}
...> ExOpenAi.Parser.parse(response, %{}, nil)
{:ok, %{"id" => "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7"}}