@tmlmobilidade/ai 20260504.1029.41
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/README.md +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/oci-generativeai.d.ts +19 -0
- package/dist/oci-generativeai.js +95 -0
- package/package.json +49 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './oci-generativeai.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './oci-generativeai.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a simple wrapper around the OCI Generative AI SDK to make it easier
|
|
3
|
+
* to use in our application. It currently only supports running a text prompt
|
|
4
|
+
* through the "openai.gpt-oss-120b" model and getting a response.
|
|
5
|
+
*/
|
|
6
|
+
export declare class OCIGenerativeAIProvider {
|
|
7
|
+
private readonly modelOCID;
|
|
8
|
+
private readonly ociClient;
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* Runs a prompt through the OCI Generative AI service and returns the response.
|
|
12
|
+
* This is using the "openai.gpt-oss-120b" model, which is a 120B parameter model
|
|
13
|
+
* based on the open source Falcon-40B-Instruct-v2 architecture,
|
|
14
|
+
* but with more parameters and trained on more data.
|
|
15
|
+
* @param prompt The prompt to run through the model.
|
|
16
|
+
* @returns The response from the model.
|
|
17
|
+
*/
|
|
18
|
+
run(prompt: string): Promise<string>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/* * */
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { NoRetryConfigurationDetails, Region, SimpleAuthenticationDetailsProvider } from 'oci-common';
|
|
4
|
+
import { GenerativeAiInferenceClient } from 'oci-generativeaiinference';
|
|
5
|
+
/**
|
|
6
|
+
* This is a simple wrapper around the OCI Generative AI SDK to make it easier
|
|
7
|
+
* to use in our application. It currently only supports running a text prompt
|
|
8
|
+
* through the "openai.gpt-oss-120b" model and getting a response.
|
|
9
|
+
*/
|
|
10
|
+
export class OCIGenerativeAIProvider {
|
|
11
|
+
//
|
|
12
|
+
modelOCID = 'ocid1.generativeaimodel.oc1.eu-frankfurt-1.amaaaaaask7dceyaz25iqdyw27r4kve6tu7mnbpviyllkgswzwkwstriguvq';
|
|
13
|
+
ociClient;
|
|
14
|
+
constructor() {
|
|
15
|
+
// Validate that all required environment variables are set
|
|
16
|
+
if (!process.env.OCI_FINGERPRINT)
|
|
17
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider');
|
|
18
|
+
if (!process.env.OCI_PRIVATE_KEY_PATH && !process.env.OCI_PRIVATE_KEY)
|
|
19
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider: either OCI_PRIVATE_KEY_PATH or OCI_PRIVATE_KEY must be set');
|
|
20
|
+
if (!process.env.OCI_REGION)
|
|
21
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider');
|
|
22
|
+
if (!process.env.OCI_TENANCY)
|
|
23
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider');
|
|
24
|
+
if (!process.env.OCI_USER)
|
|
25
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider');
|
|
26
|
+
if (!process.env.OCI_COMPARTMENT)
|
|
27
|
+
throw new Error('Missing required environment variables for OCI Generative AI Provider');
|
|
28
|
+
// Build the OCI client using the environment variables for authentication
|
|
29
|
+
this.ociClient = new GenerativeAiInferenceClient({
|
|
30
|
+
authenticationDetailsProvider: new SimpleAuthenticationDetailsProvider(process.env.OCI_TENANCY, process.env.OCI_USER, process.env.OCI_FINGERPRINT, process.env.OCI_PRIVATE_KEY_PATH ? readFileSync(process.env.OCI_PRIVATE_KEY_PATH, 'utf8') : process.env.OCI_PRIVATE_KEY, null, Region.fromRegionId(process.env.OCI_REGION)),
|
|
31
|
+
});
|
|
32
|
+
this.ociClient.endpoint = `https://inference.generativeai.${process.env.OCI_REGION}.oci.oraclecloud.com`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Runs a prompt through the OCI Generative AI service and returns the response.
|
|
36
|
+
* This is using the "openai.gpt-oss-120b" model, which is a 120B parameter model
|
|
37
|
+
* based on the open source Falcon-40B-Instruct-v2 architecture,
|
|
38
|
+
* but with more parameters and trained on more data.
|
|
39
|
+
* @param prompt The prompt to run through the model.
|
|
40
|
+
* @returns The response from the model.
|
|
41
|
+
*/
|
|
42
|
+
async run(prompt) {
|
|
43
|
+
//
|
|
44
|
+
//
|
|
45
|
+
// Build the chat request, in the most complicated way possible
|
|
46
|
+
// because the OCI SDK is a nightmare to work with.
|
|
47
|
+
// (This was written by AI, I swear.)
|
|
48
|
+
const chatRequest = {
|
|
49
|
+
chatDetails: {
|
|
50
|
+
chatRequest: {
|
|
51
|
+
apiFormat: 'GENERIC',
|
|
52
|
+
frequencyPenalty: 0,
|
|
53
|
+
maxTokens: 2048,
|
|
54
|
+
messages: [
|
|
55
|
+
{
|
|
56
|
+
content: [
|
|
57
|
+
{
|
|
58
|
+
// @ts-expect-error — OMG this has to be the most convoluted way to send
|
|
59
|
+
// a simple text message I've ever seen in my life.
|
|
60
|
+
text: prompt,
|
|
61
|
+
type: 'TEXT',
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
role: 'USER',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
presencePenalty: 0,
|
|
68
|
+
temperature: 1,
|
|
69
|
+
topK: 0,
|
|
70
|
+
topP: 1,
|
|
71
|
+
},
|
|
72
|
+
compartmentId: process.env.OCI_COMPARTMENT,
|
|
73
|
+
servingMode: {
|
|
74
|
+
modelId: this.modelOCID,
|
|
75
|
+
servingType: 'ON_DEMAND',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
retryConfiguration: NoRetryConfigurationDetails,
|
|
79
|
+
};
|
|
80
|
+
//
|
|
81
|
+
// Send the chat request and return the response.
|
|
82
|
+
// Again, this is the most convoluted way to get a simple text response from the model.
|
|
83
|
+
const chatResponse = await this.ociClient.chat(chatRequest);
|
|
84
|
+
if (!('chatResult' in chatResponse)) {
|
|
85
|
+
throw new Error('Invalid response from OCI Generative AI service: ' + JSON.stringify(chatResponse));
|
|
86
|
+
}
|
|
87
|
+
if (!('choices' in chatResponse.chatResult.chatResponse)) {
|
|
88
|
+
throw new Error('Invalid response from OCI Generative AI service: ' + JSON.stringify(chatResponse));
|
|
89
|
+
}
|
|
90
|
+
// @ts-expect-error — Yes, I know this is a nightmare to read,
|
|
91
|
+
// but this is the documented way to get the text response from the model.
|
|
92
|
+
return chatResponse.chatResult.chatResponse.choices[0].message.content[0].text;
|
|
93
|
+
//
|
|
94
|
+
}
|
|
95
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tmlmobilidade/ai",
|
|
3
|
+
"version": "20260504.1029.41",
|
|
4
|
+
"author": {
|
|
5
|
+
"email": "iso@tmlmobilidade.pt",
|
|
6
|
+
"name": "TML-ISO"
|
|
7
|
+
},
|
|
8
|
+
"license": "AGPL-3.0-or-later",
|
|
9
|
+
"homepage": "https://github.com/tmlmobilidade/go#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/tmlmobilidade/go/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/tmlmobilidade/go.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"public transit",
|
|
19
|
+
"tml",
|
|
20
|
+
"transportes metropolitanos de lisboa",
|
|
21
|
+
"services"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc && resolve-tspaths",
|
|
34
|
+
"lint": "eslint && tsc --noEmit",
|
|
35
|
+
"lint:fix": "eslint --fix",
|
|
36
|
+
"watch": "tsc-watch --onSuccess 'resolve-tspaths'"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"oci-common": "2.131.0",
|
|
40
|
+
"oci-generativeaiinference": "2.131.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@tmlmobilidade/tsconfig": "*",
|
|
44
|
+
"@types/node": "25.6.0",
|
|
45
|
+
"resolve-tspaths": "0.8.23",
|
|
46
|
+
"tsc-watch": "7.2.0",
|
|
47
|
+
"typescript": "5.9.3"
|
|
48
|
+
}
|
|
49
|
+
}
|