Class: OmniAI::OpenAI::Thread

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/openai/thread.rb,
lib/omniai/openai/thread/run.rb,
lib/omniai/openai/thread/runs.rb,
lib/omniai/openai/thread/text.rb,
lib/omniai/openai/thread/content.rb,
lib/omniai/openai/thread/message.rb,
lib/omniai/openai/thread/messages.rb,
lib/omniai/openai/thread/annotation.rb,
lib/omniai/openai/thread/attachment.rb

Overview

An OpenAI threads implementation.

Defined Under Namespace

Classes: Annotation, Attachment, Content, Message, Messages, Run, Runs, Text

Constant Summary collapse

HEADERS =
{ 'OpenAI-Beta': 'assistants=v2' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: Client.new, id: nil, metadata: {}, tool_resources: {}) ⇒ Thread

Returns a new instance of Thread.

Parameters:

  • client (OmniAI::OpenAI::Client) (defaults to: Client.new)

    optional

  • id (String) (defaults to: nil)
  • metadata (String) (defaults to: {})


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/omniai/openai/thread.rb', line 28

def initialize(
  client: Client.new,
  id: nil,
  metadata: {},
  tool_resources: {}
)
  @client = client
  @id = id
  @metadata = 
  @tool_resources = tool_resources
end

Instance Attribute Details

#deletedBoolean?

Returns:

  • (Boolean, nil)


23
24
25
# File 'lib/omniai/openai/thread.rb', line 23

def deleted
  @deleted
end

#idString?

Returns:

  • (String, nil)


11
12
13
# File 'lib/omniai/openai/thread.rb', line 11

def id
  @id
end

#metadataHash

Returns:

  • (Hash)


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

def 
  @metadata
end

#tool_resourcesHash

Returns:

  • (Hash)


19
20
21
# File 'lib/omniai/openai/thread.rb', line 19

def tool_resources
  @tool_resources
end

Class Method Details

.destroy!(id:, client: Client.new) ⇒ Hash

Parameters:

Returns:

  • (Hash)

Raises:

  • (HTTPError)


62
63
64
65
66
67
68
69
70
71
# File 'lib/omniai/openai/thread.rb', line 62

def self.destroy!(id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .headers(HEADERS)
    .delete("/#{OmniAI::OpenAI::Client::VERSION}/threads/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  response.parse
end

.find(id:, client: Client.new) ⇒ OmniAI::OpenAI::Thread

Parameters:

Returns:

Raises:

  • (HTTPError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/omniai/openai/thread.rb', line 48

def self.find(id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .headers(HEADERS)
    .get("/#{OmniAI::OpenAI::Client::VERSION}/threads/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
end

Instance Method Details

#destroy!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (OmniAI::Error)


88
89
90
91
92
93
94
# File 'lib/omniai/openai/thread.rb', line 88

def destroy!
  raise OmniAI::Error, 'cannot destroy a non-persisted thread' unless @id

  data = self.class.destroy!(id: @id, client: @client)
  @deleted = data['deleted']
  self
end

#inspectString

Returns:

  • (String)


41
42
43
# File 'lib/omniai/openai/thread.rb', line 41

def inspect
  "#<#{self.class.name} id=#{@id.inspect}>"
end

#messagesOmniAI::OpenAI::Thread::Messages



97
98
99
# File 'lib/omniai/openai/thread.rb', line 97

def messages
  Messages.new(client: @client, thread: self)
end

#runsOmniAI::OpenAI::Thread::Runs



102
103
104
# File 'lib/omniai/openai/thread.rb', line 102

def runs
  Runs.new(client: @client, thread: self)
end

#save!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (HTTPError)


75
76
77
78
79
80
81
82
83
84
# File 'lib/omniai/openai/thread.rb', line 75

def save!
  response = @client.connection
    .accept(:json)
    .headers(HEADERS)
    .post("/#{OmniAI::OpenAI::Client::VERSION}/threads#{"/#{@id}" if @id}", json: payload)
  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
  self
end