notdiamond 0.0.20 → 0.0.21

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 CHANGED
@@ -1,10 +1,6 @@
1
1
  # NotDiamond Node API Library
2
2
 
3
- This library provides convenient access to the NotDiamond model routing API from TypeScript or JavaScript.
4
-
5
- To use the API, you will need to sign up for a NotDiamond account and obtain an API key. [You can sign up here.](https://app.notdiamond.ai)
6
-
7
- To learn how to use the NotDiamond API, check out our [API Reference and Documentation.](https://notdiamond.readme.io/v0.1.0-beta/docs/getting-started)
3
+ This library provides convenient access to the NotDiamond model routing API from TypeScript or JavaScript. NotDiamond helps you select the best AI model for your specific use case, optimizing for factors like cost, latency, or performance.
8
4
 
9
5
  ## Installation
10
6
 
@@ -12,42 +8,122 @@ To learn how to use the NotDiamond API, check out our [API Reference and Documen
12
8
  npm install notdiamond
13
9
  ```
14
10
 
15
- You can import in Deno via:
11
+ For Deno users:
16
12
 
17
13
  ```ts
18
14
  import { NotDiamond } from 'https://deno.land/x/notdiamond@v1.0.0/mod.ts';
19
15
  ```
20
16
 
21
- ## Usage
17
+ ## Quick Start
22
18
 
23
- 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.
19
+ To use the API, you need to sign up for a NotDiamond account and obtain an API key. [Sign up here](https://app.notdiamond.ai).
24
20
 
25
- ```ts
21
+ ### Basic Usage
22
+
23
+ Here's a simple example of how to use NotDiamond to select the best model between GPT-4o, Claude 3.5 Sonnet, and Gemini 1.5 Pro, while optimizing for latency:
24
+
25
+ ```typescript
26
26
  import { NotDiamond } from 'notdiamond';
27
27
 
28
28
  const notDiamond = new NotDiamond({
29
29
  apiKey: process.env.NOTDIAMOND_API_KEY,
30
30
  });
31
31
 
32
- async function main() {
33
- const result = await notDiamond.hashModelSelect({
32
+ async function basicExample() {
33
+ const result = await notDiamond.modelSelect({
34
+ messages: [{ content: 'What is 12x12?', role: 'user' }],
35
+ llmProviders: [
36
+ { provider: 'openai', model: 'gpt-4o-2024-05-13' },
37
+ { provider: 'anthropic', model: 'claude-3-5-sonnet-20240620' },
38
+ { provider: 'google', model: 'gemini-1.5-pro-latest' },
39
+ ],
40
+ tradeoff: 'latency',
41
+ });
42
+
43
+ if ('detail' in result) {
44
+ console.error('Error:', result.detail);
45
+ return;
46
+ }
47
+
48
+ console.log('Selected providers:', result.providers);
49
+ console.log('Session ID:', result.session_id);
50
+ }
51
+
52
+ basicExample();
53
+ ```
54
+
55
+ ### Advanced Usage with Tools
56
+
57
+ You can also use NotDiamond with custom tools:
58
+
59
+ ```typescript
60
+ import { NotDiamond, Tool } from 'notdiamond';
61
+
62
+ const notDiamond = new NotDiamond({
63
+ apiKey: process.env.NOTDIAMOND_API_KEY,
64
+ });
65
+
66
+ const tools: Tool[] = [
67
+ {
68
+ type: 'function',
69
+ function: {
70
+ name: 'multiply',
71
+ description: 'Multiplies two numbers.',
72
+ parameters: {
73
+ type: 'object',
74
+ properties: {
75
+ a: { type: 'integer' },
76
+ b: { type: 'integer' },
77
+ },
78
+ required: ['a', 'b'],
79
+ },
80
+ },
81
+ },
82
+ ];
83
+
84
+ async function toolCallingExample() {
85
+ const result = await notDiamond.modelSelect({
34
86
  messages: [{ content: 'What is 12x12?', role: 'user' }],
35
87
  llmProviders: [
36
- { provider: 'openai', model: 'gpt-4' },
37
- { provider: 'openai', model: 'gpt-3.5-turbo' },
38
- { provider: 'anthropic', model: 'claude-3-opus-20240229' },
88
+ { provider: 'openai', model: 'gpt-4-1106-preview' },
39
89
  { provider: 'anthropic', model: 'claude-3-sonnet-20240229' },
40
90
  ],
91
+ tools: tools,
41
92
  tradeoff: 'cost',
93
+ maxModelDepth: 2,
42
94
  });
95
+
43
96
  if ('detail' in result) {
44
- // There was an error
45
- console.error(result.detail);
97
+ console.error('Error:', result.detail);
46
98
  return;
47
99
  }
48
- const { providers, session_id } = result;
49
- console.log({ providers, session_id });
100
+
101
+ console.log('Selected providers:', result.providers);
102
+ console.log('Session ID:', result.session_id);
50
103
  }
51
104
 
52
- void main();
105
+ toolCallingExample();
53
106
  ```
107
+
108
+ ## API Reference
109
+
110
+ For a complete API reference and more detailed documentation, please check our [API Documentation](https://notdiamond.readme.io/v0.1.0-beta/docs/getting-started).
111
+
112
+ ## Key Concepts
113
+
114
+ - **llmProviders**: An array of AI providers and models you want NotDiamond to choose from.
115
+ - **tradeoff**: The factor to optimize for (e.g., 'latency', 'cost', 'performance').
116
+ - **tools**: Custom functions that the AI can use to perform specific tasks.
117
+ - **maxModelDepth**: The maximum number of models to include in the response (for advanced use cases).
118
+
119
+ ## Error Handling
120
+
121
+ NotDiamond uses typed responses. If there's an error, the response will have a `detail` property with the error message. Always check for this property when handling responses.
122
+
123
+ ## Support
124
+
125
+ If you encounter any issues or have questions, please [open an issue](https://github.com/Not-Diamond/notdiamond-node) on our GitHub repository.
126
+
127
+ ## License
128
+
129
+ This library is released under the MIT License.
package/dist/index.cjs CHANGED
@@ -24,8 +24,9 @@ var __publicField = (obj, key, value) => {
24
24
  };
25
25
  dotenv__namespace.config();
26
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`;
27
+ const MODEL_SELECT_URL = `${BASE_URL}/v2/optimizer/modelSelect`;
28
+ const FEEDBACK_URL = `${BASE_URL}/v2/report/metrics/feedback`;
29
+ const DEFAULT_TIMEOUT = 5;
29
30
  class NotDiamond {
30
31
  constructor(options = {}) {
31
32
  __publicField(this, "apiKey");
@@ -64,25 +65,31 @@ class NotDiamond {
64
65
  return { detail: "An unexpected error occurred." };
65
66
  }
66
67
  }
67
- async hashModelSelect(options) {
68
- console.log("Calling hashModelSelect with options:", options);
68
+ async modelSelect(options) {
69
+ console.log("Calling modelSelect with options:", options);
70
+ const requestBody = {
71
+ messages: options.messages,
72
+ llm_providers: options.llmProviders,
73
+ ...options.tradeoff && {
74
+ tradeoff: options.tradeoff
75
+ },
76
+ ...options.maxModelDepth && {
77
+ max_model_depth: options.maxModelDepth
78
+ },
79
+ ...options.tools && { tools: options.tools },
80
+ ...options.hashContent !== void 0 && {
81
+ hash_content: options.hashContent
82
+ },
83
+ ...options.preferenceId && { preference_id: options.preferenceId },
84
+ ...options.timeout ? { timeout: options.timeout } : {
85
+ timeout: DEFAULT_TIMEOUT
86
+ },
87
+ ...options.default && { default: options.default }
88
+ };
89
+ console.log("Request body:", JSON.stringify(requestBody, null, 2));
69
90
  return this.postRequest(
70
- HASH_MODEL_SELECT_URL,
71
- {
72
- messages: options.messages,
73
- llm_providers: options.llmProviders,
74
- ...options.tradeoff && {
75
- tradeoff: options.tradeoff
76
- },
77
- ...options.maxModelDepth && {
78
- max_model_depth: options.maxModelDepth
79
- },
80
- ...options.tools && { tools: options.tools },
81
- ...options.hashContent !== void 0 && {
82
- hash_content: options.hashContent
83
- },
84
- ...options.preferenceId && { preference_id: options.preferenceId }
85
- }
91
+ MODEL_SELECT_URL,
92
+ requestBody
86
93
  );
87
94
  }
88
95
  async feedback(options) {
package/dist/index.d.cts CHANGED
@@ -21,7 +21,7 @@ interface Message {
21
21
  content: string;
22
22
  role: 'user' | 'assistant' | 'system';
23
23
  }
24
- interface HashModelSelectOptions {
24
+ interface ModelSelectOptions {
25
25
  messages: Message[];
26
26
  llmProviders: Provider[];
27
27
  tools?: Tool[];
@@ -29,8 +29,10 @@ interface HashModelSelectOptions {
29
29
  tradeoff?: 'cost' | 'latency';
30
30
  preferenceId?: string;
31
31
  hashContent?: boolean;
32
+ timeout?: number;
33
+ default?: Provider | number | string;
32
34
  }
33
- interface HashModelSelectSuccessResponse {
35
+ interface ModelSelectSuccessResponse {
34
36
  providers: Provider[];
35
37
  session_id: string;
36
38
  }
@@ -51,8 +53,8 @@ declare class NotDiamond {
51
53
  constructor(options?: NotDiamondOptions);
52
54
  private getAuthHeader;
53
55
  private postRequest;
54
- hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
56
+ modelSelect(options: ModelSelectOptions): Promise<ModelSelectSuccessResponse | NotDiamondErrorResponse>;
55
57
  feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
56
58
  }
57
59
 
58
- export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
60
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type Message, type ModelSelectOptions, type ModelSelectSuccessResponse, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
package/dist/index.d.mts CHANGED
@@ -21,7 +21,7 @@ interface Message {
21
21
  content: string;
22
22
  role: 'user' | 'assistant' | 'system';
23
23
  }
24
- interface HashModelSelectOptions {
24
+ interface ModelSelectOptions {
25
25
  messages: Message[];
26
26
  llmProviders: Provider[];
27
27
  tools?: Tool[];
@@ -29,8 +29,10 @@ interface HashModelSelectOptions {
29
29
  tradeoff?: 'cost' | 'latency';
30
30
  preferenceId?: string;
31
31
  hashContent?: boolean;
32
+ timeout?: number;
33
+ default?: Provider | number | string;
32
34
  }
33
- interface HashModelSelectSuccessResponse {
35
+ interface ModelSelectSuccessResponse {
34
36
  providers: Provider[];
35
37
  session_id: string;
36
38
  }
@@ -51,8 +53,8 @@ declare class NotDiamond {
51
53
  constructor(options?: NotDiamondOptions);
52
54
  private getAuthHeader;
53
55
  private postRequest;
54
- hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
56
+ modelSelect(options: ModelSelectOptions): Promise<ModelSelectSuccessResponse | NotDiamondErrorResponse>;
55
57
  feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
56
58
  }
57
59
 
58
- export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
60
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type Message, type ModelSelectOptions, type ModelSelectSuccessResponse, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
package/dist/index.d.ts CHANGED
@@ -21,7 +21,7 @@ interface Message {
21
21
  content: string;
22
22
  role: 'user' | 'assistant' | 'system';
23
23
  }
24
- interface HashModelSelectOptions {
24
+ interface ModelSelectOptions {
25
25
  messages: Message[];
26
26
  llmProviders: Provider[];
27
27
  tools?: Tool[];
@@ -29,8 +29,10 @@ interface HashModelSelectOptions {
29
29
  tradeoff?: 'cost' | 'latency';
30
30
  preferenceId?: string;
31
31
  hashContent?: boolean;
32
+ timeout?: number;
33
+ default?: Provider | number | string;
32
34
  }
33
- interface HashModelSelectSuccessResponse {
35
+ interface ModelSelectSuccessResponse {
34
36
  providers: Provider[];
35
37
  session_id: string;
36
38
  }
@@ -51,8 +53,8 @@ declare class NotDiamond {
51
53
  constructor(options?: NotDiamondOptions);
52
54
  private getAuthHeader;
53
55
  private postRequest;
54
- hashModelSelect(options: HashModelSelectOptions): Promise<HashModelSelectSuccessResponse | NotDiamondErrorResponse>;
56
+ modelSelect(options: ModelSelectOptions): Promise<ModelSelectSuccessResponse | NotDiamondErrorResponse>;
55
57
  feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
56
58
  }
57
59
 
58
- export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type HashModelSelectOptions, type HashModelSelectSuccessResponse, type Message, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
60
+ export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type Message, type ModelSelectOptions, type ModelSelectSuccessResponse, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, type Provider, type Tool, type ToolFunction };
package/dist/index.mjs CHANGED
@@ -8,8 +8,9 @@ var __publicField = (obj, key, value) => {
8
8
  };
9
9
  dotenv.config();
10
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`;
11
+ const MODEL_SELECT_URL = `${BASE_URL}/v2/optimizer/modelSelect`;
12
+ const FEEDBACK_URL = `${BASE_URL}/v2/report/metrics/feedback`;
13
+ const DEFAULT_TIMEOUT = 5;
13
14
  class NotDiamond {
14
15
  constructor(options = {}) {
15
16
  __publicField(this, "apiKey");
@@ -48,25 +49,31 @@ class NotDiamond {
48
49
  return { detail: "An unexpected error occurred." };
49
50
  }
50
51
  }
51
- async hashModelSelect(options) {
52
- console.log("Calling hashModelSelect with options:", options);
52
+ async modelSelect(options) {
53
+ console.log("Calling modelSelect with options:", options);
54
+ const requestBody = {
55
+ messages: options.messages,
56
+ llm_providers: options.llmProviders,
57
+ ...options.tradeoff && {
58
+ tradeoff: options.tradeoff
59
+ },
60
+ ...options.maxModelDepth && {
61
+ max_model_depth: options.maxModelDepth
62
+ },
63
+ ...options.tools && { tools: options.tools },
64
+ ...options.hashContent !== void 0 && {
65
+ hash_content: options.hashContent
66
+ },
67
+ ...options.preferenceId && { preference_id: options.preferenceId },
68
+ ...options.timeout ? { timeout: options.timeout } : {
69
+ timeout: DEFAULT_TIMEOUT
70
+ },
71
+ ...options.default && { default: options.default }
72
+ };
73
+ console.log("Request body:", JSON.stringify(requestBody, null, 2));
53
74
  return this.postRequest(
54
- HASH_MODEL_SELECT_URL,
55
- {
56
- messages: options.messages,
57
- llm_providers: options.llmProviders,
58
- ...options.tradeoff && {
59
- tradeoff: options.tradeoff
60
- },
61
- ...options.maxModelDepth && {
62
- max_model_depth: options.maxModelDepth
63
- },
64
- ...options.tools && { tools: options.tools },
65
- ...options.hashContent !== void 0 && {
66
- hash_content: options.hashContent
67
- },
68
- ...options.preferenceId && { preference_id: options.preferenceId }
69
- }
75
+ MODEL_SELECT_URL,
76
+ requestBody
70
77
  );
71
78
  }
72
79
  async feedback(options) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "notdiamond",
3
3
  "type": "module",
4
- "version": "0.0.20",
4
+ "version": "0.0.21",
5
5
  "author": "not-diamond",
6
6
  "license": "MIT",
7
7
  "description": "TS/JS client for the NotDiamond API",