Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoel committed Apr 14, 2023
1 parent 8acd8f6 commit 97d3a62
Show file tree
Hide file tree
Showing 77 changed files with 5,299 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
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
28 changes: 28 additions & 0 deletions .eslintrc.json
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"
}
}
49 changes: 49 additions & 0 deletions .gitignore
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
28 changes: 28 additions & 0 deletions .vscode/launch.json
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"
}
}
]
}
27 changes: 27 additions & 0 deletions .vscode/settings.json
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"
}
}
53 changes: 53 additions & 0 deletions README.md
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.
17 changes: 17 additions & 0 deletions app/api/chat-messages/route.ts
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)
}
11 changes: 11 additions & 0 deletions app/api/conversations/route.ts
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)
})
}
13 changes: 13 additions & 0 deletions app/api/messages/route.ts
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)
})
}
11 changes: 11 additions & 0 deletions app/api/parameters/route.ts
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)
})
}
102 changes: 102 additions & 0 deletions app/api/sdk.js
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)
}
}

11 changes: 11 additions & 0 deletions app/api/site/route.ts
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)
})
}
Loading

0 comments on commit 97d3a62

Please sign in to comment.