13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RubyでGPT-3.5-turboを試した

Last updated at Posted at 2023-03-02

ChatGPTのAPIが公開されたそうな。
Rubyからも、簡単に呼ぶことができる。
これを利用してデスクトップ用のミニツールを書いた。

image.png

使ったライブラリ

コード

require 'glimmer-dsl-libui'
require 'ruby/openai'

class ChatGPT
  class Message < Struct.new(:role, :content)
    def role_color
      [role, color]
    end

    def content_color
      [content, color]
    end

    def color
      case role
      when 'System' then :gray
      when 'Human' then :green
      when 'CatGPT' then :brown
      else raise
      end
    end
  end
  attr_reader :history

  def initialize
    @client = OpenAI::Client.new(access_token: ENV['OPENAI_ACCESS_TOKEN'])
    @history = [Message.new('System', 'Act like a cat')]
    call('Hi gpt 3.5 turbo!')
  end

  def call(query_text)
    res = @client.chat(
      parameters: {
        model: 'gpt-3.5-turbo',
        messages: generate_message(query_text)
      }
    )
    str = res.dig('choices', 0, 'message', 'content')
    history << Message.new('Human', query_text)
    history << Message.new('CatGPT', str)
  end

  def generate_message(query_text)
    m = history.map do |m|
      case m.role
      when 'CatGPT'
        { 'role' => 'assistant', 'content' => m.content }
      when 'Human'
        { 'role' => 'user', 'content' => m.content }
      when 'System'
        { 'role' => 'system', 'content' => m.content }
      end
    end
    m.push({ 'role' => 'user', 'content' => query_text })
    p m
  end
end

class Chat
  include Glimmer
  attr_accessor :entry_text

  def initialize
    @chatgpt = ChatGPT.new
  end

  def launch
    window('CatGPT - Glimmer DSL LibUI', 400, 400) do
      vertical_box do
        table do
          text_color_column('role')
          text_color_column('content')
          cell_rows <=> [@chatgpt, :history,
                         { column_attributes: { 'role' => :role_color, 'content' => :content_color } }]
        end
        horizontal_box do
          stretchy false
          entry do
            text <=> [self, :entry_text]
          end

          button('GO') do
            stretchy false
            on_clicked do
              @chatgpt.call(entry_text)
            end
          end
        end
      end
    end.show
  end
end

Chat.new.launch

この記事は以上です。

13
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?