fix: add missing code (#788)

This commit is contained in:
crazywoola 2023-08-09 19:36:39 +08:00 committed by GitHub
parent a510f32124
commit 36080fe352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 92 deletions

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "lib/dify_client/version" require_relative "lib/version"
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "dify_client" spec.name = "dify_client"

View File

@ -1,91 +0,0 @@
require 'net/http'
require 'json'
require 'uri'
class DifyClient
def initialize(api_key)
@api_key = api_key
@base_url = "https://api.dify.ai/v1"
end
def _send_request(method, endpoint, data = nil, params = nil, stream = false)
uri = URI.parse("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {
"Authorization" => "Bearer #{@api_key}",
"Content-Type" => "application/json"
}
if method == "GET"
uri.query = URI.encode_www_form(params) if params
request = Net::HTTP::Get.new(uri.request_uri, headers)
elsif method == "POST"
request = Net::HTTP::Post.new(uri.request_uri, headers)
request.body = data.to_json
end
response = http.request(request)
return response
end
def message_feedback(message_id, rating, user)
data = {
rating: rating,
user: user
}
return _send_request("POST", "/messages/#{message_id}/feedbacks", data)
end
def get_application_parameters(user)
params = {user: user}
return _send_request("GET", "/parameters", nil, params)
end
end
class CompletionClient < DifyClient
def create_completion_message(inputs, query, response_mode, user)
data = {
inputs: inputs,
query: query,
response_mode: response_mode,
user: user
}
return _send_request("POST", "/completion-messages", data, nil, response_mode == "streaming")
end
end
class ChatClient < DifyClient
def create_chat_message(inputs, query, user, response_mode = "blocking", conversation_id = nil)
data = {
inputs: inputs,
query: query,
user: user,
response_mode: response_mode
}
data[:conversation_id] = conversation_id if conversation_id
return _send_request("POST", "/chat-messages", data, nil, response_mode == "streaming")
end
def get_conversation_messages(user, conversation_id = nil, first_id = nil, limit = nil)
params = {user: user}
params[:conversation_id] = conversation_id if conversation_id
params[:first_id] = first_id if first_id
params[:limit] = limit if limit
return _send_request("GET", "/messages", nil, params)
end
def get_conversations(user, last_id = nil, limit = nil, pinned = nil)
params = {user: user, last_id: last_id, limit: limit, pinned: pinned}
return _send_request("GET", "/conversations", nil, params)
end
def rename_conversation(conversation_id, name, user)
data = {name: name, user: user}
return _send_request("POST", "/conversations/#{conversation_id}/name", data)
end
end