mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2026-01-03 12:25:26 +08:00
feat: init
This commit is contained in:
17
app/api/chat-messages/route.ts
Normal file
17
app/api/chat-messages/route.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { type NextRequest } from 'next/server'
|
||||
import { getInfo, client } from '@/app/api/utils/common'
|
||||
import { OpenAIStream } from '@/app/api/utils/stream'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = await request.json()
|
||||
const {
|
||||
inputs,
|
||||
query,
|
||||
conversation_id: conversationId,
|
||||
response_mode: responseMode
|
||||
} = body
|
||||
const { user } = getInfo(request);
|
||||
const res = await client.createChatMessage(inputs, query, user, responseMode, conversationId)
|
||||
const stream = await OpenAIStream(res as any)
|
||||
return new Response(stream as any)
|
||||
}
|
||||
11
app/api/conversations/route.ts
Normal file
11
app/api/conversations/route.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { type NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { getInfo, setSession, client } from '@/app/api/utils/common'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { sessionId, user } = getInfo(request);
|
||||
const { data }: any = await client.getConversations(user);
|
||||
return NextResponse.json(data, {
|
||||
headers: setSession(sessionId)
|
||||
})
|
||||
}
|
||||
13
app/api/messages/route.ts
Normal file
13
app/api/messages/route.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { type NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { getInfo, setSession, client } from '@/app/api/utils/common'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { sessionId, user } = getInfo(request);
|
||||
const { searchParams } = new URL(request.url);
|
||||
const conversationId = searchParams.get('conversation_id')
|
||||
const { data }: any = await client.getConversationMessages(user, conversationId as string);
|
||||
return NextResponse.json(data, {
|
||||
headers: setSession(sessionId)
|
||||
})
|
||||
}
|
||||
11
app/api/parameters/route.ts
Normal file
11
app/api/parameters/route.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { type NextRequest } from 'next/server'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { getInfo, setSession, client } from '@/app/api/utils/common'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const { sessionId, user } = getInfo(request);
|
||||
const { data } = await client.getApplicationParameters(user);
|
||||
return NextResponse.json(data as object, {
|
||||
headers: setSession(sessionId)
|
||||
})
|
||||
}
|
||||
102
app/api/sdk.js
Normal file
102
app/api/sdk.js
Normal file
@ -0,0 +1,102 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
11
app/api/site/route.ts
Normal file
11
app/api/site/route.ts
Normal file
@ -0,0 +1,11 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
20
app/api/utils/common.ts
Normal file
20
app/api/utils/common.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { type NextRequest } from 'next/server'
|
||||
import { APP_ID, API_KEY } from '@/config'
|
||||
import { ChatClient } from '../sdk'
|
||||
const userPrefix = `user_${APP_ID}:`;
|
||||
const uuid = require('uuid')
|
||||
|
||||
export const getInfo = (request: NextRequest) => {
|
||||
const sessionId = request.cookies.get('session_id')?.value || uuid.v4();
|
||||
const user = userPrefix + sessionId;
|
||||
return {
|
||||
sessionId,
|
||||
user
|
||||
}
|
||||
}
|
||||
|
||||
export const setSession = (sessionId: string) => {
|
||||
return { 'Set-Cookie': `session_id=${sessionId}` }
|
||||
}
|
||||
|
||||
export const client = new ChatClient(API_KEY)
|
||||
25
app/api/utils/stream.ts
Normal file
25
app/api/utils/stream.ts
Normal file
@ -0,0 +1,25 @@
|
||||
export async function OpenAIStream(res: { body: any }) {
|
||||
const reader = res.body.getReader();
|
||||
|
||||
const stream = new ReadableStream({
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
|
||||
// https://github.com/whichlight/chatgpt-api-streaming/blob/master/pages/api/OpenAIStream.ts
|
||||
start(controller) {
|
||||
return pump();
|
||||
function pump() {
|
||||
return reader.read().then(({ done, value }: any) => {
|
||||
// When no more data needs to be consumed, close the stream
|
||||
if (done) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
// Enqueue the next data chunk into our target stream
|
||||
controller.enqueue(value);
|
||||
return pump();
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return stream;
|
||||
}
|
||||
Reference in New Issue
Block a user