notdiamond 0.0.2 → 0.0.4

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/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [0.0.2](https://github.com/Not-Diamond/notdiamond-node/pull/4) (2024-05-22)
2
+
3
+ ### Bug Fixes
4
+
5
+ - Cleaned up readme [4c33bd8be65850ef4c5ca34aa9d18c60ee44ead5](https://github.com/Not-Diamond/notdiamond-node/pull/4/commits/4c33bd8be65850ef4c5ca34aa9d18c60ee44ead5)
6
+
7
+ ## [0.0.1](https://github.com/Not-Diamond/notdiamond-node/tree/32bccf352544414e1486c170b90cf00f75041190) (2024-05-22)
8
+
9
+ ### Initial release
10
+
11
+ - Code setup
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm install notdiamond
13
13
  You can import in Deno via:
14
14
 
15
15
  ```ts
16
- import NotDiamond from 'https://deno.land/x/notdiamond@v1.0.0/mod.ts';
16
+ import { NotDiamond } from 'https://deno.land/x/notdiamond@v1.0.0/mod.ts';
17
17
  ```
18
18
 
19
19
  ## Usage
@@ -21,24 +21,32 @@ import NotDiamond from 'https://deno.land/x/notdiamond@v1.0.0/mod.ts';
21
21
  The full API of this library can be found in `api.md` file along with many code examples. The code below shows how to get started using the chat completions API.
22
22
 
23
23
  ```ts
24
- import NotDiamond from 'notdiamond';
24
+ import { NotDiamond } from '../src/notdiamond';
25
25
 
26
- const notdiamond = new NotDiamond();
26
+ const notDiamond = new NotDiamond({
27
+ apiKey: process.env.NOTDIAMOND_API_KEY,
28
+ });
27
29
 
28
30
  async function main() {
29
- const { providers, session_id } = await notdiamond.hashModelSelect({
31
+ const result = await notDiamond.hashModelSelect({
30
32
  messages: [{ content: 'What is 12x12?', role: 'user' }],
31
33
  llmProviders: [
32
- { provider: 'openai', model: 'gpt-4o' },
34
+ { provider: 'openai', model: 'gpt-4' },
35
+ { provider: 'openai', model: 'gpt-3.5-turbo' },
33
36
  { provider: 'anthropic', model: 'claude-3-opus-20240229' },
34
- { provider: 'google', model: 'gemini-1.5-pro' },
37
+ { provider: 'anthropic', model: 'claude-3-sonnet-20240229' },
35
38
  ],
36
39
  preferenceWeights: { quality: 0.7, cost: 0.1, latency: 0.2 },
40
+ maxModelDepth: 2,
37
41
  });
38
-
39
- console.log(providers);
40
- console.log(session_id);
42
+ if ('detail' in result) {
43
+ // There was an error
44
+ console.error(result.detail);
45
+ return;
46
+ }
47
+ const { providers, session_id } = result;
48
+ console.log({ providers, session_id });
41
49
  }
42
50
 
43
- main();
51
+ void main();
44
52
  ```
package/dist/index.cjs ADDED
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ const dotenv = require('dotenv');
4
+
5
+ function _interopNamespaceCompat(e) {
6
+ if (e && typeof e === 'object' && 'default' in e) return e;
7
+ const n = Object.create(null);
8
+ if (e) {
9
+ for (const k in e) {
10
+ n[k] = e[k];
11
+ }
12
+ }
13
+ n.default = e;
14
+ return n;
15
+ }
16
+
17
+ const dotenv__namespace = /*#__PURE__*/_interopNamespaceCompat(dotenv);
18
+
19
+ var __defProp = Object.defineProperty;
20
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
21
+ var __publicField = (obj, key, value) => {
22
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
23
+ return value;
24
+ };
25
+ dotenv__namespace.config();
26
+ const BASE_URL = "https://not-diamond-server.onrender.com";
27
+ const HASH_MODEL_SELECT_URL = `${BASE_URL}/v2/optimizer/hashModelSelect`;
28
+ const FEEDBACK_URL = `${BASE_URL}/v1/report/metrics/feedback`;
29
+ const LATENCY_URL = `${BASE_URL}/v1/report/metrics/latency`;
30
+ class NotDiamond {
31
+ constructor(options = {}) {
32
+ __publicField(this, "apiKey");
33
+ this.apiKey = options.apiKey || process.env.NOTDIAMOND_API_KEY || "";
34
+ }
35
+ getAuthHeader() {
36
+ const authHeader = `Bearer ${this.apiKey}`;
37
+ return authHeader;
38
+ }
39
+ async postRequest(url, body) {
40
+ try {
41
+ const response = await fetch(url, {
42
+ method: "POST",
43
+ headers: {
44
+ Authorization: this.getAuthHeader(),
45
+ accept: "application/json",
46
+ "content-type": "application/json"
47
+ },
48
+ body: JSON.stringify(body)
49
+ });
50
+ if (!response.ok) {
51
+ const errorData = await response.json();
52
+ return { detail: errorData.detail };
53
+ }
54
+ const responseData = await response.json();
55
+ return responseData;
56
+ } catch (error) {
57
+ return { detail: "An unexpected error occurred." };
58
+ }
59
+ }
60
+ async hashModelSelect(options) {
61
+ return this.postRequest(
62
+ HASH_MODEL_SELECT_URL,
63
+ {
64
+ messages: options.messages,
65
+ llm_providers: options.llmProviders,
66
+ ...options.preferenceWeights && {
67
+ preference_weights: options.preferenceWeights
68
+ },
69
+ ...options.maxModelDepth && {
70
+ max_model_depth: options.maxModelDepth
71
+ },
72
+ ...options.tools && { tools: options.tools }
73
+ }
74
+ );
75
+ }
76
+ async feedback(options) {
77
+ return this.postRequest(FEEDBACK_URL, {
78
+ session_id: options.sessionId,
79
+ feedback: options.feedback,
80
+ provider: options.provider
81
+ });
82
+ }
83
+ async latency(options) {
84
+ return this.postRequest(LATENCY_URL, {
85
+ session_id: options.sessionId,
86
+ feedback: options.feedback,
87
+ provider: options.provider
88
+ });
89
+ }
90
+ }
91
+
92
+ exports.NotDiamond = NotDiamond;
@@ -0,0 +1,66 @@
1
+ interface NotDiamondOptions {
2
+ apiKey?: string;
3
+ }
4
+ interface Provider {
5
+ provider: string;
6
+ model: string;
7
+ }
8
+ interface NotDiamondErrorResponse {
9
+ detail: string;
10
+ }
11
+ interface Tool {
12
+ type: string;
13
+ functions: Record<string, string>;
14
+ }
15
+ interface Message {
16
+ content: string;
17
+ role: 'user' | 'assistant' | 'system';
18
+ }
19
+ interface HashModelSelectOptions {
20
+ messages: Message[];
21
+ llmProviders: Provider[];
22
+ tools?: Tool[];
23
+ maxModelDepth?: number;
24
+ preferenceWeights?: {
25
+ quality: number;
26
+ cost: number;
27
+ latency?: number;
28
+ preference_id?: string;
29
+ };
30
+ }
31
+ interface HashModelSelectSuccessResponse {
32
+ providers: Provider[];
33
+ session_id: string;
34
+ }
35
+ interface Feedback {
36
+ accuracy: number;
37
+ }
38
+ interface FeedbackOptions {
39
+ sessionId: string;
40
+ feedback: Feedback;
41
+ provider: Provider;
42
+ }
43
+ interface FeedbackSuccessResponse {
44
+ session_id: string;
45
+ feedback: Feedback;
46
+ }
47
+ interface LatencyOptions {
48
+ sessionId: string;
49
+ feedback: Feedback;
50
+ provider: Provider;
51
+ }
52
+ interface LatencySuccessResponse {
53
+ session_id: string;
54
+ tokens_per_second: number;
55
+ }
56
+ declare class NotDiamond {
57
+ private apiKey;
58
+ constructor(options?: NotDiamondOptions);
59
+ private getAuthHeader;
60
+ private postRequest;
61
+ hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
62
+ feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
63
+ latency(options: LatencyOptions): Promise<LatencySuccessResponse | NotDiamondErrorResponse>;
64
+ }
65
+
66
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type LatencyOptions, type LatencySuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool };
@@ -0,0 +1,66 @@
1
+ interface NotDiamondOptions {
2
+ apiKey?: string;
3
+ }
4
+ interface Provider {
5
+ provider: string;
6
+ model: string;
7
+ }
8
+ interface NotDiamondErrorResponse {
9
+ detail: string;
10
+ }
11
+ interface Tool {
12
+ type: string;
13
+ functions: Record<string, string>;
14
+ }
15
+ interface Message {
16
+ content: string;
17
+ role: 'user' | 'assistant' | 'system';
18
+ }
19
+ interface HashModelSelectOptions {
20
+ messages: Message[];
21
+ llmProviders: Provider[];
22
+ tools?: Tool[];
23
+ maxModelDepth?: number;
24
+ preferenceWeights?: {
25
+ quality: number;
26
+ cost: number;
27
+ latency?: number;
28
+ preference_id?: string;
29
+ };
30
+ }
31
+ interface HashModelSelectSuccessResponse {
32
+ providers: Provider[];
33
+ session_id: string;
34
+ }
35
+ interface Feedback {
36
+ accuracy: number;
37
+ }
38
+ interface FeedbackOptions {
39
+ sessionId: string;
40
+ feedback: Feedback;
41
+ provider: Provider;
42
+ }
43
+ interface FeedbackSuccessResponse {
44
+ session_id: string;
45
+ feedback: Feedback;
46
+ }
47
+ interface LatencyOptions {
48
+ sessionId: string;
49
+ feedback: Feedback;
50
+ provider: Provider;
51
+ }
52
+ interface LatencySuccessResponse {
53
+ session_id: string;
54
+ tokens_per_second: number;
55
+ }
56
+ declare class NotDiamond {
57
+ private apiKey;
58
+ constructor(options?: NotDiamondOptions);
59
+ private getAuthHeader;
60
+ private postRequest;
61
+ hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
62
+ feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
63
+ latency(options: LatencyOptions): Promise<LatencySuccessResponse | NotDiamondErrorResponse>;
64
+ }
65
+
66
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type LatencyOptions, type LatencySuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool };
@@ -0,0 +1,66 @@
1
+ interface NotDiamondOptions {
2
+ apiKey?: string;
3
+ }
4
+ interface Provider {
5
+ provider: string;
6
+ model: string;
7
+ }
8
+ interface NotDiamondErrorResponse {
9
+ detail: string;
10
+ }
11
+ interface Tool {
12
+ type: string;
13
+ functions: Record<string, string>;
14
+ }
15
+ interface Message {
16
+ content: string;
17
+ role: 'user' | 'assistant' | 'system';
18
+ }
19
+ interface HashModelSelectOptions {
20
+ messages: Message[];
21
+ llmProviders: Provider[];
22
+ tools?: Tool[];
23
+ maxModelDepth?: number;
24
+ preferenceWeights?: {
25
+ quality: number;
26
+ cost: number;
27
+ latency?: number;
28
+ preference_id?: string;
29
+ };
30
+ }
31
+ interface HashModelSelectSuccessResponse {
32
+ providers: Provider[];
33
+ session_id: string;
34
+ }
35
+ interface Feedback {
36
+ accuracy: number;
37
+ }
38
+ interface FeedbackOptions {
39
+ sessionId: string;
40
+ feedback: Feedback;
41
+ provider: Provider;
42
+ }
43
+ interface FeedbackSuccessResponse {
44
+ session_id: string;
45
+ feedback: Feedback;
46
+ }
47
+ interface LatencyOptions {
48
+ sessionId: string;
49
+ feedback: Feedback;
50
+ provider: Provider;
51
+ }
52
+ interface LatencySuccessResponse {
53
+ session_id: string;
54
+ tokens_per_second: number;
55
+ }
56
+ declare class NotDiamond {
57
+ private apiKey;
58
+ constructor(options?: NotDiamondOptions);
59
+ private getAuthHeader;
60
+ private postRequest;
61
+ hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
62
+ feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
63
+ latency(options: LatencyOptions): Promise<LatencySuccessResponse | NotDiamondErrorResponse>;
64
+ }
65
+
66
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type LatencyOptions, type LatencySuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool };
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ import * as dotenv from 'dotenv';
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField = (obj, key, value) => {
6
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ return value;
8
+ };
9
+ dotenv.config();
10
+ const BASE_URL = "https://not-diamond-server.onrender.com";
11
+ const HASH_MODEL_SELECT_URL = `${BASE_URL}/v2/optimizer/hashModelSelect`;
12
+ const FEEDBACK_URL = `${BASE_URL}/v1/report/metrics/feedback`;
13
+ const LATENCY_URL = `${BASE_URL}/v1/report/metrics/latency`;
14
+ class NotDiamond {
15
+ constructor(options = {}) {
16
+ __publicField(this, "apiKey");
17
+ this.apiKey = options.apiKey || process.env.NOTDIAMOND_API_KEY || "";
18
+ }
19
+ getAuthHeader() {
20
+ const authHeader = `Bearer ${this.apiKey}`;
21
+ return authHeader;
22
+ }
23
+ async postRequest(url, body) {
24
+ try {
25
+ const response = await fetch(url, {
26
+ method: "POST",
27
+ headers: {
28
+ Authorization: this.getAuthHeader(),
29
+ accept: "application/json",
30
+ "content-type": "application/json"
31
+ },
32
+ body: JSON.stringify(body)
33
+ });
34
+ if (!response.ok) {
35
+ const errorData = await response.json();
36
+ return { detail: errorData.detail };
37
+ }
38
+ const responseData = await response.json();
39
+ return responseData;
40
+ } catch (error) {
41
+ return { detail: "An unexpected error occurred." };
42
+ }
43
+ }
44
+ async hashModelSelect(options) {
45
+ return this.postRequest(
46
+ HASH_MODEL_SELECT_URL,
47
+ {
48
+ messages: options.messages,
49
+ llm_providers: options.llmProviders,
50
+ ...options.preferenceWeights && {
51
+ preference_weights: options.preferenceWeights
52
+ },
53
+ ...options.maxModelDepth && {
54
+ max_model_depth: options.maxModelDepth
55
+ },
56
+ ...options.tools && { tools: options.tools }
57
+ }
58
+ );
59
+ }
60
+ async feedback(options) {
61
+ return this.postRequest(FEEDBACK_URL, {
62
+ session_id: options.sessionId,
63
+ feedback: options.feedback,
64
+ provider: options.provider
65
+ });
66
+ }
67
+ async latency(options) {
68
+ return this.postRequest(LATENCY_URL, {
69
+ session_id: options.sessionId,
70
+ feedback: options.feedback,
71
+ provider: options.provider
72
+ });
73
+ }
74
+ }
75
+
76
+ export { NotDiamond };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "notdiamond",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.4",
5
5
  "author": "not-diamond",
6
6
  "license": "MIT",
7
7
  "description": "TS/JS client for the NotDiamond API",
@@ -25,10 +25,15 @@
25
25
  "dist"
26
26
  ],
27
27
  "keywords": [
28
- "boilerplate",
28
+ "ai",
29
+ "not-diamond",
29
30
  "typescript",
30
- "release",
31
- "swc"
31
+ "openai",
32
+ "chatgpt",
33
+ "anthropic",
34
+ "claude",
35
+ "gemini",
36
+ "model router"
32
37
  ],
33
38
  "scripts": {
34
39
  "prepare": "husky install",
@@ -86,5 +91,9 @@
86
91
  },
87
92
  "resolutions": {
88
93
  "wrap-ansi": "7.0.0"
94
+ },
95
+ "engines": {
96
+ "node": ">=20",
97
+ "npm": ">=8"
89
98
  }
90
99
  }