Skip to content

Commit bf1ddc3

Browse files
Feature: Add SambaNova (#4961)
* add sambanova * add sambanova credential * fix samba nova chat node --------- Co-authored-by: Henry <[email protected]>
1 parent ad06798 commit bf1ddc3

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { INodeParams, INodeCredential } from '../src/Interface'
2+
3+
class SambanovaApi implements INodeCredential {
4+
label: string
5+
name: string
6+
version: number
7+
inputs: INodeParams[]
8+
9+
constructor() {
10+
this.label = 'Sambanova API'
11+
this.name = 'sambanovaApi'
12+
this.version = 1.0
13+
this.inputs = [
14+
{
15+
label: 'Sambanova Api Key',
16+
name: 'sambanovaApiKey',
17+
type: 'password'
18+
}
19+
]
20+
}
21+
}
22+
23+
module.exports = { credClass: SambanovaApi }
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { BaseCache } from '@langchain/core/caches'
2+
import { ChatOpenAI, ChatOpenAIFields } from '@langchain/openai'
3+
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
4+
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
5+
6+
class ChatSambanova_ChatModels implements INode {
7+
label: string
8+
name: string
9+
version: number
10+
type: string
11+
icon: string
12+
category: string
13+
description: string
14+
baseClasses: string[]
15+
credential: INodeParams
16+
inputs: INodeParams[]
17+
18+
constructor() {
19+
this.label = 'ChatSambanova'
20+
this.name = 'chatSambanova'
21+
this.version = 1.0
22+
this.type = 'ChatSambanova'
23+
this.icon = 'sambanova.png'
24+
this.category = 'Chat Models'
25+
this.description = 'Wrapper around Sambanova Chat Endpoints'
26+
this.baseClasses = [this.type, ...getBaseClasses(ChatOpenAI)]
27+
this.credential = {
28+
label: 'Connect Credential',
29+
name: 'credential',
30+
type: 'credential',
31+
credentialNames: ['sambanovaApi']
32+
}
33+
this.inputs = [
34+
{
35+
label: 'Cache',
36+
name: 'cache',
37+
type: 'BaseCache',
38+
optional: true
39+
},
40+
{
41+
label: 'Model',
42+
name: 'modelName',
43+
type: 'string',
44+
default: 'Meta-Llama-3.3-70B-Instruct',
45+
placeholder: 'Meta-Llama-3.3-70B-Instruct'
46+
},
47+
{
48+
label: 'Temperature',
49+
name: 'temperature',
50+
type: 'number',
51+
step: 0.1,
52+
default: 0.9,
53+
optional: true
54+
},
55+
{
56+
label: 'Streaming',
57+
name: 'streaming',
58+
type: 'boolean',
59+
default: true,
60+
optional: true
61+
},
62+
{
63+
label: 'BasePath',
64+
name: 'basepath',
65+
type: 'string',
66+
optional: true,
67+
default: 'htps://api.sambanova.ai/v1',
68+
additionalParams: true
69+
},
70+
{
71+
label: 'BaseOptions',
72+
name: 'baseOptions',
73+
type: 'json',
74+
optional: true,
75+
additionalParams: true
76+
}
77+
]
78+
}
79+
80+
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
81+
const cache = nodeData.inputs?.cache as BaseCache
82+
const temperature = nodeData.inputs?.temperature as string
83+
const modelName = nodeData.inputs?.modelName as string
84+
const streaming = nodeData.inputs?.streaming as boolean
85+
const basePath = nodeData.inputs?.basepath as string
86+
const baseOptions = nodeData.inputs?.baseOptions
87+
88+
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
89+
const sambanovaApiKey = getCredentialParam('sambanovaApiKey', credentialData, nodeData)
90+
91+
const obj: ChatOpenAIFields = {
92+
temperature: temperature ? parseFloat(temperature) : undefined,
93+
model: modelName,
94+
apiKey: sambanovaApiKey,
95+
openAIApiKey: sambanovaApiKey,
96+
streaming: streaming ?? true
97+
}
98+
99+
if (cache) obj.cache = cache
100+
101+
let parsedBaseOptions: any | undefined = undefined
102+
103+
if (baseOptions) {
104+
try {
105+
parsedBaseOptions = typeof baseOptions === 'object' ? baseOptions : JSON.parse(baseOptions)
106+
} catch (exception) {
107+
throw new Error("Invalid JSON in the ChatSambanova's BaseOptions: " + exception)
108+
}
109+
}
110+
111+
if (basePath || parsedBaseOptions) {
112+
obj.configuration = {
113+
baseURL: basePath,
114+
defaultHeaders: parsedBaseOptions
115+
}
116+
}
117+
118+
const model = new ChatOpenAI(obj)
119+
return model
120+
}
121+
}
122+
123+
module.exports = { nodeClass: ChatSambanova_ChatModels }
12.3 KB
Loading
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { getBaseClasses, getCredentialData, getCredentialParam, ICommonObject, INode, INodeData, INodeParams } from '../../../src'
2+
import { OpenAI } from '@langchain/openai'
3+
import { BaseCache } from '@langchain/core/caches'
4+
5+
class Sambanova_LLMs implements INode {
6+
label: string
7+
name: string
8+
version: number
9+
type: string
10+
icon: string
11+
category: string
12+
description: string
13+
baseClasses: string[]
14+
credential: INodeParams
15+
inputs: INodeParams[]
16+
17+
constructor() {
18+
this.label = 'Sambanova'
19+
this.name = 'sambanova'
20+
this.version = 1.0
21+
this.type = 'Sambanova'
22+
this.icon = 'sambanova.png'
23+
this.category = 'LLMs'
24+
this.description = 'Wrapper around Sambanova API for large language models'
25+
this.baseClasses = [this.type, ...getBaseClasses(OpenAI)]
26+
this.credential = {
27+
label: 'Connect Credential',
28+
name: 'credential',
29+
type: 'credential',
30+
credentialNames: ['sambanovaApi']
31+
}
32+
this.inputs = [
33+
{
34+
label: 'Cache',
35+
name: 'cache',
36+
type: 'BaseCache',
37+
optional: true
38+
},
39+
{
40+
label: 'Model Name',
41+
name: 'modelName',
42+
type: 'string',
43+
default: 'Meta-Llama-3.3-70B-Instruct',
44+
description: 'For more details see https://docs.sambanova.ai/cloud/docs/get-started/supported-models',
45+
optional: true
46+
}
47+
]
48+
}
49+
50+
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
51+
const cache = nodeData.inputs?.cache as BaseCache
52+
const modelName = nodeData.inputs?.modelName as string
53+
54+
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
55+
const sambanovaKey = getCredentialParam('sambanovaApiKey', credentialData, nodeData)
56+
57+
const obj: any = {
58+
model: modelName,
59+
configuration: {
60+
baseURL: 'https://api.sambanova.ai/v1',
61+
apiKey: sambanovaKey
62+
}
63+
}
64+
if (cache) obj.cache = cache
65+
66+
const sambanova = new OpenAI(obj)
67+
return sambanova
68+
}
69+
}
70+
71+
module.exports = { nodeClass: Sambanova_LLMs }
12.3 KB
Loading

packages/server/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ export const isFlowValidForStream = (reactFlowNodes: IReactFlowNode[], endingNod
14901490
'chatTogetherAI',
14911491
'chatTogetherAI_LlamaIndex',
14921492
'chatFireworks',
1493+
'ChatSambanova',
14931494
'chatBaiduWenxin'
14941495
],
14951496
LLMs: ['azureOpenAI', 'openAI', 'ollama']

0 commit comments

Comments
 (0)