Unleashing the Power of Language:

An Introduction to the OpenAI API

Completions API

            client = OpenAI::Client.new(
              access_token: Rails.application.credentials.openai_key
            )
            client.chat(
              parameters: {
                model: "gpt-3.5-turbo-0613",
                messages: [
                  {
                    role: "system",
                    content: "You are a professional SEO consultant"
                  },
                  {
                    role: "user",
                    content: "How can I rank my website on Google?",
                  },
                ],
              }
            )
            

            client = OpenAI::Client.new(
              access_token: Rails.application.credentials.openai_key
            )
            client.chat(
              parameters: {
                model: "gpt-3.5-turbo-0613",
                messages: [
                  {
                    role: 'system',
                    content: "You are a professional SEO consultant"
                  },
                  {
                    role: "user",
                    content: "How can I rank my website on Google?",
                  },
                ],
              }
            )
            

            client = OpenAI::Client.new(
              access_token: Rails.application.credentials.openai_key
            )
            client.chat(
              parameters: {
                model: "gpt-3.5-turbo-0613",
                messages: [
                  {
                    role: 'system',
                    content: "You are a professional SEO consultant"
                  },
                  {
                    role: "user",
                    content: "How can I rank my website on Google?",
                  },
                ],
              }
            )
            

            client = OpenAI::Client.new(
              access_token: Rails.application.credentials.openai_key
            )
            api_response = client.chat(
              parameters: {
                model: "gpt-3.5-turbo-0613",
                messages: [
                  {
                    role: 'system',
                    content: "You are a professional SEO consultant"
                  },
                  {
                    role: "user",
                    content: "How can I rank my website on Google?",
                  },
                ],
              }
            )
            

              api_response.dig("choices", 0, "message")

              # long response string
              "Here are some seo recommendations...."
            

"Can you summarize https://rubyonrails.org?"

😢

"I apologize, but I am an AI language model and I am not able to browse the internet or access specific websites. Therefore, I cannot provide a summary or description of the website rubyonrails.org"

Function Calling

Define a function call


          api_response = client.chat(
            parameters: {
              model: "gpt-3.5-turbo-0613",
              messages: [ ... ],
              functions: [{
                name: "get_webpage_content",
                description: "Gets the content of a webpage",
                parameters: {
                  type: :object,
                  properties: {
                    url: {
                      type: :string,
                      description: "The webpage's url",
                    },
                  },
                  required: ["url"],
                }
              }]
            }
          )
          

Example


            /**
             * Gets the content of a webpage
             */
            get_webpage_content({ url: "https://www.example.com" })
          

Pass function along with messages


          api_response = client.chat(
            parameters: {
              model: "gpt-3.5-turbo-0613",
              messages: [
                {
                  role: "user",
                  content: "Can you summarize rubyonrails.org?",
                },
              ],
              functions: [ ... ],
            }
          )
          

Crawl the webpage


            message = api_response.dig("choices", 0, "message")

            # "get_webpage_content"
            function_name = response.dig("function_call", "name")

            if function_name == "get_webpage_content"
              # { url: "https://rubyonrails.org" }
              kwargs = JSON.parse(message.dig("function_call", "arguments"))
              @result = WebsiteCrawler.crawl(**kwargs)
            end
          

Pass the crawl result back to the API


            client.chat(
              parameters: {
                model: "gpt-3.5-turbo-0613",
                functions: [ ... ]
                messages: [
                  {
                    role: "user",
                    content: "Can you summarize https://rubyonrails.org?",
                  },
                  {
                    role: "function",
                    name: "get_webpage_content",
                    content: @result
                  }
                ],
              }
            )
          

🥳

"The content of https://rubyonrails.org ist about the framework Ruby on Rails. It was ..."

Questions?