triangle-utils 1.0.24 → 1.0.26

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,14 +1,16 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
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-bedrock-runtime": "^3.953.0",
10
11
  "@aws-sdk/client-dynamodb": "^3.953.0",
11
12
  "@aws-sdk/client-s3": "^3.953.0",
12
- "nodemailer": "^7.0.11"
13
+ "nodemailer": "^7.0.11",
14
+ "scrapingbee": "^1.7.6"
13
15
  }
14
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,58 @@
1
+ import { ScrapingBeeClient } from "scrapingbee"
2
+
3
+ export default class Utils_Bee {
4
+
5
+ constructor(config) {
6
+ this.config = config
7
+ this.bee = new ScrapingBeeClient(config.scraping_bee_api_key)
8
+ this.text_decoder = new TextDecoder()
9
+ }
10
+
11
+ async get(url, params) {
12
+ for (let i = 0; i < 3; i++) {
13
+ try {
14
+ const response = await bee.get({
15
+ url : url,
16
+ params : params
17
+ }).catch(response => response)
18
+ if (response.status !== 200) {
19
+ console.log("Status", response.status)
20
+ return undefined
21
+ }
22
+ const text = text_decoder.decode(response.data)
23
+ return text
24
+ } catch (e) {
25
+ console.log(e.stack)
26
+ console.log("Failed to get from Scraping Bee.")
27
+ }
28
+ }
29
+ console.log("Failed to get from Scraping Bee, quitting.")
30
+ return undefined
31
+ }
32
+
33
+ async google_search(query, news = false) {
34
+ for (let i = 0; i < 3; i++) {
35
+ try {
36
+ const bee_url = "https://app.scrapingbee.com/api/v1/store/google?api_key=" + this.config.scraping_bee_api_key + "&search=" + encodeURI(query) + (news ? "&search_type=news" : "")
37
+ console.log(bee_url)
38
+ const response = await fetch(bee_url)
39
+ if (!response.ok) {
40
+ console.log("Failed to Google:", response.status)
41
+ continue
42
+ }
43
+ const data = await response.json()
44
+ const results = news ? data.news_results : data.organic_results
45
+
46
+ if (results === undefined) {
47
+ console.log("Failed to Google.")
48
+ continue
49
+ }
50
+ return results
51
+ } catch {
52
+ console.log("Failed to Google.")
53
+ }
54
+ }
55
+ console.log("Failed to Google thrice, quitting.")
56
+ return undefined
57
+ }
58
+ }