OpenAI Chat API


OpenAI Chat API

  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:finch, "~> 0.16.0"}


Home Report An Issue APIsPokemon API

cURL Request

Make the following cURL request to the Open AI Chat API. This comes from the Open AI API Reference.

Make sure to replace $OPENAI_API_KEY with your Bearer token. After creating an OpenAI account, you can find or create an API key here:

curl \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]

Finch Request

Make a Finch request to create a chat completion. You can base the request on the cURL request above.

Decode the response using Jason and print the response string from the Chat API using IO.puts/2.

Example Solution

We’ve used the fake token sk-1WHDb0NwRkq3mfyRVparT3BlbkFJ500axJFd8pZ2RKxGJ0x to demonstrate how to replace $OPENAI_API_TOKEN with your bearer token.

request =
      {"Content-Type", "application/json"},
      {"Authorization", "Bearer sk-1WHDb0NwRkq3mfyRVparT3BlbkFJ500axJFd8pZ2RKxGJ0x"}
      model: "gpt-3.5-turbo",
      messages: [%{role: "user", content: "Hello!"}]
  |> Finch.request!(MyApp.Finch)

decoded_body = Jason.decode!(request.body)

[%{"message" => %{"content" => message}}] = decoded_body["choices"]


Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish OpenAI Chat API exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.


Home Report An Issue APIsPokemon API