triangle-utils 1.0.23 → 1.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,15 +1,23 @@
1
1
  import states from "./states.js"
2
2
 
3
+ import Utils_Misc from "./utils/Utils_Misc.js"
3
4
  import Utils_DynamoDB from "./utils/Utils_DynamoDB.js"
4
5
  import Utils_S3 from "./utils/Utils_S3.js"
5
- import Utils_Misc from "./utils/Utils_Misc.js"
6
+ import Utils_Bedrock from "./utils/Utils_Bedrock.js"
7
+ import Utils_Bee from "./utils/Utils_Bee.js"
6
8
 
7
9
  export default class Utils extends Utils_Misc {
8
10
  constructor(config) {
9
11
  super(config)
10
12
  this.config = config
11
13
  this.states = states
12
- this.dynamodb = new Utils_DynamoDB(config)
13
- this.s3 = new Utils_S3(config)
14
+ if (config.region !== undefined) {
15
+ this.dynamodb = new Utils_DynamoDB(config)
16
+ this.s3 = new Utils_S3(config)
17
+ this.bedrock = new Utils_Bedrock(config)
18
+ }
19
+ if (config.scraping_bee_api_key !== undefined) {
20
+ this.bee = new Utils_Bee(config)
21
+ }
14
22
  }
15
23
  }
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "main": "index.js",
5
5
  "author": "",
6
6
  "license": "ISC",
7
7
  "description": "",
8
8
  "type": "module",
9
9
  "dependencies": {
10
- "@aws-sdk/client-cognito-identity-provider": "^3.953.0",
10
+ "@aws-sdk/client-bedrock-runtime": "^3.953.0",
11
11
  "@aws-sdk/client-dynamodb": "^3.953.0",
12
12
  "@aws-sdk/client-s3": "^3.953.0",
13
- "nodemailer": "^7.0.11"
13
+ "nodemailer": "^7.0.11",
14
+ "scrapingbee": "^1.7.6"
14
15
  }
15
16
  }
@@ -0,0 +1,86 @@
1
+ import { BedrockRuntime } from "@aws-sdk/client-bedrock-runtime"
2
+
3
+ export default class Utils_Bedrock {
4
+
5
+ constructor(config) {
6
+ this.bedrock = new BedrockRuntime({ region: config.region })
7
+ this.text_decoder = new TextDecoder()
8
+ }
9
+
10
+ async llama_invoke(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
11
+ for (let i = 0; i < 3; i++) {
12
+ try {
13
+ const output = await this.bedrock.invokeModel({
14
+ modelId : model_id,
15
+ body : JSON.stringify({
16
+ prompt : prompt,
17
+ temperature : temperature,
18
+ top_p : top_p,
19
+ max_gen_len : max_gen_len
20
+ }),
21
+ contentType : "application/json"
22
+ })
23
+ const response = JSON.parse(this.text_decoder.decode(output.body))
24
+ return response.generation
25
+ } catch (e) {
26
+ console.log("Failed to get from Bedrock.")
27
+ return undefined
28
+ }
29
+ }
30
+ console.log("Failed to get from Bedrock, quitting.")
31
+ return undefined
32
+ }
33
+
34
+ async gpt_converse(model_id, prompt, temperature = 0.5, max_gen_len = 512, top_p = 0.9) {
35
+ for (let i = 0; i < 3; i++) {
36
+ try {
37
+ const output = await this.bedrock.converse({
38
+ modelId : model_id,
39
+ messages : [
40
+ { role : "user", content : [ { text : prompt } ] }
41
+ ],
42
+ inferenceConfig : {
43
+ temperature : temperature,
44
+ topP : top_p,
45
+ maxTokens : max_gen_len
46
+ }
47
+ })
48
+ const response = output.output
49
+ for (const c of response.message.content) {
50
+ if (c.text !== undefined) {
51
+ return c.text
52
+ }
53
+ }
54
+ } catch (e) {
55
+ console.log("Failed to get from Bedrock.")
56
+ console.log(e.stack)
57
+ return undefined
58
+ }
59
+ }
60
+ console.log("Failed to get from Bedrock, quitting.")
61
+ return undefined
62
+ }
63
+
64
+ async titan_invoke(text) {
65
+ for (let i = 0; i < 3; i++) {
66
+ try {
67
+ const output = await this.bedrock.invokeModel({
68
+ modelId : "amazon.titan-embed-text-v2:0",
69
+ body : JSON.stringify({
70
+ inputText: text
71
+ }),
72
+ contentType : "application/json"
73
+ })
74
+ const response = JSON.parse(this.text_decoder.decode(output.body))
75
+ return response.embedding
76
+ } catch (e) {
77
+ console.log("Failed to get from Bedrock.")
78
+ console.log(e.stack)
79
+ return undefined
80
+ }
81
+ }
82
+ console.log("Failed to get from Bedrock, quitting.")
83
+ return undefined
84
+ }
85
+
86
+ }
@@ -0,0 +1,57 @@
1
+ import { ScrapingBeeClient } from "scrapingbee"
2
+
3
+ export default class Utils_Bee {
4
+
5
+ constructor(config) {
6
+ this.bee = new ScrapingBeeClient(config.scraping_bee_api_key)
7
+ this.text_decoder = new TextDecoder()
8
+ }
9
+
10
+ async get(url, params) {
11
+ for (let i = 0; i < 3; i++) {
12
+ try {
13
+ const response = await bee.get({
14
+ url : url,
15
+ params : params
16
+ }).catch(response => response)
17
+ if (response.status !== 200) {
18
+ console.log("Status", response.status)
19
+ return undefined
20
+ }
21
+ const text = text_decoder.decode(response.data)
22
+ return text
23
+ } catch (e) {
24
+ console.log(e.stack)
25
+ console.log("Failed to get from Scraping Bee.")
26
+ }
27
+ }
28
+ console.log("Failed to get from Scraping Bee, quitting.")
29
+ return undefined
30
+ }
31
+
32
+ async google_search(query, news = false) {
33
+ for (let i = 0; i < 3; i++) {
34
+ try {
35
+ const bee_url = "https://app.scrapingbee.com/api/v1/store/google?api_key=" + config.scraping_bee_api_key + "&search=" + encodeURI(query) + (news ? "&search_type=news" : "")
36
+ console.log(bee_url)
37
+ const response = await fetch(bee_url)
38
+ if (!response.ok) {
39
+ console.log("Failed to Google:", response.status)
40
+ continue
41
+ }
42
+ const data = await response.json()
43
+ const results = news ? data.news_results : data.organic_results
44
+
45
+ if (results === undefined) {
46
+ console.log("Failed to Google.")
47
+ continue
48
+ }
49
+ return results
50
+ } catch {
51
+ console.log("Failed to Google.")
52
+ }
53
+ }
54
+ console.log("Failed to Google thrice, quitting.")
55
+ return undefined
56
+ }
57
+ }
@@ -114,7 +114,7 @@ export default class Utils_Misc {
114
114
  }
115
115
 
116
116
  get_secret_hash(username) {
117
- const secret_hash = crypto.createHmac("sha256", this.config.secret_key)
117
+ const secret_hash = crypto.createHmac("sha256", this.config.cognito_client_secret)
118
118
  .update(username + this.config.cognito_client_id)
119
119
  .digest("base64")
120
120
  return secret_hash