xfyun-spark 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +1 -1
- data/README.md +19 -3
- data/lib/xfyun/spark/client.rb +9 -5
- data/lib/xfyun/spark/request.rb +14 -7
- data/lib/xfyun/spark/version.rb +1 -1
- data/lib/xfyun/spark.rb +3 -1
- metadata +7 -8
- data/xfyun-spark.gemspec +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f24573cb26d274cbefe3bee747507c5bd550f41ddbb7b8624fed75ae416a01f
|
4
|
+
data.tar.gz: a5ed7ff6ce9f969d62dff486d972030cbbb376e3fb898c04cc122d9ae89de7dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27006a4e25e71673980feb8ac737c36c812718ca059b6dd7d1be0a1718f9b7ab08915832dc13ee57b29f9ecfe2ac6b301a5d9db24923696aae2cc3583f520623
|
7
|
+
data.tar.gz: f68550d6ba619f3c770d565cb266f1902c24ed547bd6987968f9e00a777b7c05568b725ce1bdb5cce4c4c4c6cf7e3158055caaa677415d2b36674219d39085e7
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## [0.1.3] - 2023-09-22
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- config add logger option
|
13
|
+
- chat function add stream option
|
14
|
+
|
15
|
+
## [0.1.2] - 2023-09-20
|
16
|
+
|
17
|
+
### Fixed
|
18
|
+
|
19
|
+
- raise exception when ws onerror
|
20
|
+
- fixed last message content missing
|
21
|
+
|
8
22
|
## [0.1.1] - 2023-09-19
|
9
23
|
|
10
24
|
### Changed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -29,6 +29,7 @@ Xfyun::Spark.configure do |config|
|
|
29
29
|
# config.model = 'V1.5'
|
30
30
|
# config.host = 'your host'
|
31
31
|
# config.request_timeout = 10
|
32
|
+
# config.logger = Logger.new($stdout)
|
32
33
|
end
|
33
34
|
```
|
34
35
|
|
@@ -68,10 +69,10 @@ Chat with custom header and parameter
|
|
68
69
|
|
69
70
|
```ruby
|
70
71
|
response_body = client.chat(
|
72
|
+
header: {
|
73
|
+
uid: "12345"
|
74
|
+
},
|
71
75
|
parameter: {
|
72
|
-
header: {
|
73
|
-
uid: "12345"
|
74
|
-
},
|
75
76
|
chat: {
|
76
77
|
temperature: 0.5,
|
77
78
|
max_tokens: 1024,
|
@@ -86,6 +87,21 @@ response_body = client.chat(
|
|
86
87
|
answer = response_body.dig('payload', 'choices', 'text', 0, 'content')
|
87
88
|
```
|
88
89
|
|
90
|
+
Chat with stream
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
client.chat(
|
94
|
+
payload: {
|
95
|
+
message: {
|
96
|
+
text: [{"role": "user", "content": "你是谁"}]
|
97
|
+
}
|
98
|
+
},
|
99
|
+
stream: proc do |chunk|
|
100
|
+
puts chunk
|
101
|
+
end
|
102
|
+
)
|
103
|
+
```
|
104
|
+
|
89
105
|
## Development
|
90
106
|
|
91
107
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/xfyun/spark/client.rb
CHANGED
@@ -3,7 +3,7 @@ module Xfyun
|
|
3
3
|
class Client
|
4
4
|
include Xfyun::Spark::Request
|
5
5
|
|
6
|
-
CONFIG_KEYS = %i[appid api_key api_secret model host request_timeout].freeze
|
6
|
+
CONFIG_KEYS = %i[appid api_key api_secret model host request_timeout logger].freeze
|
7
7
|
attr_reader *CONFIG_KEYS, :request_version, :request_domain
|
8
8
|
|
9
9
|
def initialize(config = {})
|
@@ -22,16 +22,16 @@ module Xfyun
|
|
22
22
|
}.fetch(@model)
|
23
23
|
end
|
24
24
|
|
25
|
-
def chat(header: {}, parameter: {}, payload: {})
|
25
|
+
def chat(header: {}, parameter: {}, payload: {}, stream: nil)
|
26
26
|
header = default_header.merge(header)
|
27
27
|
parameter = default_parameter.map do |k, v|
|
28
|
-
[k, v.merge(parameter
|
28
|
+
[k, v.merge(parameter[k] || {})]
|
29
29
|
end.to_h
|
30
30
|
request(path: "/chat", parameters: {
|
31
31
|
header: header,
|
32
32
|
parameter: parameter,
|
33
|
-
payload: payload
|
34
|
-
})
|
33
|
+
payload: payload,
|
34
|
+
}, stream: stream)
|
35
35
|
end
|
36
36
|
|
37
37
|
def default_header
|
@@ -46,6 +46,10 @@ module Xfyun
|
|
46
46
|
}
|
47
47
|
end
|
48
48
|
|
49
|
+
def logging(level, msg)
|
50
|
+
@logger.send(level, msg) if @logger != nil
|
51
|
+
end
|
52
|
+
|
49
53
|
end
|
50
54
|
end
|
51
55
|
end
|
data/lib/xfyun/spark/request.rb
CHANGED
@@ -9,32 +9,38 @@ module Xfyun
|
|
9
9
|
module Spark
|
10
10
|
module Request
|
11
11
|
|
12
|
-
def request(path:, parameters:)
|
12
|
+
def request(path:, parameters:, stream:)
|
13
13
|
content = ""
|
14
14
|
data = nil
|
15
15
|
EM.run {
|
16
16
|
url = authorization_url(path)
|
17
|
-
|
18
|
-
puts parameters
|
17
|
+
logging(:info, "request url: #{url}, parameters: #{parameters}")
|
19
18
|
ws = Faye::WebSocket::Client.new(url)
|
20
19
|
|
21
20
|
ws.on(:open) do |event|
|
22
|
-
p [:open, ws.headers]
|
21
|
+
# p [:open, ws.headers]
|
23
22
|
ws.send(parameters.to_json)
|
24
23
|
end
|
25
24
|
|
26
25
|
ws.on(:close) do |event|
|
27
|
-
|
26
|
+
logging(:info, "ws close: #{event.code} #{event.reason}")
|
27
|
+
# p [:close, event.code, event.reason]
|
28
28
|
EM.stop
|
29
29
|
end
|
30
30
|
|
31
31
|
ws.on(:error) do |event|
|
32
|
-
|
32
|
+
logging(:info, "ws error: #{event.message}")
|
33
|
+
# p [:error, event.message]
|
34
|
+
raise Xfyun::Spark::Error.new(event.message)
|
33
35
|
end
|
34
36
|
|
35
37
|
ws.on(:message) do |event|
|
36
|
-
p [:message, event.data]
|
38
|
+
# p [:message, event.data]
|
39
|
+
logging(:info, "ws message: #{event.data}")
|
37
40
|
response_data = JSON.parse(event.data)
|
41
|
+
if stream.respond_to?(:call)
|
42
|
+
stream.call(response_data)
|
43
|
+
end
|
38
44
|
code = response_data.dig('header', 'code')
|
39
45
|
if code == 0
|
40
46
|
status = response_data.dig('header', 'status')
|
@@ -43,6 +49,7 @@ module Xfyun
|
|
43
49
|
content << response_data.dig('payload', 'choices', 'text', 0, 'content')
|
44
50
|
when 2
|
45
51
|
# last message
|
52
|
+
content << response_data.dig('payload', 'choices', 'text', 0, 'content')
|
46
53
|
response_data['payload']['choices']['text'][0]['content'] = content
|
47
54
|
data = response_data
|
48
55
|
end
|
data/lib/xfyun/spark/version.rb
CHANGED
data/lib/xfyun/spark.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require_relative "spark/version"
|
2
2
|
require_relative "spark/request"
|
3
3
|
require_relative "spark/client"
|
4
|
+
require 'logger'
|
4
5
|
|
5
6
|
module Xfyun
|
6
7
|
module Spark
|
7
8
|
class Error < StandardError; end
|
8
9
|
|
9
10
|
class Configuration
|
10
|
-
attr_accessor :appid, :api_key, :api_secret, :model, :host, :request_timeout
|
11
|
+
attr_accessor :appid, :api_key, :api_secret, :model, :host, :request_timeout, :logger
|
11
12
|
|
12
13
|
DEFAULT_MODEL = "V1.5".freeze
|
13
14
|
DEFAULT_HOST = "spark-api.xf-yun.com".freeze
|
@@ -20,6 +21,7 @@ module Xfyun
|
|
20
21
|
@model = DEFAULT_MODEL
|
21
22
|
@host = DEFAULT_HOST
|
22
23
|
@request_timeout = DEFAULT_REQUEST_TIMEOUT
|
24
|
+
@logger = nil # Logger.new($stdout)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xfyun-spark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chaucerling
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
description:
|
27
|
+
description:
|
28
28
|
email:
|
29
29
|
- chaucerling@gmail.com
|
30
30
|
executables: []
|
@@ -44,14 +44,13 @@ files:
|
|
44
44
|
- lib/xfyun/spark/request.rb
|
45
45
|
- lib/xfyun/spark/version.rb
|
46
46
|
- sig/xfyun/spark.rbs
|
47
|
-
- xfyun-spark.gemspec
|
48
47
|
homepage: https://github.com/chaucerling/xfyun-spark
|
49
48
|
licenses:
|
50
49
|
- MIT
|
51
50
|
metadata:
|
52
51
|
homepage_uri: https://github.com/chaucerling/xfyun-spark
|
53
52
|
source_code_uri: https://github.com/chaucerling/xfyun-spark/blob/main/CHANGELOG.md
|
54
|
-
post_install_message:
|
53
|
+
post_install_message:
|
55
54
|
rdoc_options: []
|
56
55
|
require_paths:
|
57
56
|
- lib
|
@@ -66,8 +65,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
65
|
- !ruby/object:Gem::Version
|
67
66
|
version: '0'
|
68
67
|
requirements: []
|
69
|
-
rubygems_version: 3.3.
|
70
|
-
signing_key:
|
68
|
+
rubygems_version: 3.3.26
|
69
|
+
signing_key:
|
71
70
|
specification_version: 4
|
72
71
|
summary: xfyun spark ruby sdk
|
73
72
|
test_files: []
|
data/xfyun-spark.gemspec
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require_relative "lib/xfyun/spark/version"
|
2
|
-
|
3
|
-
Gem::Specification.new do |spec|
|
4
|
-
spec.name = "xfyun-spark"
|
5
|
-
spec.version = Xfyun::Spark::VERSION
|
6
|
-
spec.authors = ["chaucerling"]
|
7
|
-
spec.email = ["chaucerling@gmail.com"]
|
8
|
-
|
9
|
-
spec.summary = "xfyun spark ruby sdk"
|
10
|
-
spec.homepage = "https://github.com/chaucerling/xfyun-spark"
|
11
|
-
spec.license = "MIT"
|
12
|
-
spec.required_ruby_version = ">= 2.6.0"
|
13
|
-
|
14
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
-
spec.metadata["source_code_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
16
|
-
|
17
|
-
# Specify which files should be added to the gem when it is released.
|
18
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
-
spec.files = Dir.chdir(__dir__) do
|
20
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
21
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
22
|
-
end
|
23
|
-
end
|
24
|
-
spec.bindir = "exe"
|
25
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
26
|
-
spec.require_paths = ["lib"]
|
27
|
-
|
28
|
-
# Uncomment to register a new dependency of your gem
|
29
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
30
|
-
|
31
|
-
# For more information and examples about making a new gem, check out our
|
32
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
33
|
-
|
34
|
-
spec.add_dependency "faye-websocket"
|
35
|
-
end
|