diff --git a/sdks/nodejs-client/.gitignore b/sdks/nodejs-client/.gitignore new file mode 100644 index 0000000000..2c01328155 --- /dev/null +++ b/sdks/nodejs-client/.gitignore @@ -0,0 +1,49 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* +.pnpm-debug.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts + +# npm +package-lock.json + +# yarn +.pnp.cjs +.pnp.loader.mjs +.yarn/ +yarn.lock +.yarnrc.yml + +# pmpm +pnpm-lock.yaml \ No newline at end of file diff --git a/sdks/nodejs-client/README.md b/sdks/nodejs-client/README.md new file mode 100644 index 0000000000..ee78037576 --- /dev/null +++ b/sdks/nodejs-client/README.md @@ -0,0 +1,47 @@ +# LangGenius Node.js SDK +This is the Node.js SDK for the LangGenius API, which allows you to easily integrate LangGenius into your Node.js applications. + +## Install +```bash +npm install langgenius-client +``` + +## Usage +After installing the SDK, you can use it in your project like this: + +```js +import { LangGeniusClient, ChatClient, CompletionClient } from 'langgenius-client' + +const API_KEY = 'your-api-key-here'; +const APP_ID = 'your-app-id-here'; +const user = `user_${APP_ID}:user_id`: + +// Create a completion client +const completionClient = new CompletionClient(API_KEY) +// Create a completion message +completionClient.createCompletionMessage(inputs, query, responseMode, user) + +// Create a chat client +const chatClient = new ChatClient(API_KEY) +// Create a chat message +chatClient.createChatMessage(inputs, query, user, responseMode, conversationId) +// Fetch conversations +chatClient.getConversations(user) +// Fetch conversation messages +chatClient.getConversationMessages(conversationId, user) +// Rename conversation +chatClient.renameConversation(conversationId, name, user) + + +const langGeniusClient = new LangGeniusClient(API_KEY) +// Fetch application parameters +langGeniusClient.getApplicationParameters(user) +// Provide feedback for a message +langGeniusClient.messageFeedback(messageId, rating, user) + +``` + +Replace 'your-api-key-here' with your actual LangGenius API key.Replace 'your-app-id-here' with your actual LangGenius APP ID. + +## License +This SDK is released under the MIT License. \ No newline at end of file diff --git a/sdks/nodejs-client/index.js b/sdks/nodejs-client/index.js new file mode 100644 index 0000000000..8b245d80b8 --- /dev/null +++ b/sdks/nodejs-client/index.js @@ -0,0 +1,106 @@ +import axios from 'axios' + +export class LangGeniusClient { + constructor(apiKey, baseUrl = 'https://api.langgenius.ai/v1') { + this.apiKey = apiKey + this.baseUrl = baseUrl + } + + updateApiKey(apiKey) { + this.apiKey = apiKey + } + + async sendRequest(method, endpoint, data = null, params = null, stream = false) { + const headers = { + 'Authorization': `Bearer ${this.apiKey}`, + 'Content-Type': 'application/json', + } + + const url = `${this.baseUrl}${endpoint}` + let response + if (!stream) { + response = await axios({ + method, + url, + data, + params, + headers, + responseType: stream ? 'stream' : 'json', + }) + } else { + response = await fetch(url, { + headers, + method, + body: JSON.stringify(data), + }) + } + + return response + } + + messageFeedback(messageId, rating, user) { + const data = { + rating, + user, + } + return this.sendRequest('POST', `/messages/${messageId}/feedbacks`, data) + } + + getApplicationParameters(user) { + const params = { user } + return this.sendRequest('GET', '/parameters', null, params) + } +} + +export class CompletionClient extends LangGeniusClient { + createCompletionMessage(inputs, query, responseMode, user) { + const data = { + inputs, + query, + responseMode, + user, + } + return this.sendRequest('POST', '/completion-messages', data, null, responseMode === 'streaming') + } +} + +export class ChatClient extends LangGeniusClient { + createChatMessage(inputs, query, user, responseMode = 'blocking', conversationId = null) { + const data = { + inputs, + query, + user, + responseMode, + } + if (conversationId) + data.conversation_id = conversationId + + return this.sendRequest('POST', '/chat-messages', data, null, responseMode === 'streaming') + } + + getConversationMessages(user, conversationId = '', firstId = null, limit = null) { + const params = { user } + + if (conversationId) + params.conversation_id = conversationId + + if (firstId) + params.first_id = firstId + + if (limit) + params.limit = limit + + return this.sendRequest('GET', '/messages', null, params) + } + + getConversations(user, firstId = null, limit = null, pinned = null) { + const params = { user, first_id: firstId, limit, pinned } + return this.sendRequest('GET', '/conversations', null, params) + } + + renameConversation(conversationId, name, user) { + const data = { name, user } + return this.sendRequest('PATCH', `/conversations/${conversationId}`, data) + } +} + diff --git a/sdks/nodejs-client/package.json b/sdks/nodejs-client/package.json new file mode 100644 index 0000000000..6cbfcfcb7e --- /dev/null +++ b/sdks/nodejs-client/package.json @@ -0,0 +1,16 @@ +{ + "name": "langgenius-client", + "version": "1.0.0", + "description": "This is the Node.js SDK for the LangGenius API, which allows you to easily integrate LangGenius into your Node.js applications.", + "main": "index.js", + "type": "module", + "keywords": [ + "LangGenius", + "LLM" + ], + "author": "Joel", + "license": "MIT", + "dependencies": { + "axios": "^1.3.5" + } +} \ No newline at end of file