Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/components/credentials/GreenPTApi.credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { INodeCredential, INodeParams } from '../src/Interface'

class GreenPTApi implements INodeCredential {
label: string
name: string
version: number
inputs: INodeParams[]

constructor() {
this.label = 'GreenPT API'
this.name = 'greenPTApi'
this.version = 1.0
this.inputs = [
{
label: 'GreenPT API Key',
name: 'greenPTApiKey',
type: 'password'
}
]
}
}

module.exports = { credClass: GreenPTApi }
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
jest.mock('@langchain/openai', () => ({
ChatOpenAI: jest.fn().mockImplementation((fields) => ({ fields }))
}))

jest.mock('../../../src/utils', () => ({
getBaseClasses: jest.fn().mockReturnValue(['BaseChatModel']),
getCredentialData: jest.fn(),
getCredentialParam: jest.fn()
}))

jest.mock('../../../src/greenpt', () => ({
GREENPT_API_BASE_URL: 'https://api.greenpt.ai/v1',
listGreenPTModels: jest.fn()
}))

import { listGreenPTModels } from '../../../src/greenpt'
import { getCredentialData, getCredentialParam } from '../../../src/utils'

const { nodeClass: ChatGreenPT } = require('./ChatGreenPT')

describe('ChatGreenPT', () => {
beforeEach(() => jest.clearAllMocks())

it('loads chat models from the GreenPT endpoint', async () => {
;(listGreenPTModels as jest.Mock).mockResolvedValue([{ label: 'glm-5.2', name: 'glm-5.2' }])
const node = new ChatGreenPT()
const nodeData = { credential: 'cred-1' }
const options = { appDataSource: {} }

await expect(node.loadMethods.listModels(nodeData, options)).resolves.toEqual([{ label: 'glm-5.2', name: 'glm-5.2' }])
expect(listGreenPTModels).toHaveBeenCalledWith(nodeData, options, 'chat')
})

it('configures ChatOpenAI for GreenPT', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ greenPTApiKey: 'secret' })
;(getCredentialParam as jest.Mock).mockReturnValue('secret')
const node = new ChatGreenPT()

const model = await node.init(
{
credential: 'cred-1',
inputs: {
modelName: 'glm-5.2',
temperature: '0.2',
streaming: false,
maxTokens: '4096',
topP: '0.8'
}
},
'',
{}
)

expect(model.fields).toMatchObject({
model: 'glm-5.2',
apiKey: 'secret',
openAIApiKey: 'secret',
temperature: 0.2,
streaming: false,
maxTokens: 4096,
topP: 0.8,
configuration: { baseURL: 'https://api.greenpt.ai/v1' }
})
})
})
144 changes: 144 additions & 0 deletions packages/components/nodes/chatmodels/ChatGreenPT/ChatGreenPT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { BaseCache } from '@langchain/core/caches'
import { ChatOpenAI, ChatOpenAIFields } from '@langchain/openai'
import { GREENPT_API_BASE_URL, listGreenPTModels } from '../../../src/greenpt'
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'

class ChatGreenPT_ChatModels implements INode {
label: string
name: string
version: number
type: string
icon: string
category: string
description: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]

constructor() {
this.label = 'GreenPT'
this.name = 'chatGreenPT'
this.version = 1.0
this.type = 'ChatGreenPT'
this.icon = 'greenpt.svg'
this.category = 'Chat Models'
this.description =
'GreenPT is a European AI provider with an OpenAI-compatible API, optimized infrastructure, and data centers powered by 100% renewable energy.'
this.baseClasses = [this.type, ...getBaseClasses(ChatOpenAI)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['greenPTApi']
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'BaseCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
type: 'asyncOptions',
loadMethod: 'listModels',
default: 'glm-5.2',
description: 'Models are loaded from GreenPT. glm-5.2 is the flagship model; kimi-k2.7-code is optimized for coding.'
},
{
label: 'Temperature',
name: 'temperature',
type: 'number',
step: 0.1,
default: 0.7,
optional: true
},
{
label: 'Streaming',
name: 'streaming',
type: 'boolean',
default: true,
optional: true,
additionalParams: true
},
{
label: 'Max Tokens',
name: 'maxTokens',
type: 'number',
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Top Probability',
name: 'topP',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
},
{
label: 'Frequency Penalty',
name: 'frequencyPenalty',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
},
{
label: 'Presence Penalty',
name: 'presencePenalty',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
},
{
label: 'Timeout',
name: 'timeout',
type: 'number',
step: 1,
optional: true,
additionalParams: true
}
]
}

