Skip to content

Commit

Permalink
update payment and order
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Aug 30, 2023
1 parent 2b2005b commit dc14699
Show file tree
Hide file tree
Showing 28 changed files with 1,129 additions and 51 deletions.
2 changes: 1 addition & 1 deletion api/anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetChatGPTResponse(message []types.ChatGPTMessage, token int) (string, erro
}

if res.(map[string]interface{})["choices"] == nil {
return "empty", nil
return res.(map[string]interface{})["error"].(map[string]interface{})["message"].(string), nil
}
data := res.(map[string]interface{})["choices"].([]interface{})[0].(map[string]interface{})["message"].(map[string]interface{})["content"]
return data.(string), nil
Expand Down
18 changes: 15 additions & 3 deletions api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,21 @@ func SendSegmentMessage(conn *websocket.Conn, message types.ChatGPTSegmentRespon
_ = conn.WriteMessage(websocket.TextMessage, []byte(utils.ToJson(message)))
}

func TextChat(conn *websocket.Conn, instance *conversation.Conversation) string {
func TextChat(db *sql.DB, user *auth.User, conn *websocket.Conn, instance *conversation.Conversation) string {
keyword, segment := ChatWithWeb(conversation.CopyMessage(instance.GetMessageSegment(12)), true)
SendSegmentMessage(conn, types.ChatGPTSegmentResponse{Keyword: keyword, End: false})

msg := ""
StreamRequest("gpt-3.5-turbo-16k-0613", segment, 2000, func(resp string) {

if instance.IsEnableGPT4() && !auth.ReduceGPT4(db, user) {
SendSegmentMessage(conn, types.ChatGPTSegmentResponse{
Message: "You have run out of GPT-4 usage. Please buy more.",
End: true,
})
return "You have run out of GPT-4 usage. Please buy more."
}

StreamRequest(instance.IsEnableGPT4(), segment, 2000, func(resp string) {
msg += resp
SendSegmentMessage(conn, types.ChatGPTSegmentResponse{
Message: resp,
Expand All @@ -38,6 +47,9 @@ func TextChat(conn *websocket.Conn, instance *conversation.Conversation) string
})
if msg == "" {
msg = "There was something wrong... Please try again later."
if instance.IsEnableGPT4() {
auth.IncreaseGPT4(db, user, 1)
}
SendSegmentMessage(conn, types.ChatGPTSegmentResponse{
Message: msg,
End: false,
Expand Down Expand Up @@ -150,7 +162,7 @@ func ChatAPI(c *gin.Context) {
cache := c.MustGet("cache").(*redis.Client)
msg = ImageChat(conn, instance, user, db, cache)
} else {
msg = TextChat(conn, instance)
msg = TextChat(db, user, conn, instance)
}
instance.SaveResponse(db, msg)
}
Expand Down
5 changes: 4 additions & 1 deletion api/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ func GetImageWithUserLimit(user *auth.User, prompt string, db *sql.DB, cache *re
}

if res == "5" {
return "", fmt.Errorf("you have reached your limit of 5 images per day")
if auth.ReduceDalle(db, user) {
return GetImageWithCache(context.Background(), prompt, cache)
}
return "", fmt.Errorf("you have reached your limit of 5 free images per day, please buy more dalle usage or wait until tomorrow")
} else {
cache.Set(context.Background(), GetLimitFormat(user.GetID(db)), fmt.Sprintf("%d", utils.ToInt(res)+1), time.Hour*24)
return GetImageWithCache(context.Background(), prompt, cache)
Expand Down
19 changes: 15 additions & 4 deletions api/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"chat/utils"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io"
"log"
Expand Down Expand Up @@ -50,26 +51,28 @@ func processLine(buf []byte) []string {
return resp
}

func StreamRequest(model string, messages []types.ChatGPTMessage, token int, callback func(string)) {
func NativeStreamRequest(model string, endpoint string, apikeys string, messages []types.ChatGPTMessage, token int, callback func(string)) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

client := &http.Client{}
req, err := http.NewRequest("POST", viper.GetString("openai.user_endpoint")+"/chat/completions", utils.ConvertBody(types.ChatGPTRequest{
req, err := http.NewRequest("POST", endpoint+"/chat/completions", utils.ConvertBody(types.ChatGPTRequest{
Model: model,
Messages: messages,
MaxToken: token,
Stream: true,
}))
if err != nil {
fmt.Println(err)
return
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+GetRandomKey(viper.GetString("openai.user")))
req.Header.Set("Authorization", "Bearer "+GetRandomKey(apikeys))

res, err := client.Do(req)
if err != nil {
log.Fatal(err)
fmt.Println(err)
return
}
defer res.Body.Close()

Expand All @@ -89,3 +92,11 @@ func StreamRequest(model string, messages []types.ChatGPTMessage, token int, cal
}
}
}

func StreamRequest(enableGPT4 bool, messages []types.ChatGPTMessage, token int, callback func(string)) {
if enableGPT4 {
NativeStreamRequest("gpt-4", viper.GetString("openai.gpt4_endpoint"), viper.GetString("openai.gpt4"), messages, token, callback)
} else {
NativeStreamRequest("gpt-3.5-turbo-16k-0613", viper.GetString("openai.user_endpoint"), viper.GetString("openai.user"), messages, token, callback)
}
}
7 changes: 7 additions & 0 deletions app/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const router = createRouter({ //@ts-ignore
meta: {
title: "Login | Chat Nio",
}
}, {
path: "/settings",
name: "settings",
component: () => import("../src/views/SettingsView.vue"),
meta: {
title: "Settings | Chat Nio",
}
}
],
});
Expand Down
10 changes: 6 additions & 4 deletions app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import Delete from "./components/icons/delete.vue";
import { deleteConversation } from "./assets/script/api";
import {ref} from "vue";
import Close from "./components/icons/close.vue";
import Notification from "./components/Notification.vue";
const current = manager.getCurrent();
const sidebar = ref(false), padding = ref(false);
function goto() {
window.location.href = "https://deeptrain.net/login?app=chatnio";
window.location.href = "https://deeptrain.lightxi.com/login?app=chatnio";
}
function toggle(n: boolean) {
Expand All @@ -37,6 +38,7 @@ function toggleConversation(id: number) {
</script>

<template>
<Notification />
<div class="sidebar conversation-container mobile" v-if="mobile" :class="{'active': sidebar, 'padding': padding}">
<div class="operation-wrapper" v-if="mobile">
<div class="grow" />
Expand Down Expand Up @@ -88,10 +90,10 @@ function toggleConversation(id: number) {
</div>
</div>
<div class="grow" />
<div class="user" v-if="auth">
<router-link to="/settings" class="user" v-if="auth">
<img class="avatar" :src="'https://api.deeptrain.net/avatar/' + username" alt="" @click="setSidebar(true)">
<span class="username">{{ username }}</span>
</div>
</router-link>
<div class="login" v-else>
<button @click="goto">
<login />
Expand All @@ -105,7 +107,7 @@ function toggleConversation(id: number) {
</div>
<div class="copyright">
<a href="https://github.com/zmh-program/chatnio" target="_blank"><github /> chatnio</a>
<a href="https://deeptrain.net" target="_blank">© 2023 Deeptrain Team</a>
<a href="https://deeptrain.lightxi.com" target="_blank">© 2023 Deeptrain Team</a>
</div>
</template>

Expand Down
12 changes: 12 additions & 0 deletions app/src/assets/script/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import axios from "axios";
import { auth, token } from "./auth";
import { ws_api } from "./conf";
import { gpt4 } from "./shared";
import {notify} from "./notify";

var state = true;

export type Message = {
content: string;
Expand Down Expand Up @@ -37,13 +40,21 @@ export class Connection {
this.state = false;
this.connection.onopen = () => {
this.state = true;
if (!state) {
notify("服务器连接已恢复", 1000);
state = true;
}
this.send({
token: token.value,
id: this.id,
})
}
this.connection.onclose = () => {
this.state = false;
if (state) {
notify("服务器连接已断开,正在尝试重连中...", 3000);
state = false;
}
setTimeout(() => {
this.init();
}, 3000);
Expand Down Expand Up @@ -130,6 +141,7 @@ export class Conversation {
})
const status = this.connection?.send({
message: content,
gpt4: gpt4.value,
});
if (status) {
this.addDynamicMessageFromAI(message, keyword, end);
Expand Down
26 changes: 26 additions & 0 deletions app/src/assets/script/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {ref} from "vue";

type Notification = {
content: string;
expire: number;
leave?: boolean;
}

export const notifications = ref<Notification[]>([]);

export function notify(content: string, expire: number = 5000): void {
if (!notifications.value) notifications.value = [];
notifications.value.push({content, expire: (new Date()).getTime() + expire});
}

setInterval(() => {
if (!notifications.value) return;
const now = (new Date()).getTime();
// leave animation: 0.5s
notifications.value = notifications.value.filter((notification) => {
if (notification.expire < now) {
notification.leave = true;
}
return notification.expire > now - 500;
});
}, 800);
85 changes: 85 additions & 0 deletions app/src/components/Notification.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<script setup lang="ts">
import { notifications } from "../assets/script/notify";
</script>

<template>
<div class="notification-wrapper">
<div class="notification" v-for="(notification, index) in notifications" :key="index" :class="{'leave': notification.leave}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="icon">
<path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm0 18c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8z"></path><path d="M11 11h2v6h-2zm0-4h2v2h-2z"></path>
</svg>
<div class="content">{{ notification.content }}</div>
</div>
</div>
</template>

<style scoped>
.notification-wrapper {
position: fixed;
top: 0;
width: 100%;
height: max-content;
pointer-events: none;
z-index: 64;
align-items: center;
justify-content: center;
}
.notification {
display: flex;
flex-direction: row;
align-items: center;
vertical-align: center;
justify-content: center;
margin: 12px auto;
width: max-content;
min-width: 160px;
text-align: center;
height: min-content;
padding: 12px 24px;
border-radius: 8px;
background: #202121;
border: 1px solid #2d2d2f;
color: #ddddde;
font-size: 16px;
user-select: none;
animation: FadeInAnimation 0.5s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
}
.icon {
width: 24px;
height: 24px;
margin-right: 8px;
fill: #ddddde;
vertical-align: middle;
user-select: none;
}
.leave {
animation: FadeOutAnimation 0.5s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
}
@keyframes FadeInAnimation {
0% {
opacity: 0;
transform: translateY(12px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
@keyframes FadeOutAnimation {
0% {
opacity: 1;
transform: translateY(0);
}
100% {
opacity: 0;
transform: translateY(12px);
}
}
</style>
3 changes: 3 additions & 0 deletions app/src/components/icons/back.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M13.293 6.293 7.586 12l5.707 5.707 1.414-1.414L10.414 12l4.293-4.293z"></path></svg>
</template>
3 changes: 3 additions & 0 deletions app/src/components/icons/buy.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><circle cx="10.5" cy="19.5" r="1.5"></circle><circle cx="17.5" cy="19.5" r="1.5"></circle><path d="m14 13.99 4-5h-3v-4h-2v4h-3l4 5z"></path><path d="M17.31 15h-6.64L6.18 4.23A2 2 0 0 0 4.33 3H2v2h2.33l4.75 11.38A1 1 0 0 0 10 17h8a1 1 0 0 0 .93-.64L21.76 9h-2.14z"></path></svg>
</template>
3 changes: 3 additions & 0 deletions app/src/components/icons/chart.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M2 2h20v2H2z"></path><rect x="5" y="6" width="6" height="16" rx="1"></rect><rect x="13" y="6" width="6" height="12" rx="1"></rect></svg>
</template>
3 changes: 3 additions & 0 deletions app/src/components/icons/draw.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M13.4 2.096a10.08 10.08 0 0 0-8.937 3.331A10.054 10.054 0 0 0 2.096 13.4c.53 3.894 3.458 7.207 7.285 8.246a9.982 9.982 0 0 0 2.618.354l.142-.001a3.001 3.001 0 0 0 2.516-1.426 2.989 2.989 0 0 0 .153-2.879l-.199-.416a1.919 1.919 0 0 1 .094-1.912 2.004 2.004 0 0 1 2.576-.755l.412.197c.412.198.85.299 1.301.299A3.022 3.022 0 0 0 22 12.14a9.935 9.935 0 0 0-.353-2.76c-1.04-3.826-4.353-6.754-8.247-7.284zm5.158 10.909-.412-.197c-1.828-.878-4.07-.198-5.135 1.494-.738 1.176-.813 2.576-.204 3.842l.199.416a.983.983 0 0 1-.051.961.992.992 0 0 1-.844.479h-.112a8.061 8.061 0 0 1-2.095-.283c-3.063-.831-5.403-3.479-5.826-6.586-.321-2.355.352-4.623 1.893-6.389a8.002 8.002 0 0 1 7.16-2.664c3.107.423 5.755 2.764 6.586 5.826.198.73.293 1.474.282 2.207-.012.807-.845 1.183-1.441.894z"></path><circle cx="7.5" cy="14.5" r="1.5"></circle><circle cx="7.5" cy="10.5" r="1.5"></circle><circle cx="10.5" cy="7.5" r="1.5"></circle><circle cx="14.5" cy="7.5" r="1.5"></circle></svg>
</template>
3 changes: 3 additions & 0 deletions app/src/components/icons/gift.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M5 12H4v8a2 2 0 0 0 2 2h5V12H5zm13 0h-5v10h5a2 2 0 0 0 2-2v-8h-2zm.791-5A4.92 4.92 0 0 0 19 5.5C19 3.57 17.43 2 15.5 2c-1.622 0-2.705 1.482-3.404 3.085C11.407 3.57 10.269 2 8.5 2 6.57 2 5 3.57 5 5.5c0 .596.079 1.089.209 1.5H2v4h9V9h2v2h9V7h-3.209zM7 5.5C7 4.673 7.673 4 8.5 4c.888 0 1.714 1.525 2.198 3H8c-.374 0-1 0-1-1.5zM15.5 4c.827 0 1.5.673 1.5 1.5C17 7 16.374 7 16 7h-2.477c.51-1.576 1.251-3 1.977-3z"></path></svg>
</template>
3 changes: 3 additions & 0 deletions app/src/components/icons/sale.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 15c-1.84 0-2-.86-2-1H8c0 .92.66 2.55 3 2.92V18h2v-1.08c2-.34 3-1.63 3-2.92 0-1.12-.52-3-4-3-2 0-2-.63-2-1s.7-1 2-1 1.39.64 1.4 1h2A3 3 0 0 0 13 7.12V6h-2v1.09C9 7.42 8 8.71 8 10c0 1.12.52 3 4 3 2 0 2 .68 2 1s-.62 1-2 1z"></path><path d="M5 2H2v2h2v17a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1V4h2V2H5zm13 18H6V4h12z"></path></svg>
</template>
11 changes: 11 additions & 0 deletions app/src/components/icons/wallet.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path
d="M95.5 104h320a87.73 87.73 0 0111.18.71 66 66 0 00-77.51-55.56L86 94.08h-.3a66 66 0 00-41.07 26.13A87.57 87.57 0 0195.5 104zM415.5 128h-320a64.07 64.07 0 00-64 64v192a64.07 64.07 0 0064 64h320a64.07 64.07 0 0064-64V192a64.07 64.07 0 00-64-64zM368 320a32 32 0 1132-32 32 32 0 01-32 32z"
/>
<path
d="M32 259.5V160c0-21.67 12-58 53.65-65.87C121 87.5 156 87.5 156 87.5s23 16 4 16-18.5 24.5 0 24.5 0 23.5 0 23.5L85.5 236z"
fill="rgba(255, 255, 255, 0.1)"
/>
</svg>
</template>
Loading

0 comments on commit dc14699

Please sign in to comment.