hunyuan 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e9e6434928006a16554b5149e1597f78758f01fd0b555d0ebf79e938aff98e96
4
+ data.tar.gz: 90e8a0d8fdb8ebf8422d3daebcfdfd825b6d9ec8ee801ba44819bc389577aa58
5
+ SHA512:
6
+ metadata.gz: a841d84acede4bd9a2585adea3ccb2eb23fbf3a9c5a8b03fa6817a2b03f9813e2e1f2ad7bf8ee86803742be5f8f5f1f7dff1d899add3acfb1719fa6cf731b87c
7
+ data.tar.gz: 17706fc9d8af020ddd0d623f852e50f3a0f8757824f79dc0b8af78e0f1c05a902f61973b204ff2cd79a9aefd80a688f86a513546bce27018783d203afe85cfbd
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-07
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kervinchangyu@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in hunyuan.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hunyuan (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.2)
12
+ parallel (1.22.1)
13
+ parser (3.1.2.1)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.6.0)
18
+ rexml (3.2.5)
19
+ rspec (3.12.0)
20
+ rspec-core (~> 3.12.0)
21
+ rspec-expectations (~> 3.12.0)
22
+ rspec-mocks (~> 3.12.0)
23
+ rspec-core (3.12.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-expectations (3.12.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-mocks (3.12.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.12.0)
31
+ rspec-support (3.12.0)
32
+ rubocop (1.38.0)
33
+ json (~> 2.3)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.1.2.1)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.23.0, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 3.0)
42
+ rubocop-ast (1.23.0)
43
+ parser (>= 3.1.1.0)
44
+ ruby-progressbar (1.11.0)
45
+ unicode-display_width (2.3.0)
46
+
47
+ PLATFORMS
48
+ universal-darwin-21
49
+
50
+ DEPENDENCIES
51
+ bundler
52
+ hunyuan!
53
+ rake (~> 13.0)
54
+ rspec (~> 3.0)
55
+ rubocop (~> 1.21)
56
+
57
+ BUNDLED WITH
58
+ 2.3.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kervin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Hunyuan
2
+
3
+ Welcome to Hunyuan! Hunyuan is a Ruby gem that provides a client for interacting with Tencent Hunyuan API for chat completions. With Hunyuan, you can easily integrate chat completion capabilities into your Ruby applications.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add hunyuan
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install hunyuan
16
+
17
+ ## Usage
18
+
19
+ To use Hunyuan, you'll need an API key from Hunyuan. You can obtain the required credentials by signing up for the Hunyuan API service.
20
+ ```ruby
21
+ require 'hunyuan'
22
+
23
+ # Initialize the ChatClient with your Hunyuan API credentials
24
+ client = Hunyuan::ChatClient.new(app_id: 'YOUR_APP_ID', secret_id: 'YOUR_SECRET_ID', secret_key: 'YOUR_SECRET_KEY')
25
+
26
+ # Send a chat query
27
+ response = client.chat('Hello, world!')
28
+
29
+ # Process the response
30
+ puts response
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (git checkout -b my-new-feature)
37
+ 3. Commit your changes (git commit -am 'Added some feature')
38
+ 4. Push to the branch (git push origin my-new-feature)
39
+ 5. Create new Pull Request
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hunyuan
4
+ VERSION = "0.1.0"
5
+ end
data/lib/hunyuan.rb ADDED
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "openssl"
5
+ require "base64"
6
+ require "securerandom"
7
+ require "json"
8
+ require_relative "hunyuan/version"
9
+
10
+ module Hunyuan
11
+
12
+ class ChatClient
13
+ HOST = "hunyuan.cloud.tencent.com"
14
+ PATH = "hyllm/v1/chat/completions"
15
+ URL = "https://#{HOST}/#{PATH}"
16
+
17
+ def initialize(app_id = ENV["HUNYUAN_APP_ID"],
18
+ secret_id = ENV["HUNYUAN_SECRET_ID"],
19
+ secret_key = ENV["HUNYUAN_SECRET_KEY"])
20
+ @app_id = app_id
21
+ @secret_id = secret_id
22
+ @secret_key = secret_key
23
+ end
24
+
25
+ def chat(messages)
26
+ request = build_request(messages)
27
+ signature = generate_signature(sign_params(request))
28
+
29
+ headers = {
30
+ "Content-Type" => "application/json",
31
+ "Authorization" => signature
32
+ }
33
+
34
+ puts "Request: #{URL} | #{headers} | #{request}"
35
+
36
+ response = Net::HTTP.post(URI(URL), request.to_json, headers)
37
+
38
+ puts "Response:"
39
+ result = JSON.parse(response.body)
40
+ puts result
41
+
42
+ result
43
+ end
44
+
45
+ private
46
+
47
+ def build_request(messages)
48
+ {
49
+ app_id: @app_id,
50
+ secret_id: @secret_id,
51
+ query_id: "test_query_id_#{SecureRandom.uuid}",
52
+ messages: messages,
53
+ temperature: 0,
54
+ top_p: 0.8,
55
+ stream: 0,
56
+ timestamp: Time.now.to_i + 10_000,
57
+ expired: Time.now.to_i + 24 * 60 * 60
58
+ }
59
+ end
60
+
61
+ def generate_signature(params)
62
+ query_string = params.sort.map { |k, v| "#{k}=#{v}" }.join("&")
63
+
64
+ string_to_sign = "#{HOST}/#{PATH}?#{query_string}"
65
+
66
+ digest = OpenSSL::Digest.new("sha1")
67
+ hmac = OpenSSL::HMAC.digest(digest, @secret_key, string_to_sign)
68
+
69
+ Base64.strict_encode64(hmac)
70
+ end
71
+
72
+ def sign_params(data)
73
+ {
74
+ app_id: data[:app_id],
75
+ secret_id: data[:secret_id],
76
+ query_id: data[:query_id],
77
+ temperature: data[:temperature].to_s,
78
+ top_p: data[:top_p].to_s,
79
+ stream: data[:stream],
80
+ messages: data[:messages].to_json,
81
+ timestamp: data[:timestamp].to_s,
82
+ expired: data[:expired].to_s
83
+ }
84
+ end
85
+ end
86
+
87
+ end
data/sig/hunyuan.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Hunyuan
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hunyuan
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kervin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Hunyuan is a Ruby gem designed to simplify the integration of the Hunyuan
42
+ API for chat completions into your Ruby applications. With Hunyuan, you can effortlessly
43
+ add natural language processing capabilities, enabling your applications to provide
44
+ intelligent responses to user queries. Whether you're building chatbots, virtual
45
+ assistants, or any other application that requires text-based interactions, Hunyuan
46
+ streamlines the process and empowers your Ruby code with advanced chat completion
47
+ features.
48
+ email:
49
+ - kervinchangyu@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - ".rspec"
55
+ - ".rubocop.yml"
56
+ - CHANGELOG.md
57
+ - CODE_OF_CONDUCT.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - lib/hunyuan.rb
64
+ - lib/hunyuan/version.rb
65
+ - sig/hunyuan.rbs
66
+ homepage: https://github.com/kervinchang/hunyuan
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.3.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Hunyuan - Ruby Gem for Chat Completions with Tencent Hunyuan API
89
+ test_files: []