mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2025-12-08 17:32:27 +08:00
chore: use sdk
This commit is contained in:
102
app/api/sdk.js
102
app/api/sdk.js
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
@ -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();
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -15,10 +15,6 @@ export const sendChatMessage = async (body: Record<string, any>, { onData, onCom
|
||||
}, { onData, onCompleted, onError })
|
||||
}
|
||||
|
||||
export const fetchAppInfo = async () => {
|
||||
return get('site')
|
||||
}
|
||||
|
||||
export const fetchConversations = async () => {
|
||||
return get('conversations', { params: { limit: 20, first_id: '' } })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user