loadMethods = {
async listModels(nodeData: INodeData, options?: ICommonObject): Promise<INodeOptionsValue[]> {
return listGreenPTModels(nodeData, options, 'chat')
}
}

async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const apiKey = getCredentialParam('greenPTApiKey', credentialData, nodeData)
const obj: ChatOpenAIFields = {
model: nodeData.inputs?.modelName as string,
apiKey,
openAIApiKey: apiKey,
temperature: parseFloat(nodeData.inputs?.temperature as string),
streaming: (nodeData.inputs?.streaming as boolean) ?? true,
configuration: { baseURL: GREENPT_API_BASE_URL }
}

const maxTokens = nodeData.inputs?.maxTokens as string
const topP = nodeData.inputs?.topP as string
const frequencyPenalty = nodeData.inputs?.frequencyPenalty as string
const presencePenalty = nodeData.inputs?.presencePenalty as string
const timeout = nodeData.inputs?.timeout as string
const cache = nodeData.inputs?.cache as BaseCache
if (maxTokens) obj.maxTokens = parseInt(maxTokens, 10)
if (topP) obj.topP = parseFloat(topP)
if (frequencyPenalty) obj.frequencyPenalty = parseFloat(frequencyPenalty)
if (presencePenalty) obj.presencePenalty = parseFloat(presencePenalty)
if (timeout) obj.timeout = parseInt(timeout, 10)
if (cache) obj.cache = cache

return new ChatOpenAI(obj)
}
}

module.exports = { nodeClass: ChatGreenPT_ChatModels }
13 changes: 13 additions & 0 deletions packages/components/nodes/chatmodels/ChatGreenPT/greenpt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
jest.mock('@langchain/openai', () => ({
OpenAIEmbeddings: jest.fn().mockImplementation((fields) => ({ fields }))
}))

jest.mock('../../../src/utils', () => ({
getBaseClasses: jest.fn().mockReturnValue(['Embeddings']),
getCredentialData: jest.fn(),
getCredentialParam: jest.fn()
}))

jest.mock('../../../src/greenpt', () => ({
GREENPT_API_BASE_URL: 'https://api.greenpt.ai/v1',
listGreenPTModels: jest.fn()
}))

import { listGreenPTModels } from '../../../src/greenpt'
import { getCredentialData, getCredentialParam } from '../../../src/utils'

const { nodeClass: GreenPTEmbedding } = require('./GreenPTEmbedding')

describe('GreenPTEmbedding', () => {
beforeEach(() => jest.clearAllMocks())

it('loads embedding models from the GreenPT endpoint', async () => {
;(listGreenPTModels as jest.Mock).mockResolvedValue([{ label: 'green-embedding', name: 'green-embedding' }])
const node = new GreenPTEmbedding()
const nodeData = { credential: 'cred-1' }
const options = { appDataSource: {} }

await expect(node.loadMethods.listModels(nodeData, options)).resolves.toEqual([
{ label: 'green-embedding', name: 'green-embedding' }
])
expect(listGreenPTModels).toHaveBeenCalledWith(nodeData, options, 'embedding')
})

it('configures OpenAIEmbeddings for GreenPT', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ greenPTApiKey: 'secret' })
;(getCredentialParam as jest.Mock).mockReturnValue('secret')
const node = new GreenPTEmbedding()

const model = await node.init(
{
credential: 'cred-1',
inputs: {
modelName: 'green-embedding',
stripNewLines: true,
batchSize: '16',
timeout: '15000'
}
},
'',
{}
)

expect(model.fields).toMatchObject({
model: 'green-embedding',
modelName: 'green-embedding',
openAIApiKey: 'secret',
stripNewLines: true,
batchSize: 16,
timeout: 15000,
configuration: { baseURL: 'https://api.greenpt.ai/v1' }
})
})
})
Loading