Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ gem "minitest", "~> 5.0"
gem "rubocop", "~> 1.72"

gem "webmock"

gem "multipart-post"
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
dify_client (0.1.1)
dify_client (0.1.2)
multipart-post (~> 2.3)

GEM
remote: https://rubygems.org/
Expand All @@ -18,6 +19,7 @@ GEM
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
minitest (5.25.4)
multipart-post (2.4.1)
parallel (1.26.3)
parser (3.3.7.1)
ast (~> 2.4.1)
Expand Down Expand Up @@ -57,6 +59,7 @@ PLATFORMS
DEPENDENCIES
dify_client!
minitest (~> 5.0)
multipart-post
rake (~> 13.0)
rubocop (~> 1.72)
webmock
Expand Down
2 changes: 1 addition & 1 deletion dify-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency "multipart-post", "~> 2.3"

# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
43 changes: 43 additions & 0 deletions lib/dify/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

require_relative "client/version"
require "net/https"
require 'net/http/post/multipart'

require "json"
require 'uri'

module Dify
module Client
Expand All @@ -29,6 +32,28 @@ def update_api_key(new_key)
@api_key = new_key
end

def upload(io_obj, user, filename="localfile", mine_type="text/plain")
uri = URI.parse("#{@base_url}/files/upload")

request = Net::HTTP::Post::Multipart.new(uri.path, {
'file' => UploadIO.new(io_obj, 'text/plain', filename),
'user' => user
})

request['Authorization'] = "Bearer #{@api_key}"
request['User-Agent'] = 'Ruby-Dify-Uploader'

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

http.request(request)
end

def upload_file(file_path, user, filename="localfile", mine_type="text/plain")
fileio = File.new(file_path)
upload(fileio, user, filename, mine_type)
end

private

def _send_request(method, endpoint, data = nil, params = nil, _stream = false)
Expand Down Expand Up @@ -66,6 +91,24 @@ def create_completion_message(inputs, query, response_mode, user)
end
end

class WorkflowClient < DifyClient
def run_workflow(inputs, user, response_mode = "blocking", trace_id = nil)
data = {
inputs: inputs,
user: user,
response_mode: response_mode
}

data[:trace_id] = trace_id if trace_id

_send_request("POST", "/workflows/run", data, nil, response_mode == "streaming")
end

def get_workflow(workflow_id)
_send_request("GET", "/workflows/run/#{workflow_id}", nil, nil)
end
end

class ChatClient < DifyClient
def create_chat_message(inputs, query, user, response_mode = "blocking", conversation_id = nil)
data = {
Expand Down
2 changes: 1 addition & 1 deletion lib/dify/client/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Dify
module Client
VERSION = "0.1.1"
VERSION = "0.1.2"
end
end