diff --git a/app/api/sdk.js b/app/api/sdk.js deleted file mode 100644 index 6e8c51d..0000000 --- a/app/api/sdk.js +++ /dev/null @@ -1,102 +0,0 @@ -import axios from 'axios' - -export class LangGeniusClient { - constructor(apiKey, baseUrl = 'https://api.langgenius.ai/v1') { - this.apiKey = apiKey - this.baseUrl = baseUrl - } - - 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/app/api/site/route.ts b/app/api/site/route.ts deleted file mode 100644 index 86aff37..0000000 --- a/app/api/site/route.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { type NextRequest } from 'next/server' -import { NextResponse } from 'next/server' -import { getInfo, setSession } from '@/app/api/utils/common' -import { APP_INFO } from '@/config' - -export async function GET(request: NextRequest) { - const { sessionId } = getInfo(request); - return NextResponse.json(APP_INFO, { - headers: setSession(sessionId) - }) -} \ No newline at end of file diff --git a/app/api/utils/common.ts b/app/api/utils/common.ts index 6a3a46a..2330499 100644 --- a/app/api/utils/common.ts +++ b/app/api/utils/common.ts @@ -1,8 +1,10 @@ import { type NextRequest } from 'next/server' import { APP_ID, API_KEY } from '@/config' -import { ChatClient } from '../sdk' +import { ChatClient } from 'langgenius-client' +import uuid from 'uuid' + const userPrefix = `user_${APP_ID}:`; -const uuid = require('uuid') +// const uuid = require('uuid') export const getInfo = (request: NextRequest) => { const sessionId = request.cookies.get('session_id')?.value || uuid.v4(); diff --git a/package.json b/package.json index 9867256..fee6e8a 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "i18next-resources-to-backend": "^1.1.3", "immer": "^9.0.19", "js-cookie": "^3.0.1", + "langgenius-client": "^1.0.0", "negotiator": "^0.6.3", "next": "13.2.4", "react": "18.2.0", diff --git a/service/index.ts b/service/index.ts index 35e3eef..8028e0a 100644 --- a/service/index.ts +++ b/service/index.ts @@ -15,10 +15,6 @@ export const sendChatMessage = async (body: Record, { onData, onCom }, { onData, onCompleted, onError }) } -export const fetchAppInfo = async () => { - return get('site') -} - export const fetchConversations = async () => { return get('conversations', { params: { limit: 20, first_id: '' } }) }