Sha256: 670a5e062c82b0db16101d81ca671de99b0064f1c7c49eccb277c5d103b66db1

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

require 'json'
require 'openai'

module GeekDict
	module OpenAI

		#Youdao API version

		module_function

		def translate(word)
			@debugger = GeekDict.debugger
			client = ::OpenAI::Client.new(access_token: ENV.fetch('OPENAI_API_KEY'))
      response = client.chat(
        parameters: {
          model: "gpt-3.5-turbo",
          messages: [
            { role: "system", content: system_prompt(word)},
            { role: "user", content: word}
          ], 
          temperature: 0.2,
        })
      return response.dig("choices", 0, "message", "content")
		end

    def system_prompt(word)
      <<-EOS
      You are helpful and enthusiastic language translator. If the word or sentense is Chinese, translate it into English. If it is English, translate into Chinese. If the word is incorrectly spelled or it's not a correct word in English or Chinese, try your best to find the closest word. In addition to the translation, offer a comprehensive explanation of the word along with one or two examples to illustrate its usage. Do NOT add Pinyin in the example sentences for Chinese.
      EOS
    end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geekdict-0.1.1 lib/geekdict/openai/gpt.rb