Class: OpenAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/client.rb

Defined Under Namespace

Classes: Error, LastResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, default_engine: "davinci") ⇒ Client

Returns a new instance of Client.



17
18
19
20
# File 'lib/openai/client.rb', line 17

def initialize(api_key:, default_engine: "davinci")
  @api_key = api_key
  @default_engine = default_engine
end

Instance Attribute Details

#api_key=(value) ⇒ Object (writeonly)

Sets the attribute api_key

Parameters:

  • value

    the value to set the attribute api_key to.



14
15
16
# File 'lib/openai/client.rb', line 14

def api_key=(value)
  @api_key = value
end

#default_engineObject

Returns the value of attribute default_engine.



15
16
17
# File 'lib/openai/client.rb', line 15

def default_engine
  @default_engine
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



13
14
15
# File 'lib/openai/client.rb', line 13

def last_response
  @last_response
end

Instance Method Details

#completions(prompt: nil, max_tokens: nil, temperature: nil, top_p: nil, n: nil, logprobs: nil, echo: nil, stop: nil, presence_penalty: nil, frequency_penalty: nil, best_of: nil, engine: default_engine) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/openai/client.rb', line 34

def completions(prompt: nil, max_tokens: nil, temperature: nil, top_p: nil, n: nil, logprobs: nil, echo: nil, stop: nil, presence_penalty: nil, frequency_penalty: nil, best_of: nil, engine: default_engine)
  body = {
    "prompt"            => prompt,
    "max_tokens"        => max_tokens,
    "temperature"       => temperature,
    "top_p"             => top_p,
    "n"                 => n,
    "logprobs"          => logprobs,
    "echo"              => echo,
    "stop"              => stop,
    "presence_penalty"  => presence_penalty,
    "frequency_penalty" => frequency_penalty,
    "best_of"           => best_of,
  }.compact

  completion = post("/v1/engines/#{engine}/completions", body: body)

  choices = completion[:choices]&.map do |choice|
    Choice.new(
      finish_reason: choice[:finish_reason],
      index: choice[:index],
      text: choice[:text],
      logprobs: choice[:logprobs]&.tap do |logprob|
        Logprobs.new(
          text_offset: logprob[:text_offset],
          token_logprobs: logprob[:token_logprobs],
          tokens: logprob[:tokens],
          top_logprobs: logprob[:top_logprobs]
        )
      end
    )
  end

  Completion.new(
    choices: choices,
    created: completion[:created],
    id: completion[:id],
    model: completion[:model],
    object: completion[:object]
  )
end

#engine(id = default_engine) ⇒ Object



22
23
24
25
# File 'lib/openai/client.rb', line 22

def engine(id = default_engine)
  engine = get("/v1/engines/#{id}")
  Engine.new(**engine)
end

#enginesObject



27
28
29
30
31
32
# File 'lib/openai/client.rb', line 27

def engines
  engines = get("/v1/engines")
  engines[:data].map do |engine|
    Engine.new(**engine)
  end
end

#search(documents:, query:, engine: default_engine) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/openai/client.rb', line 76

def search(documents:, query:, engine: default_engine)
  body = {
    "documents" => documents,
    "query"     => query,
  }.compact

  search_results = post("/v1/engines/#{engine}/search", body: body)
  search_results[:data].map.with_index do |datum, index|
    SearchResult.new(**datum).tap do |result|
      result.text = documents[index]
    end
  end
end