forked from langgenius/webapp-conversation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
5,299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# EditorConfig is awesome: https://EditorConfig.org | ||
|
||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
# Matches multiple files with brace expansion notation | ||
# Set default charset | ||
[*.{js,tsx}] | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
|
||
# Matches the exact files either package.json or .travis.yml | ||
[{package.json,.travis.yml}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"extends": [ | ||
"@antfu", | ||
"plugin:react-hooks/recommended" | ||
], | ||
"rules": { | ||
"@typescript-eslint/consistent-type-definitions": [ | ||
"error", | ||
"type" | ||
], | ||
"no-console": "off", | ||
"indent": "off", | ||
"@typescript-eslint/indent": [ | ||
"error", | ||
2, | ||
{ | ||
"SwitchCase": 1, | ||
"flatTernaryExpressions": false, | ||
"ignoredNodes": [ | ||
"PropertyDefinition[decorators]", | ||
"TSUnionType", | ||
"FunctionExpression[params]:has(Identifier[decorators])" | ||
] | ||
} | ||
], | ||
"react-hooks/exhaustive-deps": "warn" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"version": "0.1.0", | ||
"configurations": [ | ||
{ | ||
"name": "Next.js: debug server-side", | ||
"type": "node-terminal", | ||
"request": "launch", | ||
"command": "npm run dev" | ||
}, | ||
{ | ||
"name": "Next.js: debug client-side", | ||
"type": "chrome", | ||
"request": "launch", | ||
"url": "http://localhost:3000" | ||
}, | ||
{ | ||
"name": "Next.js: debug full stack", | ||
"type": "node-terminal", | ||
"request": "launch", | ||
"command": "npm run dev", | ||
"serverReadyAction": { | ||
"pattern": "started server on .+, url: (https?://.+)", | ||
"uriFormat": "%s", | ||
"action": "debugWithChrome" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"typescript.tsdk": ".yarn/cache/typescript-patch-72dc6f164f-ab417a2f39.zip/node_modules/typescript/lib", | ||
"typescript.enablePromptUseWorkspaceTsdk": true, | ||
"prettier.enable": false, | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
}, | ||
"[python]": { | ||
"editor.formatOnType": true | ||
}, | ||
"[html]": { | ||
"editor.defaultFormatter": "vscode.html-language-features" | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "vscode.typescript-language-features" | ||
}, | ||
"[javascript]": { | ||
"editor.defaultFormatter": "vscode.typescript-language-features" | ||
}, | ||
"[javascriptreact]": { | ||
"editor.defaultFormatter": "vscode.typescript-language-features" | ||
}, | ||
"[jsonc]": { | ||
"editor.defaultFormatter": "vscode.json-language-features" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Conversion Web App Template | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). | ||
|
||
## Set App Info | ||
Set app info in `config/index.ts`. Includes: | ||
- APP_ID | ||
- API_KEY | ||
- APP_INFO | ||
|
||
## Getting Started | ||
First, install dependencies: | ||
```bash | ||
npm install | ||
# or | ||
yarn | ||
# or | ||
pnpm install | ||
``` | ||
|
||
Then, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`. | ||
|
||
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
Oops, something went wrong.